Showing posts with label Data Structure and Algorithm. Show all posts
Showing posts with label Data Structure and Algorithm. Show all posts

Saturday, June 29, 2013

Write an algorithm to count total number of nodes in 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
·         ‘last’ is a pointer to SingleNode
·         ‘count’ is a integer variable initially set to zero
ALGORITHM:
1.       Start
2.       check if the list is empty or not
if phead ==NULL
OUTPUT ‘Zero nodes’ and exit
else goto step 3
3.       Set the head pointer to a temporary variable
last=phead
4.       Traverse till the last node
SET while(last->next!= NULL)
{ increase the node count by 1
SET count++
point to the next node in the list
SET last=last->next
}
5.       Increase the value of count by 1 for last node if the list has more than one node otherwise this condition is applicable if the list has exactly one node.
6.       Display the total count of nodes
OUTPUT count
7.       Stop

Write an algorithm to delete the last 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
·         ‘last’ is a pointer to SingleNode
·         ‘secondlast’ 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 temporary pointer
SET last=phead->next
SET secondlast=phead
4.       Find the last node
SET while(last->next!=NULL)
      { SET last=last->next
        SET secondlast= secondlast->next  }
5.       set the newly last node to NULL
SET secondlast->next= NULL
6.       Display the element of deleted node
OUTPUT last->data
7.       Free the deleted node from the system
SET free(last)
8.       Stop

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

Write an algorithm to insert a new node at the end of a 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
·         ‘last’ is a pointer to SingleNode
ALGORITHM:
1.       Start
2.       create a new node
SET ptr= (SingleNode*) malloc (sizeof(SingleNode))
3.       Add the element ‘item’ in the data field of the node
SET ptr->data= item
4.       Set the pointer field of the new node to NULL
SET ptr->next=NULL
5.        If phead==NULL
SET phead=ptr
else
SET last=phead
find the last node
SET while(last->next!=NULL)
                last=last->next
6.       Add the new node at the end of list
SET last->next=ptr
7.       Stop