Hi friends, I think all of you are enjoying my posts on this blog.
Today i am here with DSPM programs related to Linked Lists .
I am attaching a file related to linked list and all the operations performed on it.............
//To create
and traverse a linked list
#include<iostream.h>
#include<conio.h>
class node
{ public:
int data;
node *next;
};
void main()
{
node *start,*p;
char ch='y';
int j=0;clrscr();
start='\0';
do
{
if (start=='\0')
{
start= new node;
p=start;
}
else
{
p->next = new node;
p=p->next;
}
cout<<"\n
Enter data ";
cin>>p->data;j++;
cout<<"\n Enter your choice (y/n)? ";
cin>>ch;
}
while(ch=='y'
|| ch=='Y');
p->next='\0';cout<<"\n--------------------------------";
cout<<"\n
Data for nodes ";
p=start;
while(p!=0)
{
cout<<"\n"<<p->data;
p=p->next;
}
cout<<"\n
Number of nodes = "<<j;
getch();
}
//To insert
an element in the beginning of linked list
#include<iostream.h>
#include<conio.h>
class node
{
public:
int
data ;
node
*next;
};
void main()
{
node *start,*p,*beg;
char ch;
int j=0;
clrscr();
start='\0';
do
{
if(start=='\0')
{
start = new node;
p= start;
}
else
{
p->next=new node;
p=p->next;
}
cout<<"\nEnter
the elements\t";
cin>>p->data;
j++;
cout<<"\nEnter your choice(y/n)?\t";
cin>>ch;
}while(ch=='y'||ch=='Y');
p->next='\0';
cout<<"\nElements for nodes are";
p=start;
while(p!=0)
{
cout<<"\t"<<endl<<p->data;
p=p->next;
}
cout<<endl;
cout<<"\nNumber of nodes are "<<j;
getch();
cout<<"\n-----------------INSERTION AT THE
BEGINNING---------";
beg=new node;
cout<<"\nEnter Element for new node ";
cin>>beg->data;
beg->next=start;
start=beg;
cout<<"\nElement for nodes is";
p=start;
j=0;
while(p!=0)
{
cout<<"\t"<<endl<<p->data;
j++;
p=p->next;
}
cout<<endl;
cout<<"\nNumber of nodes = "<<j;
getch();
}
//Insertion
at any location in linked list
#include<iostream.h>
#include<conio.h>
class node
{
public:
char data;
node *next;
};
void main()
{
node
*start, *p, *current;
char
ch;
int
j=0;
clrscr();
start='\0';
do
{
if(start=='\0')
{
start=new
node;
p=start;
}
else
{
p->next=new
node;
p=p->next;
}
cout<<"\nEnter data ";
cin>>p->data;
j++;
cout<<"\nEnter choice y/n?";
cin>>ch;
}
while(ch=='Y'||ch=='y');
p->next='\0';
cout<<"\nData for nodes ";
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
}
cout<<endl<<"No. of nodes "<<j<<endl;
cout<<"\n--------------Insertion of element at
any location---------------------";
int loc;
int count=1;
cout<<"\nEnter location where you want to enter
node ";
cin>>loc;
if(loc<=j)
{ current=new
node;
cout<<"\nEnter
data : ";
cin>>current->data;
p=start;
while(p!='\0' && count<loc-1)
{
p=p->next;
count++;
}
current->next=p->next;
p->next=current;
cout<<"Data in list is :"<<endl;
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
} }
else
{ cout<<"\nEnter valid loction!!"; }
getch(); }
//To delete
a node from the beginning of the linked list
#include<iostream.h>
#include<conio.h>
class node
{
public:
char data;
node *next;
};
void main()
{
node
*start, *p;
char
ch;
int
j=0;
clrscr();
start='\0';
do
{
if(start=='\0')
{
start=new
node;
p=start;
}
else
{
p->next=new
node;
p=p->next;
}
cout<<"\nEnter data ";
cin>>p->data;
j++;
cout<<"\nEnter choice (y/n)?";
cin>>ch;
}while(ch=='Y'||ch=='y');
p->next='\0';
cout<<"\nData for nodes ";
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
}
cout<<endl<<"No. of nodes "<<j;
if(start==0)
{
cout<<"There
is no node in the list ";
}
else
{
p=start;
start=p->next;
delete
p;
cout<<"\nData
deleted from beginning ";
}
cout<<"\nData for nodes after deletion ";
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
}
getch();
}
//To delete
a node from a given location
#include<iostream.h>
#include<conio.h>
struct node
{ int data;
node *next;
}*p,*start,*save,*loc,*locp;
int findloc(int);
int deleteitem();
void main()
{ char ch;
int
j=0;
clrscr();
start='\0';
do
{
if(start=='\0')
{
start=new node;
p=start;
}
else
{
p->next=new node;
p=p->next;
}
cout<<"\nEnter data ";
cin>>p->data;
j++;
cout<<"\nEnter choice (y/n)?";
cin>>ch;
}while(ch=='Y'||ch=='y');
p->next='\0';
cout<<"\nData for nodes ";
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
}
cout<<endl<<"No. of nodes"<<j;
cout<<"\nEnter the item to be deleted ";
int i;
cin>>i;
findloc(i);
deleteitem();
cout<<"\nData for nodes after deletion ";
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
}
getch();
}
int findloc(int d)
{ if (start==0)
{
loc=0;
locp=0;
return(0);
}
if (start->data==d)
{
loc=start;
locp=0;
return(0);
}
save=start;
p=start->next;
while (p!=0)
{ if
(p->data==d)
{loc=p;
locp=save;
return(0);
}
save=p;
p=p->next; }
loc=0;
return(0);
}
int deleteitem()
{ if(loc==0)
{cout<<"\nNode not found ";
return(0); }
else if(locp==0)
{ start=start->next; }
else
{ locp->next=loc->next; }
delete loc;
}
//To search
an element in the linked list
#include<iostream.h>
#include<conio.h>
#include<process.h>
class node
{
public:
int
data ;
node
*next;
};
void main()
{ node *start,*p,*beg,*end,*loc;
char ch;
int j=0,item;
clrscr();
start='\0';
do
{
if(start=='\0')
{ start
= new node;
p= start;
}
else
{
p->next=new node;
p=p->next;
}
cout<<"\nEnter the Element\t";
cin>>p->data;
j++;
cout<<"\nTo continue enter your choice(y/n)?
";
cin>>ch;
}while(ch=='y'||ch=='Y');
p->next='\0';
cout<<"\nElement for nodes is";
p=start;
while(p!=0)
{ cout<<"\t"<<p->data;
p=p->next;
}
cout<<"\nNumber of nodes is "<<j;
cout<<"\nEnter the element to searched: ";
cin>>item;
p=start;
while(p!=0)
{ if (item==p->data)
{ loc=p;
cout<<"\nSearch Successful ----------Element
found at location: "<<loc;
getch();
exit(0);
}
else
{ p=p->next; }
}
loc='\0';
cout<<"\nSearch Unsuccessful";
getch();
}
thanks a lot sir......upload more such materials which are useful for we students...
ReplyDelete