Lab 7 Circular and Doubly Linked Lists

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Lab 7 Circular and Doubly Linked Lists

1. Create a circulat linked list to


a. implement the data structure ‘stack’ and its primitive operations.
Ans;
code;#include<iostream>
#include<stdlib.h>
using namespace std;
class cstack
{
struct node
{
int info;
struct node *next ;

};
struct node * headptr;
public:
cstack();
void push(int x);
int pop();
void traverse();
};
cstack::cstack()
{
headptr=NULL;
}
void cstack::push(int x)
{
struct node *p;
p=new node();

if(headptr==NULL)
{
p->next=p;
p->info=x;
headptr=p;
}
else
{
p->info=x;
p->next=headptr->next;
headptr->next=p;
}

int cstack::pop()
{
struct node *p;
int r;
if(headptr==NULL)
{
cout<<"list is empty"<<endl;
return 0;
}
if(headptr==headptr->next)
{
r=headptr->info;
headptr=NULL;
cout<<"deleted element - "<<r<<endl;
}
else
{
p=headptr->next;
r=p->info;
headptr->next=p->next;
p->next=NULL;
delete p;
cout<<"deleted element - "<<r<<endl;
}return r;
}
void cstack::traverse()
{
struct node *p;
if (headptr == NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return ;
}
p= headptr->next;
cout<<"Circular Link List: "<<endl;
while (p != headptr)
{
cout<<p->info<<endl;
p= p->next;
}
cout<<p->info<<" ";

}
int main()
{
int c,x;
cstack cs;
while(c!=4)
{
cout<<"1.push 2.pop 3.traverse 4.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>c;
switch(c)
{
case 1:cout<<"enter the no ";
cin>>x;
cs.push(x);
break;
case 2:cs.pop();
break;
case 3:cs.traverse();
break;
case 4:exit(0);
}
}
}

output:
b. implement the data structure ‘queue’ and its primitive operations
answer:
code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class cqueue
{
struct node
{
int info;
struct node *next ;

};
struct node * headptr;
public:
cqueue();
void push(int x);
int pop();
void traverse();
};
cqueue:: cqueue()
{
headptr=NULL;
}
void cqueue::push(int x)
{
struct node *p;
p=new node();

if(headptr==NULL)
{
headptr=p;
p->info=x;
headptr->next=p;
}
else
{
p->next=headptr->next;
p->info=x;
headptr->next=p;
headptr=p;
}
}

int cqueue::pop()
{
struct node *p;
int r;
if(headptr==NULL)
{
cout<<"list is empty"<<endl;
return 0;
}
if(headptr==headptr->next)
{
r=headptr->info;
headptr=NULL;
cout<<"deleted element - "<<r<<endl;
}
else
{
p=headptr->next;
r=p->info;
headptr->next=p->next;
p->next=NULL;
delete p;
cout<<"deleted element - "<<r<<endl;
}return r;
}
void cqueue::traverse()
{
struct node *p;
if (headptr == NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return ;
}
p= headptr->next;
cout<<"Circular Link List: "<<endl;
while (p != headptr)
{
cout<<p->info<<endl;
p= p->next;
}
cout<<p->info<<endl;

}
int main()
{
int c,x;
cqueue cq;
while(c!=4)
{
cout<<"1.push 2.pop 3.traverse 4.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>c;
switch(c)
{
case 1:cout<<"enter the no ";
cin>>x;
cq.push(x);
break;
case 2:cq.pop();
break;
case 3:cq.traverse();
break;
case 4:exit(0);
}
}
}

output:
2. Create a doubly linked list with the following functions
a. AddNodeToHead
b. DeleteNodeFromHead
c. Traverse
ans:
code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class dstack
{
struct node
{
int info;
struct node *next ,*prev;

};
struct node * headptr;
public:
dstack();
void addnodetohead(int x);
int deletenodefromhead();
void traverse();
};
dstack::dstack()
{
headptr=NULL;
}
void dstack::addnodetohead(int x)
{
struct node *p;
p=new node();
if(headptr==NULL)
{
headptr=p;
p->info=x;
p->prev=NULL;
p->next=NULL;
}
else
{
p->prev=NULL;
p->info=x;
p->next=headptr;
headptr=p;
}
}
int dstack::deletenodefromhead()
{
struct node *p;

if(headptr==NULL)
{
cout<<"list is empty"<<endl;
return 0;
}
else
{
p=headptr;
int s=p->info;
headptr=p->next;
p->prev=NULL;
delete p;
cout<<" deleted element -"<<s<<endl;
return s;
}
}
void dstack::traverse()
{
struct node *p;
if (headptr == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
p = headptr;
cout<<"The Doubly Link List is :"<<endl;
while (p != NULL)
{
cout<<p->info<<endl;
p = p->next;
}

}
int main()
{
int c,x;
dstack ds;
while(c!=4)
{
cout<<"1.addnodetohead 2.deletenodefromhead
3.traverse 4.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>c;
switch(c)
{
case 1:cout<<"enter the no ";
cin>>x;
ds.addnodetohead(x);
break;
case 2:ds.deletenodefromhead();
break;
case 3:ds.traverse();
break;
case 4:exit(0);
}
}
}
output:
--------------------------the end--------------------------------------

You might also like