Google Search

Saturday, January 29, 2011

Implement the basic operations on Singly Linked List MC 9217 DATA STRUCTURES LAB Anna University lab manual download

Implement the basic operations on Singly Linked List MC 9217 DATA STRUCTURES LAB Anna University lab manual download


Aim: To write a program to implement the basic operations on Singly Linked List

Algorithm:

  1. Start
  2. Read the value of ch
  3. If ch =1 then read item of number
      set n as malloc to size, set n of into as num and next as list
  1. If ch=2 to check if list =0 then  print “Empty list”
      else      if n is list then delete the node n        
  1. If ch=3 then read position value  and set n as malloc size and read num value and store in n.
  2. If ch =4 then check list as NULL or not. If NULL then print “ Empty list”
      Else check I not equal to pos then print info of p then delete the node p
  1. If ch=5 then set n and allocate malloc size and read num.
      Else next not as Null then n as next of p
  1. If ch=6 then check pos Null and print “Empty list”
      Else    delete the node p
  1. stop

Logical Description       

The singly-linked list is the most basic of all the linked data structures. A singly-linked list is simply a sequence of dynamically allocated objects, each of which refers to its successor in the list. Despite this obvious simplicity, there are myriad implementation variations.

Program Code
struct node
   {
   int data;
   struct node *link;
   }*head;
typedef struct node node;
void maintain(void);
void create(node *);
void insertion(node *);
void deletion(node *);
void display(node *);
int count=0;
#include"stdio.h"
#include"alloc.h"
main()
{
head=malloc(sizeof(node));
create(head);
maintain();
getch();
return;
}
void maintain()
{
int n;
clrscr();
printf("   MAINTANENCE\n\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\n\nYour choice: ");
scanf("%d",&n);
switch(n)
   {
   case 1:
      {
      insertion(head);
      break;
      }
   case 2:
      {
      deletion(head);
      break;
      }
   case 3:
      {
      display(head);
      break;
      }

   case 4:
      {
      exit();
      }
   }
}

void create(node *p)
{
   int i;
   node *q;
   clrscr();
   printf("\nCreation of Linked List\n");
   printf("\nEnter the data('0' to terminate)\n");
   scanf("%d",&i);
   p->data=i; count++;
   scanf("%d",&i);
   while(i!=0)
   {
      q=malloc(sizeof(node));
      q->data=i; count++;
      p->link=q;
      scanf("%d",&i);
      if(i!=0)
     {
         p=malloc(sizeof(node));
         p->data=i; count++;
         q->link=p;
      }
   else
   {
          q->link=NULL;
           return;
    }
    scanf("%d",&i);
    }
   p->link=NULL;
   maintain();
  }
void insertion(node *p)
   {
   int n,m,i=1;
   node *new,*next,*tail;
   clrscr();
   while(i==1)
  {
      clrscr();
      p=head;
      printf("\tINSERTION\n\n1. Insertion at first\n2. Insertion at last\n3. Insertion             
                     inbetween\n\nYour choice: ");
      scanf("%d",&n);

      printf("\nEnter the data: ");
      scanf("%d",&i);
      new=malloc(sizeof(node));
      new->data=i;
      switch(n)
      {
         case 1:
            {
            new->link=head;
            head=new;
            count++; break;
            }
         case 2:
            {
            while(p->link!=NULL)
               p=p->link;
            p->link=new;
            new->link=NULL;
            count++; break;
            }
         case 3:
            {
            printf("\nEnter the location: ");
            scanf("%d",&n);
            if(n<2 || n>=count)
               {
               clrscr();
               printf("Sorry!!! Location doesn't fall within the limit");
               break;
               }
m=1;
            while(m<n-1)
               {
               p=p->link;
               m++;
               }
  next=p->link;
           p->link=new;
           new->link=next;
           count++; break;
           }
        }
      printf("\n\nDo you want to continue insertion(1/0): ");
      scanf("%d",&i);
      }
   maintain();
   }
void deletion(node *p)
   {
   int n,m,i=1;
   node *next,*tail;
   clrscr();
   while(i==1)
      {
      clrscr();
      p=head;
      printf("\tDELETION\n\n1. Deletion at first\n2. Deletion at last\n3. Deletion
                   inbetween\n\nYour choice: ");
      scanf("%d",&n);
      switch(n)
     {
         case 1:
            {
            head=head->link; 
            count--; break;
            }
         case 2:
            {
            while((p->link)->link!=NULL)
               p=p->link;
            p->link=NULL; 
            count--; break;
             }
         case 3:
        {
            printf("\nEnter the location: ");
            scanf("%d",&n);
            if(n<2 || n>=count)
               {
               clrscr();
               printf("Sorry!!! Location doesn't fall within the limit");
               break;
               }
            m=1;
           while(m<n-1)
           {
               p=p->link;
               m++;
            }
            next=p->link;
            p->link=next->link; 
            count--; break;
            }
      }
      printf("\n\nDo you want to continue deletion(1/0): ");
      scanf("%d",&i);
      }
   maintain();
   }
void display(node *p)
   {
   clrscr();
   printf("\nCreated List");
   printf("\n\nTotal no of elements %d\n",count);
   while(p!=NULL)
  {
      printf("%d ",p->data);
      p=p->link;
  }
   getch();
   maintain();
   }




0 comments:

Post a Comment