LINKED LIST OPERTION IN C++

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//PROGRAM TO PERFORM LINKED LIST OPERTION.

#include<stdio.h>
#include<iostream.h>
#include<alloc.h>
#include<malloc.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#define NewNode (Node *) malloc (sizeof(Node))

typedef struct node
{
int item;
struct node *next;
}Node;
Node *create(Node *);
void display(Node *);
int count(Node *);
Node *insert(Node *);
Node *del(Node *);
void search(Node *);
void main()
{
clrscr();
Node *ptr=NULL,*start=NULL;
int ch,cnt;
print(“to stop entering data enter -99n”);
display(start);
do
{
printf(“n 1.Countn”);
printf(“n 2.Displayn”);
printf(“n 3.Insertn”);
printf(“n 4.Deleten”);
printf(“n 5.Searchn”);
printf(“n 6.Exitn”);
printf(“n.Enter your choicen”);
scanf(“%d”,&ch)
switch(ch)
{
case 1: print(“no of nodes=%d”,count(start));
break;
case 2: display(start);
break;
case 3: start=insert(start)
break;
case 4: start=del(start);
break;
case 5: search(start);
break;
case 6: exit(0);
default:printf(“invalid optionn”);
break;
}
}
while(ch!=6);
}
Node*create(node*s)
{
Node*tmp=NuLL,*t1=NuLL;
int num;
t1=s;
printf(“enter the elementn”);
do
{
scanf(“%d”,&num);
if(num!=-99)
{
tmp=NewNode;

tmp->item=num;
tmp->next=NuLL;
if(s=NuLL)
s=t1=tmp;
else
{
t1->next=tmp;
t1=t1->next;
}
}
else
printf(“link list created created sucessfullyn”);
}
while(num!=-99);
return(s);
}
void display(Node*s)
{
if(s==Null)
printf(“empty linklistn”)
else
{
while(s!=NuLL)
{
printf(“%5d”,s->item);
s=s->next;
}
}
}
int count(Node*s)
{
int total=0;
while(s!=Null)
{
total++;
s=s->next;
}
return(total);
}
Node*insert(Node*s)
{
Node*insert(Node*s)
int pos;
printf(“enterpostion to be insertedn”);
scanf(“%d”, &pos);
if(pos>0&&pos<=count(s)+1)
{
printf(“enter element to be insertedn”);
scanf(“%d”,&tmp->item);
if(pos==1)
{
tmp->next=s;
s=tmp;
}
else
{
t1=s;
while(pos>2)
{
t1=t1->next;
pos–;
}
tmp->next=t1->next;
t1->next=tmp;
}
}
Node*del(Node*s)
{
Node *t1=s,*tmp=Null;
int pos;
printf(“enter postion to be deletedn”)
if(pos>0&&pos<=count(s))
{
if(pos==1)
{
s=s->next;
free(t1);
}
else
{
while(pos>2)
{
t1=t1->next;
pos–;
}
tmp=t1->next;
t1->next=tmp->next;
free(tmp);
}
}
else
printf(“invalid optionn”);
return(s);
}
void search(Node *s)
{
int ele;
int flag=0,c=0;
print(“enter the element to be searchedn”);
scanf(“%d”,ele);
if(s!=NuLL)
{
while(s!=NuLL)
{
c++;
if(s->item==ele)
{
printf(“n %d is present at postion %d n,ele,c);
}
}
s=s->next;
}
if(flag==0)
printf(” n element not foundn”);
}
else
printf(“n list is empty,key element cannot foundn”);
}

=========================output============================

enter the element=>1 2 3 4 5 -99
link list created sucessfully=> 1 2 3 4 5
1.count
2.display
3.insert
4.delete
5.search
6.exit
enter your choice=>1
no of node=5
1.count
2.display
3.insert
4.delete
5.search
6.exit
enter your choice=>2
1 2 3 4 5
1.count
2.display
3.insert
4.delete
5.search
6.exit
enter your choice=>3
enter postion to be inserted=>2

enter element to be inserted=>7

1.count
2.display
3.insert
4.delete
5.search
6.exit

enter your choice=>2
1 7 2 3 4 5
1.count
2.display
3.insert
4.delete
5.search
6.exit
enter your choice=>4
enter postion to be deleted=>3

1.count
2.display
3.insert
4.delete
5.search
6.exit
enter your choice=>2
1 7 3 4 5

1.count
2.display
3.insert
4.delete
5.search
6.exit
enter your choice=>5
enter the element to be searched=>4
4 is present at postion 4

1.count
2.display
3.insert
4.delete
5.search
6.exit
enter your choice=>2
1 7 3 4 5

1.count
2.display
3.insert
4.delete
5.search
6.exit

enter your choice=>6

Leave a Reply

Your email address will not be published. Required fields are marked *