Menus

Sep 11, 2015

Program in C to implement insertion and deletion in list.

/*1. Program in C to implement insertion and deletion in list. */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
void append(struct node **q,int item);
void addatbeg(struct node **q,int item);
void addaft(struct node **q);
void remove(struct node **q,int x);
int count (struct node *q);
void display(struct node **q);

struct node
{
int data;
struct node *link;
};
void main ()
{
int ch,item,x;
struct node *p;
p=NULL;
clrscr();
menu:
printf("\n1.Append\n2.Addatbeg\n3.Addafter\n4.Delete\n5.Display\n6.Exit");
printf("\n Select the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the element to be inserted at last end of list:");
scanf("%d",&item);
append(&p,item);
goto menu;
case 2:
printf("Enter the item to be inserted at the begining of the list:");
scanf("%d",&item);
addatbeg(&p,item);
goto menu;
case 3:
addaft(&p);
goto menu;
case 4:
printf("\nDetect which item you want to delete:");
scanf("%d",&x);
remove(&p,x);
goto menu;
case 5:
printf("\n Linked list elements are:\n");
display(&p);
goto menu;
case 6:
exit(0);
default:
printf("\n Out of menu!!\n Reenter the choice:");
goto menu;
}
}
void append(struct node **q,int item)
{
struct node *temp,*r;
temp=*q;
if(*q=='\0')
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=item;
temp->link='\0'  ;
*q=temp;
}
else
{
while(temp->link!='\0')
temp=temp->link;
r=(struct node *)malloc(sizeof(struct node));
r->data=item;
r->link='\0';
temp->link=r;
}
}
void addatbeg(struct node **q,int item)
{
struct node *temp;
/*adds a new node*/
temp=(struct node *)malloc(sizeof(struct node));
temp->data=item;
temp->link=*q;
*q=temp;
}
void addaft (struct node **q)
{
struct node *temp,*r;
int i,loc,item,n;
n=count(*q);
printf("\n The number of node=%d",n);
printf("\n Specify th\e node number after which you want to insert:");
scanf("%d",&loc);
printf("\n Input the element for insertion:");
scanf("%d",&item);
temp=*q;
/*if your choice is greater thannumber of node in the list*/
if(loc>n)
{
printf ("\n Sorry!! there is less than %d elements in the list",loc);
return;
}
/*if you want to insert at begining of list*/
if(loc==0)
{
r=(struct node *)malloc(sizeof(struct node));
r->data=item;
r->link=*q;

*q=r;
}
else
{
/*Skip to desired portiopn*/
for(i=1;i<loc;i++)
temp=temp->link;
/*insert new node*/
r=(struct node *)malloc(sizeof(struct node));
r->data=item;
r->link=temp->link;
temp->link=r;
}
}
void remove(struct node **q,int x)
{
struct node *old,*temp;
temp=*q;
while(temp!='\0')
{
if(temp->data==x)
{
if(temp==*q)
{
*q=temp->link;
free(temp);
}
else
{
old->link=temp->link;
free(temp);
}
printf("\n Deleting Success!!!!");
return;
}
else
{
old=temp;
temp=temp->link;
}
}
printf("\n The number is not Found,(i.e it is not in the linked list)");
}
void display(struct node **q)
{
struct node *temp;
temp=*q;
if(temp=='\0')
printf("List is empty");
while(temp!='\0')
{
printf("%4d",temp->data);
temp=temp->link;
}
}
int count (struct node *q)
{
int c=0;
struct node *temp;
temp=q;
while(temp!='\0')
{
temp=temp->link;
c++;
}
return c;
}


No comments:

Post a Comment

Contact Form

Name

Email *

Message *