Recdsa
Recdsa
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class list
{
struct node
{
int data;
node *link;
}*p;
public:
void inslast(int);
void insbeg(int);
void create(int);
void insnext(int,int);
void delelement(int);
void delbeg();
void dellast();
void count();
void disp();
void sort();
int seek(int);
list(){p=NULL;}
~list();
};
void list::create(int n)
{
struct node *q,*tmp;
tmp=(struct node*)malloc(sizeof(struct node*));
tmp->data=n;
tmp->link=NULL;
if(p==NULL)
p=tmp;
else
{
q=p;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
//sort.............
void list :: sort()
{ node *q,*r;
int coun=0,i,j,t;
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;
}
cout<<"\n\ncount of list is"<<coun;
r=p->link;
for(i=0;i<=coun;i++)
{
q=p->link;
{
for(j=1;j<=coun;j++)
{
if(p->data > q->data)
{
t=p->data;
p->data=q->data;
q->data=t;
}
q=q->link;
}
r=r->link;
}
}
disp();
}
void list::inslast(int x)
{
node *q,*t;
if(p==NULL)
{
p=new node;
p->data=x;
p->link=NULL;
}
else
{
q=p;
while(q->link!=NULL)
q=q->link;
t=new node;
t->data=x;
t->link=NULL;
q->link=t;
}
cout<<" Inserted successfully at the end.. ";
disp();
}
void list::delelement(int x)
{
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<<" is not found..";
}
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
return;
}
list::~list()
{
node *q;
if(p==NULL) return;
while(p!=NULL)
{
q=p->link;
delete p;
p=q;
}
}
void list::disp()
{
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 list::count()
{
node *q;
int coun=0;
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;
coun++;
}
cout<<"\n\ncount of list is"<<coun;
}
switch(ch)
{
case 1:
cout<<"enter the number of elements to be inserted";
cin>>m;
for(i=1;i<=m;i++)
{
cout<<"enter the element "<<i<<" ";
cin>>n;
l.create(n);
}
break;
case 2:
cout<<" \n\n1.Insertion at begining2.Insertion at th end ";
cout<<"\n\n3.Insertion after the mentioned position ";
cout<<"\n\n Enter ur choice:";
cin>>ps;
cout<<" \n\nEnter the value to insert:";
cin>>v;
switch(ps)
{
case 1:
l.insbeg(v);
break;
case 2:
l.inslast(v);
break;
case 3:
cout<<" \n\n Enter the position to insert the value:";
cin>>p;
l.insnext(v,p);
break;
default:
cout<<" \n\nThe choice is invalid ";
return;
}
break;
case 3:
cout<<"\n\n1.Delete the first element2.Delete the last element";
cout<<"\n\n 3.Enter the element to delete from the list";
cout<<"\n\n Enter ur choice:";
cin>>ps;
switch(ps)
{
case 1:
l.delbeg();
cout<<" \n\nThe list after deletion:";l.disp();
break;
case 2:
l.dellast();
cout<<" \n\nThe list after deletion: ";l.disp();
break;
case 3:
l.disp();
cout<<" \n\n Enter the element to delete : ";
cin>>v;
l.delelement(v);
cout<<" \n\n The list after deletion:";l.disp();
break;
default:
cout<<"\n\n The option is invalid...";
break;
}
break;
case 4:
l.disp();
break;
case 5:
l.disp();
cout<<"\n\n Enter the element to search:";
cin>>v;
cout<<"\n\n The position of the element "<< v<<" is "<<l.seek(v);
getch();
break;
case 6:
l.count();
break;
case 7:
l.sort();
break;
case 8:
exit(10);
break;
default:
cout<<" \n\n The option is invalid...";
return;
}
getch();
}while(ch!=5);
getch();
return;
}
OUTPUT:
1.creation
2.Insertion
3.Deletion
4.Display
5.See
k6.count
7.sorting
8.exit
Enter ur choice:1
enter the number of elements to be inserted3
enter the element 1: 22
enter the element 2 :44
enter the element 3 : 66
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at beginning
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:1
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at beginning
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:2
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at beginning
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:3
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:3
Enter ur choice:1
The list before deletion: The items present in the list are : 12
22 66 56 77 22 44 66 34
The list after deletion: The items present in the list are : 22 66
56 77 22 44 66 34
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:3
Enter ur choice:2
The list before deletion: The items present in the list are : 22
66 56 77 22 44 66 34
The list after deletion: The items present in the list are : 22 66
56 77 22 44 66
1.creation
2.Insertion
3.Deletion
4.Display
5.See
k6.count
7.sorting
8.exit
Enter ur choice:3
1.Delete the first element
2.Delete the last element
3.Enter the element to delete from the list
Enter ur choice:3
The items present in the list are :
22 66 56 77 22 44 66
The list after deletion: The items present in the list are :
22 56 77 22 44 66
1.creation
2.Insertion
3.Deletion
4.Display
5.See
k6.count
7.sorting
8.exit
Enter ur choice:4
The items present in the list are : 22 56 77 22 44
66
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:5
The items present in the list are : 22 56 77 22 44 66
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:6
The items present in the list are : 22 56 77 22 44
66
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:7
The items present in the list are : 22 56 77 22 44
66
The sorted elements are:22 22 44 56 66 77
PROGRAM CODING:
#include<iostream.h>
#include<conio.h>
#define top 5
int i=-1;
class stack
{
public:
int a[top],cnt;
public:
void push()
{
if(i==top-1)
cout<<"\n\tTHE STACK IS FULL";
else
{
i++;
cout<<"\nEnter the data to be pushed :";
cin>>a[i];
cnt++;
}
}
void disp()
{
cout<<"\nThe elements are:";
for(int j=0;j<cnt;j++)
{
cout<<"\t"<<a[j];
}
}
void pop()
{
if(i==-1)
cout<<"\n\tTHE STACK IS EMPTY";
else
{
cout<<"\nThe poped out data :"<<a[i];
cnt--;
i--;
}
getch();
}
};
void main()
{
int ch;
stack s;
clrscr();
while(1)
{
cout<<"\n\n\t\tSTACK";
cout<<"\nMenu";
cout<<"\n\n1.Push\n2.Pop\n3.Display\n4.Exit";
cout<<"\nEnter the choice :";
cin>>ch;
if(ch==1)
s.push();
else if(ch==2)
s.pop();
else if(ch==3)
s.disp();
else if(ch==4)
break;
}
}
OUTPUT:
STACK OPERATION
MENU
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice :1
STACK OPERATION
MENU
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice :
1
STACK OPERATION
MENU
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice :1
STACK OPERATION
MENU
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice :2
The poped out data :5
STACK OPERATION
MENU
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice :2
STACK OPERATION
MENU
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice :3
STACK OPERATION
MENU
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the choice : 4
PROGRAM CODING:
#include<iostream.h>
#include<conio.h>
class linklist
{
private:
struct node
{
int data;
node *link;
}*f,*r;
public:
linklist()
{ f=r=NULL; }
void add(int item);
void display();
int del();
~linklist();
};
void linklist::add(int item)
{
node *q;
q=new node;
q->data = item;
if(f==NULL)
f=q;
else
r->link=q;
r=q;
r->link=f;
}
int linklist::del()
{
node *q;
int item;
if(f==NULL)
cout<<"\nqueue is empty ";
else
{
if(f==r)
{
item = f->data;
cout<<"\ndeleted element "<<item;
delete f;
f=NULL;r=NULL;
display();
}
else
{
q=f;
item=q->data;
f=f->link;
r->link=f;
cout<<"\ndeleted element " <<item;
delete q;
}
return(item);
}
return NULL;
}
void linklist::display()
{
node *q,*p;
q=f;
p=NULL;
cout<<"elements in the circular linked list after insertion ";
while(q!=p)
{
cout<<q->data<<"\n";
q=q->link;
p=f;
}
}
linklist::~linklist()
{
node *q;
while(f!=r)
{
q=f->link;
delete f;
f=q;
}
delete r;
}
void main()
{
linklist l;
int n,n1,n2,ch;
clrscr();
cir:
cout<<"\n\n1.insert 2.display 3.delete 4.exit";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\nenter the limit ";
cin>>n;
for(int i=0;i<n;i++)
{
cin>>n1;
l.add(n1);
}
l.display();
goto cir;
case 2:
cout<<"\n\nelements in the list";
l.display();
goto cir;
case 3:
cout<<"\n\nenter the limit ";
cin>>n2;
for(i=0;i<n2;i++)
{
l.del();
}
goto cir;
default:
break;
}
getch();
}
OUTPUT:
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:1
enter the number of elements to be inserted: 3
enter the element 1 :12
enter the element 2 :56
enter the element 3 :34
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at begining
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:1
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at begining
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:2
34 12 56 34 78
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at begining
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:3
34 89 12 56 34 78
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:3
Enter ur choice:1
The list before deletion: The items present in the list are : 34
12 56 34 78
The list after deletion: The items present in the list are : 12 56 34 78
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:3
Enter ur choice:2
The list before deletion: The items present in the list are : 12
56 34 78
The list after deletion: The items present in the list are : 12 56 34
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:3
Enter ur choice:3
The items present in the list are : 12 56 34
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:4
The items present in the list are : 12 34
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:5
The items present in the list are : 12 34
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:6
The items present in the list are : 12 34
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:7
The items present in the list are : 12 34
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct dlist
{
int data;
dlist *rlink;
dlist *llink;
}*p;
void create(struct dlist*p)
{
int max,n,i;
struct dlist *x;
// cout<<"\nEnter the no of nodes:";
// cin>>max;
cout<<"\nEnter the element:";
cin>>n;
x->data=n;
x->rlink=NULL;
x->llink=NULL;
p->rlink=x;
/* for(i=0;i<max-1;i++)
{
l->rlink=new list;
l->llink=p;
cout<<"\nEnter the element:";
cin>>n;
l->rlink->data=n;
l->rlink->rlink=NULL;
l=l->rlink;
}*/
}
void display(struct dlist*p)
{
cout<<"\nThe elements are:\n";
while(p->rlink!=NULL)
{
p=p->rlink;
cout<<p->data<<"\n";
}
}
void insbeg(struct dlist*p,int a)
{
struct dlist *x;
x=new dlist;
x->data=a;
x->rlink=p->rlink;
x->llink=NULL;
p->rlink->llink=x;
p->rlink=x;
display(p);
}
void inslast(struct dlist*p,int a)
{
struct dlist *x,*y;
x=new dlist;
x->data=a;
x->rlink=NULL;
y=p;
while(y->rlink!=NULL)
{
y=y->rlink;
}
y->rlink=x;
x->llink=y;
display(p);
}
void inspos(struct dlist*,int a,int b)
{
int count=1;
struct dlist *x,*y;
y=p;
while(count!=b)
{
y=y->rlink;
count++;
}
x=new dlist;
x->data=a;
x->rlink=y->rlink;
y->rlink->llink=x;
y->rlink=x;
x->llink=y;
display(p);
}
void delbeg(struct dlist*p)
{
struct dlist *q;
if(p->rlink!=NULL)
{
q=p->rlink;
p->rlink=p->rlink->rlink;
delete(q);
}
else
p=NULL;
display(p);
}
void dellast(struct dlist*p)
{
struct dlist *x,*y;
y=p;
x=y->rlink;
while(x->rlink!=NULL)
{
x=x->rlink;
y=y->rlink;
}
y->rlink=NULL;
display(p);
}
void delpos(struct dlist*p,int a)
{
int count=1;
struct dlist *x;
x=p;
while(count!=a)
{
x=x->rlink;
count++;
}
x->rlink=x->rlink->rlink;
display(p);
}
void search(struct dlist*p,int a)
{
int count=0,flag=0;
struct dlist *x;
x=p;
while(x->data!=a)
{
x=x->rlink;
count++;
if(x->data==a)
{
cout<<"Element found at position "<<count;
flag=1;
break;
}
}
if(flag==0)
cout<<"\nElement not found";
}
void count(struct dlist*p)
{
int c=0;
while(p->rlink!=NULL)
{
p=p->rlink;
c++;
}
cout<<"\nNo. of nodes:"<<c;
}
void sort(struct dlist*p)
{
struct dlist *x,*y,*z;
int c=0,i,j,t;
y=p;
while(y->rlink!=NULL)
{
y=y->rlink;
c++;
}
x=p;
z=x->rlink;
for(i=0;i<=c+1;i++)
{
z=x->rlink;
for(j=0;j<=c+1;j++)
{
if(x->data > z->data)
{
t=z->data;
z->data=x->data;
x->data=t;
}
z=z->rlink;
}
x=x->rlink;
}
display(p);
}
void main()
{
int ch,n,m,ps,p1,no,ka,ka1;
clrscr();
p=new dlist;
while(1)
{
cout<<"\nManipulation\n";
cout<<"\n 1.Create\n 2.display \n 3.Insert";
cout<<"\n 4.Delete ";
cout<<"\n 5.sort\n 6.Search\n 7.count \n 8.Exit";
cout<<"\n\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
create(p);
break;
case 2:display(p);
break;
case 3:
clrscr();
cout<<"\n1.begin\n2.position\n3.last";
cout<<"\nchoice:";cin>>ka;
if(ka==1)
{
cout<<"\nEnter the element to be inserted at beginning:";
cin>>n;
insbeg(p,n);
break;
}
else if(ka==2)
{
cout<<"\nEnter element to be inserted:";
cin>>m;
cout<<"\nEnter the position:";
cin>>ps;
inspos(p,m,ps);
break;
}
else
{
cout<<"\nEnter element to be inserted at end:";
cin>>m;
inslast(p,m);
break;
}
case 4:
clrscr();
cout<<"\ndelete";
cout<<"\n1.first\n2.end\n3.position";
cout<<"\nchoice:";
cin>>ka1;
if(ka1==1)
{
delbeg(p);
break;
}
else if(ka1==2)
{
dellast(p);
break;
}
else
{
cout<<"\nEnter the position:";
cin>>p1;
delpos(p,p1);
break;
}
case 5:
sort(p);
break;
case 6:
cout<<"\nEnter the element to be searched:";
cin>>no;
search(p,no);
break;
case 7:
count(p);
break;
case 8:
exit(0);
}
}
getch();
}
OUTPUT:
1.creation
2.Insertion
3.Deletion
4.Display
5.See
k6.count
7.sorting
8.exit
Enter ur choice:1
enter the number of elements to be inserted :3
enter the element 1: 56
enter the element 2: 81
enter the element 3 :23
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at beginning
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:1
Enter the value to insert:45
Inserted successfully at the begining..
The items present in the list are : 45 56 81 23
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at beginning
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:1
Enter the value to insert:67
Inserted successfully at the begining.. The items present in the list are :
67 45 56 81 23
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:2
1.Insertion at beginning
2.Insertion at th end
3.Insertion after the mentioned position
Enter ur choice:3
Enter the value to insert:56
Inserted successfully at the desired position. The items present in the list are :
56 67 45 56 81 23
Enter ur choice:3
Enter ur choice:1
The list before deletion: The items present in the list are : 67
56 45 56 81 23
The list after deletion: The items present in the list are : 56 45
56 81 23
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:3
Enter ur choice:2
The list before deletion: The items present in the list are : 56
45 56 81 23
The list after deletion: The items present in the list are : 56 45
56 81
doubly linked list
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:3
Enter ur choice:3
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:4
The items present in the list are : 56 56 81
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:5
The items present in the list are : 56 56 81
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:6
The items present in the list are : 56 56 81
1.creation
2.Insertion
3.Deletion
4.Display
5.Seek
6.count
7.sorting
8.exit
Enter ur choice:7
The items present in the list are : 56 56 81