/*Program to impelement Singly Linked List operations
compiler- Turbo c++ author- Mangilal Sharma
--------------------------------------------------------*/
#include <process.h>
#include <conio.h>
#include <iostream.h>
class singly
{
struct s_node
{
int data;
s_node *link;
}*p;
public:
void inslast(int);
void insbeg(int);
void insnext(int,int);
void delelement(int);
void delbeg();
void dellast();
void disp();
int search(int);
singly();
~singly(); //destructor that deallocate the memory adderess
};
void singly::inslast(int x)
{
s_node *q,*t;
if(p==NULL)
{
p=new s_node;
p->data=x;
p->link=NULL;
}
else
{
q=p;
while(q->link!=NULL)
q=q->link;
t=new s_node;
t->data=x;
t->link=NULL;
q->link=t;
}
cout<<" Inserted successfully at the end...\n";
disp();
}
void singly:: insbeg(int x)
{
s_node *q;
q=p;
p=new s_node;
p->data=x;
p->link=q;
cout<<" Inserted successfully at the begining...\n";
disp();
}
void singly::delelement(int x)
{
s_node *q,*r;
q=p;
if(q->data==x)
{
p=q->link;
delete q;
return;
}
r=q;
while(q!=NULL)
{
if(q->data==x)
{
r->link=q->link;
delete q;
return;
}
r=q;
q=q->link;
}
cout<<" Element u entered "<<x<<" that is not found..";
}
void singly:: delbeg()
{
cout<<" The list before deletion:";
disp();
s_node *q;
q=p;
if(q==NULL)
{
cout<<" No data is present..";
return;
}
p=q->link;
delete q;
return;
}
void singly:: dellast()
{
cout<<" The list before deletion:";
disp();
s_node *q,*t;
q=p;
if(q==NULL)
{
cout<<" There is no data in the list...";
return;
}
if(q->link==NULL)
{
p=q->link;
delete q;
return;
}
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
return;
}
singly::~singly()
{
s_node *q;
if(p==NULL) return;
while(p!=NULL)
{
q=p->link;
delete p;
p=q;
}
}
void singly::disp()
{
s_node *q;
q=p;
if(q==NULL)
{
cout<<" No data is in the list..";
return;
}
cout<<" The items present in the list are :";
while(q!=NULL)
{
cout<<" "<<q->data;
q=q->link;
}
}
void singly::insnext(int value,int position)
{
s_node *temp,*temp1;
temp=p;
if(temp1==NULL)
{
temp1= new s_node;
temp1->data=value;
temp1->link=NULL;
p=temp1;
return;
}
for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)
{
if(i==(position-1))
{
temp1= new s_node;
temp1->data= value;
temp1->link=temp->link;
temp->link=temp1;
}
temp=temp->link;
}
cout<<" Inserted successfully at the position...\n"<<position;
disp();
}
int singly::search(int value)
{
s_node *temp;
temp=p;
int position=0;
while(temp!=NULL)
{
if(temp->data==value)
return position+1;
else
{
temp=temp->link;
position=position+1;
}
}
cout<<" Element "<<value<<" not found";
return 0;
}
singly::singly()
{
int v,p,ps,choice;
p=NULL;
while(1)
{
cout<<"\n\t\tOperations on linked List..\n";
cout<<"\t\t1. To Insert an Element in linked list\n";
cout<<"\t\t2. To Delete an Element from linked list\n";
cout<<"\t\t3. To Display the Elements of linked list\n";
cout<<"\t\t4. To Search an element\n";
cout<<"\t\t5. To Main menu\n";
cout<<"\t\t6. To Exit from Programme\n";
cout<<"\n\t\t Enter Your Choice= ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"1.Insertion at begining\n2.Insertion at the end\n";
cout<<"3.Insertion after the mentioned position\n";
cout<<" Enter ur choice:";
cin>>ps;
cout<<" Enter the value to insert:";
cin>>v;
switch(ps)
{
case 1:insbeg(v);break;
case 2:inslast(v);break;
case 3:cout<<" Enter the position to insert the value:";
cin>>p;
insnext(v,p);break;
default:cout<<" The choice is invalid";return;
}break;
case 2:
cout<<"1.Delete the first element\n2.Delete the last element\n";
cout<<"3.Enter the element to delete from the list\n";
cout<<" Enter ur choice:";
cin>>ps;
switch(ps)
{
case 1:delbeg();
cout<<" The list after deletion:";disp();break;
case 2:dellast();
cout<<" The list after deletion:";disp();break;
case 3:disp();
cout<<" Enter the element to delete:";
cin>>v;
delelement(v);
cout<<" The list after deletion:";disp();break;
default:
cout<<" The option is invalid...";break;
}break;
case 3:
disp();break;
case 4:
disp();
cout<<" Enter the element to search:";
cin>>v;
cout<<" The position of the element "<< v<<" is "<<search(v);
getch();break;
case 5:return;
case 6:exit(1);
default:cout<<" The option is invalid...";return;
}
}
}
/*=========================9Nov.,2010=============================*/
No comments:
Post a Comment
You are welcome, your comment helps me to make this blog more better.