Saturday, June 16, 2012

Implementation of Linked List

Linked list is the special list of data elements linked to one another.Logical ordering is represented by having each elements pointing to next element.Each element in called node which has two fields-info to store data and next to point next node.It can be easily implemented to grow or shrink depending upon operation mode.Entire lined list is accessed from an external  pointer that contains the address of first node but it is not included in the linked list.The next address of last node contains special value called NULL represented by electrical ground symbol.Linked  list with no node is called EMPTY linked list.
In c,we can create  a node using structure as
                 struct node{
                               int info;
                              node *next;
                              }list;

Following operation can be done to linked list
1)Inserting Nodes to the front of  the Linked-List
2)Inserting  Nodes at end of the Linked-List
3)Inserting node after the given Node
4)Inserting node before the given node
5)Deleting the first Node
6)Deleting the last Node
7)Deleting the node after given node
8)Deletion the node before given node
 

1)Inserting Nodes to the front of  the Linked-List

   i)create an empty node
   ii)Insert  data at the info field  of new node
   iii)Set next field of new node
   iv)Now Set external pointer

2)Inserting  Nodes at end of the Linked-List
   i)Declare necessary variables
   ii)Create an empty node and assign it to node pointer p
   iii)Insert the data item to info field of newly created node i.e. P->info=data
   iv)Assign NULL to the next field of newly created node
   v)    IF   list=NULL
                 list=p
           ELSE
       find the last node of the current list (say L)
              L->next=p

3)Inserting node after the given Node
   i)Declare necessary variables
   ii)Take data after which new node is to be inserted
   iii)Find the target node ,say p
           IF the target node is not found
                  Print” Node not Found”
           ELSE
      Create an empty node say r and assign data to  info field
           i.e. r->info=data
           temp=p->next
           p->next=r
           r->next=temp
  iv)Free node temp
  v)For next insertion go to 2
  vi)Stop

   
4)Inserting node before the given node
   i)Declare necessary Variables
   ii)Take the data before which new node is to be inserted
   iii)Find the target node say r
            IF the target node not  found
                   Print “Node not Found !”
            IF  it is not first node(i.e. r=list->next)
               create an empty node p and assign data to info field
               i.e. p->info=data
               p->next=list->next
               list=p
           ELSE
               create an empty node p and assign data to info field
               i.e. p->inof=data
               find the previous node of r and assign in p
               i.e. p->next=r
  iv)For next insertion goto 2
  v)Stop

5)Deleting the first Node
   i)Declare necessary variables
   ii)  IF last=NULL
            print”Epmty list”
        ELSE
               P=list
              List=p->next
  iii)Remove the node p i.e. delete(p)
  iv)For next deletion goto step2
  v)Stop

6)Deleting the last Node
   i)Declare necessary variables
   ii)  IF list=NULL
                print”Empty list”
      ELSE
            Set p=list
           IF p->next=NULL  //for single node
                 List=NULL
                 Free node(p)
           ELSE
               While(p->next!=NULL) //For   more node
                  Temp=p
                   p=p->next
                  Temp->next=NULL
                   Free node(p)
 iii)For next deletion go to step2
 iv)stop

7)Deleting the node after given node
   i)Declare the necessary variables
  ii)Read Data after which node to be deleted
  iii)find the target node say p
          IF p is not found 
              print " target node not found" 
         ELSE IF(p->next=NULL) 
                print" no node after your node to delete"
         ELSE
                    temp=s
                    p->next=s->next
             free node(temp)
   iv)stop

8)Deletion the node before given node
   i)Declare the necessary variables
  ii)Read Data before which node to be deleted
  iii)Find the target node say p
         IF p is not found
                 Print “target node is not found”
         ELSE IF(p=list) 
                print" no node before your node to delete"
         ELSE
                    temp=s
                    s=p
             free node(temp)
iv)stop
                                                                                                        















No comments:

Post a Comment