Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts

This is an algorithm for stack using linked list.


DATA STRUCTURE DEFINITION

___________________________________________________________________________________

 

NODE: Data {To store data  in  the Node}

Next {To store link of  the next Node}

_____________________________________________________________________

Algorithm for Operations in a Stack

Algorithm for push operation

_______________________________________________________________________________

INPUT : A value to be inserted

OUTPUT : The data value inserted at the end of the list ___________________________________________________________________________________

Step 1: Input v

Step 2: new node = createnode(creates a new node)

Step 3: new node->data = v

Step 4: new node->next = NULL

Step 5: if head = NULL then,

A.                   head=new node

Step 6: else

A. New node->next = head

B. Head = new node

(End of if...else)

Step 7: top++

Algorithm for pop operation _________________________________________________________________________________

INPUT : A list of data

OUTPUT : The data value to be deleted from beginning of the list

___________________________________________________________________________________

Step 1: Input p

Step 2: If top > = 1 then

A.                   tmp = head

B.                   Head = head ->next

C.                   v = tmp->data

D.                   Free tmp

E.                    Top --

Step 3:else

A.                   v= -999

B.                   Return v

___________________________________________________________________________________


It is a very important data structure to  Store a particular data format in it by only store an integer or characters or string. Stack also have a particular principle, the principal is last in first out .so let's see the algorithm of stack using array.


For C program : click here

**Algorithm for stack using array**

Data Structure Definition: -

                                         1.          An array stack [MAX], where MAX is the maximum size of the array.

                                         2.          Initialization top = -1

___________________________________________________________________

Algorithm for push operation: -

 

Input:   1. Stack element

              2. Element should be inserted into the top of the stack

Output:  stack after push operation

 

                       

Step 1: Increment top;

                       top=top+1

Step 2: if top=MAX then

              a.   print “Stack overflow”

b.       return

Step 3: else

a.      stack[top]=element

           (End of if condition)

Step 4: Return

______________________________________________________________________

Algorithm for pop operation: -

 

Input:   1.   Stack element

2.      Element should be delete from top of the stack

Output:  stack after pop operation

 

                       

Step 1: if (top = -1) then

a.      Print “Stack Underflow”  

Step 2: else

             a.   popelement = stack[top]

             b.  top = top-1

             c.  return popelement

           (End of if condition)

______________________________________________________________________

Algorithm for display the value: -

 

Input:   None

Output:  stack after traversal

_____________________________________________________________________

Step 1: if (top == -1)

a.      print "Stack is empty”

Step 2: else

a.      print “Stack element:”

b.      i =top

c.      while (i >=0)

1.  print the value of stack arr [i])

                    2.  i =i -1

________________________________________________________________

 

 

 

(End of Algorithm)

Algorithm to implement Doubly Linked list with various operation like create linked list ,travarse,insertion,deletion etc.... 

Doubly linked list image


For C program : Click here


**Algorithm for Doubly linked list**

 

Data structure definition: -

Node:  data {to store data in a node}

             prev {to point to the previous node}

             next {to point to the next node}

_____________________________________________________________________________

 

Algorithm to create a list: -

 

Input:      A, set of data values

                 number of list elements, n

Output:  A Doubly Linked List

 

                             

Step 1: input n

Step 2: head = new node

Step 3: head=NULL

Step 4: end=NULL

Step 5: tempnode=new node

Step 6: Input tempnode->data

Step 7: tempnode->next=NULL

Step 8: for i=1 to n,

a)      if(head=end=NULL) then

                                                                                      1.          tempnode->prev=NULL

                                                                                      2.          head=end=tempnode

b)      else

                                                                                      1.          tempnode->prev=end

                                                                                      2.          end->next=tempnode

                                                                                      3.          end=tempnode

                           (End of if condition)

                    (End of for loop)

Step 9: Return head

_____________________________________________________________________________

 

 

 

 

 

Algorithm for forward traversal of a Doubly linked list: -

 

Input:     A Doubly linked list whose first node is given by pointer head

Output:  Data of each node

 

 

                             

Step 1: list = head

Step 2: while (list->next <> NULL)

a.      Print list -> data

b.      list = list -> next

Step 3: return head

_____________________________________________________________________________

Algorithm for reverse traversal of a Doubly linked list:

 

Input:     A Doubly linked list whose first node is given by pointer head

Output:  Data of each node

 

 

Step 1: list = end

Step 2: while (list->prev <>  NULL)

a.      Print list -> data

b.      list = list -> prev

Step 3: return head

 

Algorithm for insertion of an element at the beginning of a Doubly linked list:

 

Input:    1.    A Doubly linked list whose first node is given by pointer head

           2.    Data to be inserted at the first node

Output:  Linked list after insertion of a new node at the beginning of the list

 

 

Step 1: tempnode = new node {creates a new node}

Step 2: Input tempnode -> data

Step 3: tempnode->prev=NULL

Step 4: tempnode -> next = head

Step 5: head->prev = tempnode

Step 6: head=tempnode

Step 7: return head

 

 

Algorithm for insertion of an element at the end of a Doubly linked list: -

 

Input:    1.  A Doubly linked list whose first node is given by pointer head

           2. Data to be inserted at the end of the linked list

Output:  Linked list after insertion of a new node at the end of the list

 

 

Step 1: tempnode = new node {creates a new node}

Step 2: Input tempnode -> data

Step 3: tempnode -> next = NULL

Step 4: tempnode -> prev=end

Step 5: end->next=tempnode

Step 6: end = tempnode

Step 7: Return head

 

 

Algorithm for insertion of an element after a key node of a Doubly linked list: -

 

Input:    1.  A Doubly linked list whose first node is given by pointer head

            2.   Data to be inserted

            3.   The key value of the node after which, the new node has to be inserted, key

Output:  Linked list after insertion

 

                             

Step 1: Input key

Step 2: tempnode = new node

Step 3: Input tempnode -> data

Step 4: found=0

Step 5: list = head

Step 6: while (found = 0 and list <> NULL)

 

a.      If (list -> data = key), then,

i.                  list1 = list->next

ii.                tempnode -> next = list1

iii.               tempnode -> prev = list

iv.               list -> next = tempnode

v.                list1 -> prev = tempnode

vi.               found = 1

b.      else,

i.                  list = list -> next

(End of if condition)

                                              (End of while loop)

Step 7: if found=0, then

                 print “NODE NOT FOUND”

          (End of if condition)

Step 8: return head

_____________________________________________________________________________

 

Algorithm for insertion of an element before a key node of a Doubly linked list: -

 

Input:    1.   A Doubly linked list whose first node is given by pointer head

             2.   Data to be inserted

             3.   The key value of the node before which, the new node has to be inserted, key

Output:   Linked list after insertion

 

                             

Step 1: Input key

Step 2: tempnode = new node

Step 3: Input tempnode -> data

Step 4: found=0

Step 5: list = head

Step 6: if head -> data = key, then

a.      tempnode -> next = head

b.      tempnode -> prev = NULL

c.       head -> prev = tempnode

d.      head = tempnode

e.      Return head

              (End of if condition)

Step 7:  while (found = 0 AND list -> next <> NULL)

a.      If (list -> data = key), then,

                        1.    listp=list->prev

                        2.    tempnode -> next = list

                        3.    list -> prev= tempnode

                        4.    listp->next=tempnode

                        5.    tempnode->prev=listp

                        6.    found = 1           

b.      else

1.     list = list -> next

                         (End of if condition)

               (End of while loop)

Step 8: if found=0, then                                                 

                  print “NODE NOT FOUND”

           (End of if condition)

Step 9: return head

_____________________________________________________________________________

 

Algorithm for insertion of an element at a particular position of a Doubly linked list: -

 

Input:1.    A Doubly linked list whose first node is given by pointer head

           2.    Data to be inserted

           3.    Keyposition, which is the position in which new node is to be inserted

Output:    Linked list after insertion

NOTE:  Here the first node is considered to be at position =0

 

 

                                                           

Step 1: input keyposition

Step 2: tempnode = new node

Step 3: input tempnode -> data

Step 4: if (keyposition =1), then,

a.      tempnode -> next = head

b.      tempnode -> prev =NULL

c.       head -> prev =tempnode

d.       head = tempnode

a.      Return head

            (End of if condition)

Step 5: validposition = 0

Step 6: currentposition = 0

Step 7: list = head

Step 8: while (vaidpostion = 0 and list <>NULL)

               a.    If (currentposition = keyposition-1), then,

                      1.    currentposition = currentposition + 1

                      2.    list = list -> next

                      (End of if condition)

             (End of while loop)

Step 9:  listp=list->prev

Step 10: tempnode->next=list

Step 11: list->prev=tempnode

Step 12: list p->next=tempnode

Step 13: tempnode->prev=listp

Step 14: Return head

____________________________________________________________________________

 

Algorithm for deletion of first node of a Doubly linked list: -

 

Input:     A Doubly linked list whose first node is given by pointer head

Output:  Linked list after deletion of a first node of the list

 

                             

Step 1: if (head = NULL), then,

              a.    print “EMPTY LIST”

              b.    Return NULL

             (End of if condition)

 Step 2: if (head = end) then

               a.    head = end = NULL

               b.    Return NULL

              (End of if condition)

Step 3: head = head -> next    

Step 4: head -> prev = NULL 

Step 5: Return head          

_____________________________________________________________________________

 

 

 

 

Algorithm for deletion of last node of a Doubly linked list: -

 

Input:     A Doubly linked list whose last node is given by pointer head

Output:  Linked list after deletion of a last node of the list

 

 

Step 1: if (head = NULL), then,

                a.    print “EMPTY LIST”

                b.    Return NULL

              (End of if condition)

Step 2: if (head = end), then,

                a.    head = end = NULL

                b.    Return NULL

              (End of if condition)

Step 3: end = end -> prev

Step 4: end -> next = NULL

Step 5: Return head

 

 

Algorithm for deletion of a node with a particular value of a Doubly linked list: -

 

Input:     A Doubly linked list whose first node is given by pointer head

                The key value of the node which is to be deleted

Output:  The Doubly linked list after deletion

 

                             

Step 1: Input keyvalue

Step 2: found = 0

Step 3: if (head -> data =keyvalue)

a.         head =head -> next

b.         head -> prev = NULL

c.         Return head

             (End of if condition)

Step 4: list = head

Step 5: while (list <>NULL and found =0)

                 a.    If (list -> data =keyvalue)

                        1.    found = 1

                        2.    nodep = list -> prev

                        3.    noden = list -> next

                        4.    nodep -> next = noden

                        5.    noden -> prev = nodep

                  b.   else

                        1.    list = list -> next

                        (End of if condition)

             (End of while loop)

Step 6: if(found=0), then,

                 1.    print “Node not found”

            (End of if condition)

Step 7: Return head

_____________________________________________________________________________

 

Algorithm for deletion of a node with a particular position of a Doubly linked list: -

 

Input:     A Doubly linked list whose first node is given by pointer head

                The key position of the node which is to be deleted

Output: The Doubly linked list after deletion

_____________________________________________________________________________

 

 

Step 1: input pos

Step 2: currrentNode = head

Step 3: i=1

Step 4: while( i<pos  AND  curNode<>=NULL)

                 a.    curNode = curNode->next

                 b.    i=i+1

Step 5: if (pos = 1) then

                 a.    DlListDeleteFirstNode()  //Write the algo of first node deletion

Step 6: else if (curNode = last) then

                a.    DlListDeleteLastNode()  // Write the algo of last node deletion

Step 7: else if (curNode <>= NULL) then

                a.    curNode->prev->next= curNode->next

                b.    curNode->next->prev = curNode->prev

                c.    Remove (curNode)

Step 8: else

                a.    print" Invalid position!...Try again."     

Step 9: Return head

_____________________________________________________________________________