Saturday, June 29, 2013

Write an algorithm to delete the first node of the singly linked list.



Conditions:
·         ‘SingleNode’ is the node representation of singly linked list, where,
struct my_node{      //structure name ‘my_node’
                   int data;    //data field
                   struct my_node *next;      //pointer to next node
                  };
typedef struct my_node SingleNode;    //defining a node of singly linked list ‘SingleNode’
·         ‘phead’ is a head pointer pointing to the first node
·         ‘temp’ is a pointer to SingleNode
ALGORITHM:
1.       Start
2.       check if the list is empty or not
if phead==NULL
OUTPUT ‘No any node to delete’ and exit
else goto step 3
3.       set the head pointer to the temporary pointer
temp=phead
4.       Increase the position of head pointer by 1
SET phead= phead->next
5.       Display the element of deleted node
OUTPUT temp->data
6.       Free the deleted node from the system
SET free(temp)
7.       Stop

No comments:

Post a Comment