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

# Computer Programming

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

## Saturday, January 29, 2011

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;
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()
{
maintain();
getch();
return;
}
void maintain()
{
int n;
clrscr();
printf("   MAINTANENCE\n\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
scanf("%d",&n);
switch(n)
{
case 1:
{
break;
}
case 2:
{
break;
}
case 3:
{
break;
}

case 4:
{
exit();
}
}
}

void create(node *p)
{
int i;
node *q;
clrscr();
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++;
scanf("%d",&i);
if(i!=0)
{
p=malloc(sizeof(node));
p->data=i; count++;
}
else
{
return;
}
scanf("%d",&i);
}
maintain();
}
void insertion(node *p)
{
int n,m,i=1;
node *new,*next,*tail;
clrscr();
while(i==1)
{
clrscr();
printf("\tINSERTION\n\n1. Insertion at first\n2. Insertion at last\n3. Insertion
scanf("%d",&n);

printf("\nEnter the data: ");
scanf("%d",&i);
new=malloc(sizeof(node));
new->data=i;
switch(n)
{
case 1:
{
count++; break;
}
case 2:
{
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)
{
m++;
}
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();
printf("\tDELETION\n\n1. Deletion at first\n2. Deletion at last\n3. Deletion
scanf("%d",&n);
switch(n)
{
case 1:
{
count--; break;
}
case 2:
{
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)
{
m++;
}
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);