0% found this document useful (0 votes)
68 views

Lab 7 Circular and Doubly Linked Lists

The document discusses implementing different data structures using circular and doubly linked lists. It provides code to: 1. Implement a stack and queue using a circular linked list, including push and pop operations. 2. Create a doubly linked list with functions to add a node to the head, delete a node from the head, and traverse the list. The code examples demonstrate how to create circular and doubly linked list implementations in C++ and include functions for common operations like push, pop, add, and delete on each data structure.

Uploaded by

Athira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Lab 7 Circular and Doubly Linked Lists

The document discusses implementing different data structures using circular and doubly linked lists. It provides code to: 1. Implement a stack and queue using a circular linked list, including push and pop operations. 2. Create a doubly linked list with functions to add a node to the head, delete a node from the head, and traverse the list. The code examples demonstrate how to create circular and doubly linked list implementations in C++ and include functions for common operations like push, pop, add, and delete on each data structure.

Uploaded by

Athira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy