Showing posts with label Program : Singly linked list – Deletion. Show all posts
Showing posts with label Program : Singly linked list – Deletion. Show all posts

Monday 2 April 2012

Program : Singly linked list – Deletion


Program : Singly linked list – Deletion

// Deletion in singly linked list (Delete First, Last and node at desire position)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct link
{
          int no;
          struct link *next;
};

struct link *node,*start,*pre,*new1;
int i=0;
void create_link(struct link *node);
void display(struct link *node);
void delete_first(struct link *node);
void delete_last(struct link *node);
void delete_desireposition(struct link *node);

void main()
{
          int ch;
          clrscr();
      do
      {
          printf("\n Singly Linked List");
          printf("\n---------------------------");
          printf("\n 1. Create Link");
          printf("\n 2. Traverse Link");
          printf("\n 3. Delete First node");
          printf("\n 4. Delete Last node");
          printf("\n 5. Delete node at DesirePosition");
          printf("\n 6. Exit");
          printf("\n-----------------------------");

          printf("\n Enter your choice:-");
          scanf("%d",&ch);
                   switch(ch)
                   {
                             case 1:
                                node=(struct link *)malloc(sizeof(struct link));
                                create_link(node);
                                break;
                             case 2:
                                printf("\n Output\n");
                                display(node);
                                break;
                             case 3:
                                delete_first(node);
                                printf("\n After Deleting First Node");
                                display(node);
                                break;
                             case 4:
                                delete_last(node);
                                printf("\n After Deleting Last Node");
                                display(node);
                                break;
                             case 5:
                                delete_desireposition(node);
                                printf("\n After Deleting At Desire Position");
                                display(node);
                                break;

                             case 6:
                                exit(0);
                                break;
                             default:
                                printf("\n Wrong Choice");
                   }
          }while(ch!=6);
          getch();

}

void create_link(struct link *node)
{
          char ans;

          start->next=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);
                   node->next=NULL;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
}

void display(struct link *node)
{
          node=start->next;
          while(node)
          {
                   printf("\n  %d",node->no);
                   node=node->next;
          }
}


void delete_first(struct link *node)
{
          node=start->next;
          pre=start;
          if(node==NULL)
          {
                   printf("\n List is empty");
                   exit(0);
          }
          pre->next=node->next;
          free(node);
}

void delete_last(struct link *node)
{
          int node_no=0;
          node=start->next;
          pre=start;
          if(node==NULL)
          {
                   printf("\n List is empty");
                   exit(0);
          }
          while(node)
          {
                   node_no++;
                   pre=pre->next;
                   node=node->next;
          }
          node=start->next;
          pre=start;
          while(node_no!=1)
          {
                   node_no--;
                   pre=pre->next;
                   node=node->next;
          }
          if(node_no==1)
          {
                   pre->next=node->next;
                   free(node);
          }
}

void delete_desireposition(struct link *node)
{
          int node_no=1,delete_no,flag=0;
          node=start->next;
          pre=start;
          printf("\n Enter position where you want to delete node:-");
          scanf("%d",&delete_no);

          while(node)
          {
                   if(node_no==delete_no)
                   {
                             pre->next=node->next;
                             free(node);
                             flag=1;
                             break;
                   }
                   else
                   {
                             pre=pre->next;
                             node=node->next;
                   }
                   node_no++;
          }
          if(flag==0)
          {
                   printf("\n Position not found");
          }
}

Posted By : Ruchita Pandya