Wednesday, 25 October 2017

Single Linked List Complete Program in C



Algorithm to insert a node at the beginning of single linked list:
Let head be a pointer that stores the address of first node
step 1: Read the value to inserted into linked list
step 2: create a new node using malloc function
step 3: Assign the value read to the data field of newnode (newnode -> data =value)
step 4: set the next of newnode to head (newnode ->next = head)
step 5: set the head pointer to newnode (head = newnode)




Algorithm to insert a node at the end of single linked list:
Let head be a pointer that stores the address of first node
step 1: Read the value to inserted into linked list
step 2: create a new node using malloc function
step 3: Assign the value read to the data field of newnode
step 4: if there is no linked list then newnode becomes first node
step 5: otherwise go to the lastnode in linked list
                                        Step 5.1: attach newnode to last node (lastnode ->next = newnode)
                                        Step 5.2: newnode->next = NULL
 





Algorithm to insert a node at a given position of single linked list:
Let head be a pointer that stores the address of first node
step 1: Read the value to inserted into linked list and the position at which newnode has to be inserted
step 2: create a new node using malloc function
step 3: Assign the value read to the data field of newnode
step 4: store the node before the given position in a pointer c
step 5: newnode->next = c->next
step 6: c->next = newnode
  


 


Algorithm to delete a node at the beginning of single linked list:
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists
                                        step 2.1: delete the first node
                                        step 2.2: second node becomes first node 


Algorithm to delete a node at the end of single linked list:
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists
                    step 2.1: move to last but one node and store the address of last but one node in pointer c
                    step 2.2: store the address of last node in pointer p and detele the last node (free(p))
                    step 2.3: c->next = NULL



Algorithm to delete a node at given position of single linked list:
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists
                                        step 2.1: Read the position at which the node has to deleted
                                        step 2.2: Store the address of node before the to be deleted node in pointer c
                                        step 2.3: c->next = c->next->next
                             
step 2.4: delete the node at given position






Algorithm to display the elements of single linked list
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists store the address of first node in pointer c ( c = head)
                step 2.1: print the data field in c (print c->data)
                step 2.2 move c to store the address of next node ( c =  c-> next)
step 3: go to step 2 until c is not equal to NULL

Program


#include <stdio.h>
#include <stdlib.h>
struct node
{
               int data;
               struct node *next;
};
struct node *head =NULL,*c,*p;
create()
{
               int value;
               printf("enter the value ");
               scanf("%d",&value);
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
               new->next=NULL;
               if(head == NULL)
               {
                              head=new;
               }
               else
               {
                              c=head;
                              while(c->next != NULL)
                              {
                                             c=c->next;
                              }
                              c->next=new;                   
               }
}

insertbeg(int value)
{
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
              
               new->next=head;
               head = new;
}
insertend(int value)
{
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
               c=head;
               while(c->next != NULL)
               {
                              c=c->next;
               }
              
               c->next=new;
               new->next=NULL;
}
insertmiddle(int value)    
{
               int pos;
               printf("enter the position at which new node has to be inserted");
               scanf("%d",&pos);
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
               c=head;
               int i=1;
               while(i<pos)
               {
                              p=c;
                              c=c->next;
                              i++;
               }
               new->next=c;
               p->next=new;
}

deletebeg()
{
               p=head;
               head=head->next;
               free(p);
}

deletemiddle()
{
    int pos, i=1;
               printf("enter the position of the node that has to be deleted");
               scanf("%d",&pos);
               c=head;
               while(i<pos)
               {
                              p=c;
                              c=c->next;
                              i++;
               }
               p->next=c->next;
               free(c);
}

deleteend()
{
               c=head;
               while(c->next != NULL)
               {
                              p=c;
                              c=c->next;
               }
               free(p->next);
               p->next=NULL;
}
display()
{
               if(head == NULL)
                              printf("list is empty");
               else
               {
                              c=head;
                              while(c->next !=NULL)
                              {
                                             printf("%d -> ",c->data);
                                             c=c->next;
                              }
                              printf("%d\n",c->data);
               }
}

main()
{
               int choice,option,value;
               while(1)
               {
                              printf("Press 1. Create\n 2. Insert \n 3. Delete  \n 4. Display\n 5. Exit \n");
                              printf("enter ur choice");
                              scanf("%d",&choice);
                              switch(choice)
                              {
                                             case 1: create();
                                                                           break;
                                             case 2:
                                               if(head != NULL)            
                                               {
                                                                           option=0;
                                                                           while(option != 4)
                                                                           {
                                                                                                        
                                                                                          printf("Press 1. insert as first node  \n 2. insert as middle node  \n 3. insert as last node \n 4. go to main menu\n");
                                                                                          printf("enter ur choice\n");
                                                                                          scanf("%d",&option);
                                                                                          if(option != 4)
                                                                                          {
                                                                                                         printf("enter the value ");
                                                                                                         scanf("%d",&value);
                                                                                          }
                                                                                          switch(option)
                                                                                          {
                                                                                         
                                                                                                         case 1: insertbeg(value);
                                                                                                                                       display();
                                                                                                                                       break;
                                                                                                         case 2: insertmiddle(value);
                                                                                                                                       display();
                                                                                                                                       break;
                                                                                                         case 3: insertend(value);
                                                                                                                                       display();
                                                                                                                                       break;
                                                                                                         case 4:  break;
                                                                                                         default: printf("invalid choice\n");
                                                                                          }
                                                                           }
                                             }
                                             else
                                                            printf("list is empty");
                                             break;
                                             case 3:
                                                                           option=0;
                                                                           if(head != NULL)
                                                                           {
                                                                                          while(option != 4)
                                                                                          {
                                                                          
                                                                                                         printf("Press 1. delete first node  \n 2. delete middle node  \n 3. Delete last node \n 4. go to main menu\n");
                                                                                                         printf("enter ur choice\n");
                                                                                                         scanf("%d",&option);
                                                                                                         switch(option)
                                                                                                         {
                                                                                         
                                                                                                                        case 1: deletebeg();
                                                                                                                                                      display();
                                                                                                                                                      break;
                                                                                                                        case 2: deletemiddle();
                                                                                                                                                      display();
                                                                                                                                                      break;
                                                                                                                        case 3: deleteend();
                                                                                                                                                      display();
                                                                                                                                                      break;
                                                                                                                        case 4:  break;
                                                                                                                        default: printf("invalid choice\n");
                                                                                                         }
                                                                                          }
                                                                           }
                                                                           else
                                                                                          printf("list is empty and cannot be deleted");
                                                                                          break;
                                             case 4: display();
                                                                           break;
                                             case 5: exit(0);
                                             default: printf("invalid choice");
                              }
                             
               }
}

No comments:

Post a Comment