Professional Documents
Culture Documents
Write a program to implement singly linked list as an ADT that supports the following
operations: i. Insert an element x at the beginning of the singly linked list ii. Insert an
element x at i th position in the singly linked list iii. Remove an element from the beginning
of the doubly linked list iv. Remove an element from i th position in the singly linked list. vi.
Search for an element x in the singly linked list and return its pointer.
Code:
#include<iostream>
class snode
public:
int info;
snode *next;
snode()
next = 0;
snode(int x, snode *n = 0)
info = x;
next = n;
};
class slist
public:
slist()
{
head = tail = 0;
void addtohead(int);
void addtotail(int);
int deletefromhead();
int deletefromtail();
void display();
int isempty();
bool isinlist(int);
void deletenode(int);
int count();
void deletefromi(int);
~slist();
};
if (head == 0)
return 1;
else
return 0;
if (isempty())
head = tail = p;
else
{
p->next = head;
head = p;
if (isempty())
head = tail = p;
else
tail->next = p;
tail = p;
int x = head->info;
if (head == tail)
delete head;
head = tail = 0;
else
snode *p = head;
head = head->next;
delete p;
}
return x;
int x = tail->info;
if (head == tail)
delete tail;
head = tail = 0;
else
snode *p = head;
while(p->next != tail)
p = p->next;
delete tail;
tail = p;
tail->next = 0;
return x;
snode *p = head;
while(p!=0)
cout<<p->info<<"->";
p = p->next;
}
}
snode *p = head;
while(p!=0)
if (x==p->info)
flag = true;
break;
else
p = p->next;
return flag;
int count = 0;
snode *p = head;
while(p!=0)
count += 1;
p = p->next;
return count;
}
void slist :: deletenode(int e)
delete head;
head = tail = 0;
else
if (head->info == e)
snode *p = head;
head = head->next;
delete p;
else
snode *p = head->next;
prev = p;
p = p->next;
if (p!=0)
prev->next = p->next;
if (p == tail)
tail = prev;
delete p;
}
else
int c;
c = count();
if (n == 1)
addtohead(x);
else if (n==c+1)
addtotail(x);
else
if (temp!=0)
temp = temp->next;
if (temp!=0)
p->next = temp->next;
temp->next = p;
else
cout<<"Invalid index!";
}
int i = 1;
int z;
int y = count();
if (x==1)
z = deletefromhead();
else if (x==y)
z = deletefromtail();
else
temp = temp->next;
prev = prev->next;
i++;
int x = temp->info;
prev->next = temp->next;
delete temp;
else if (temp!=0)
int x = temp->info;
prev->next = 0;
delete temp;
else
cout<<"Invalid index!\n";
slist :: ~slist()
while(head != 0)
head = head->next;
delete temp;
temp = head;
}
int main()
char choice;
slist s;
bool b;
int ch, x, n, a;
do
cout<<"\n****\n";
cout<<"1.Add to head\n";
cout<<"2.Add to tail\n";
cout<<"5.Display\n";
cout<<"6.Isempty\n";
cout<<"7.Isinlist\n";
cout<<"8.Delete node\n";
cout<<"9.Count\n";
cout<<"10.Insert a node\n";
cout<<"\n****\n\n";
cin>>ch;
cin>>x;
if (ch==10 || ch==11)
{
cout<<"Enter the position: ";
cin>>n;
switch(ch)
case 1 : s.addtohead(x);
s.display();
cout<<endl;
break;
case 2 : s.addtotail(x);
s.display();
cout<<endl;
break;
case 3 : if (s.isempty())
else
a = s.deletefromhead();
s.display();
cout<<endl;
break;
case 4 : if (s.isempty())
else
a = s.deletefromtail();
cout<<"Deleted element: "<<a<<endl;
s.display();
cout<<endl;
break;
case 5 : if (s.isempty())
else
s.display();
cout<<endl;
break;
case 6 : a = s.isempty();
if (a==0)
else
cout<<endl;
break;
case 7 : if (s.isempty())
else
b = s.isinlist(x);
if (b==1)
else
}
cout<<endl;
break;
case 8 : s.deletenode(x);
cout<<endl;
break;
case 9 : a = s.count();
break;
cout<<endl;
break;
case 11 : if (s.isempty())
else
s.deletefromi(n);
s.display();
cin>>choice;
Output:
/**
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
11.Delete node from i position
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
Deleted element: 7
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
Deleted element: 5
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
Invalid index!
****
MENU
1.Add to head
2.Add to tail
5.Display
6.Isempty
7.Isinlist
8.Delete node
9.Count
10.Insert a node
****
3->3->8->6->
--------------------------------
*/
2. Write a program to implement doubly linked list as an ADT that supports the following
operations: i. Insert an element x at the beginning of the doubly linked list ii. Insert an
element x at the end of the doubly linked list iii. Remove an element from the beginning of
the doubly linked list iv. Remove an element from the end of the doubly linked list.
code:
#include<iostream>
//#include<std bool.h>
using namespace std;
class dnode{
public:
int info;
dnode *next;
dnode *prev;
dnode(){
prev=next=0;
}
dnode(int x,dnode* n=0,dnode *p=0)
{info=x;
next=n;
prev=p;
}
};
};
int dlist::isempty(){
if(head==0){
return 1;
}
else{
return 0;
}
}
void dlist::addtohead(int x){
dnode *p=new dnode(x);
if(isempty()){
head=tail=p;
}
else{
p->next=head;
head->prev=p;
head=p;
}
}
void dlist::addtotail(int x){
dnode *p=new dnode(x);
if(isempty())
{head=tail=p;
}
else{
tail->next=p;
p->prev=tail;
tail=p;
}
}
int dlist::deletefromhead(){
int x=head->info;
if(head==tail){
delete head;
head=tail=0;
}
else{
dnode *p=head;
head=head->next;
head->prev=0;
delete p;
}
return x;
}
int dlist::deletefromtail(){
int x=tail->info;
if(head==tail)
{
delete tail;
head=tail=0;
}
else{
dnode *p=tail;
tail=tail->prev;
tail->next=0;
delete p;
}
return x;
}
void dlist::display(){
dnode *p=head;
while(p!=0){
cout<<p->info<<" -->";
p=p->next;
}
}
bool dlist::isinlist(int x){
dnode *p=head;
while(p!=0){
if(p->info==x){
return true;
}
else{
p=p->next;
}
}
if(p==0){
return false;
}
}
void dlist::length(){
dnode *p=head;
int count;
while(p!=0)
{
count++;
p=p->next;
}
cout<<"The length of the given linked list is"<< count<< " "<<" "<<endl;
}
void dlist::deletenode(int e)
{
if(head==tail && head->info==e)
{
delete head;
head=tail=0;
}
else
{
if(head->info==e)
{
dnode *p=head;
head=head->next;
delete p;
}
else
{
dnode *prev=head;
dnode *p=head->next;
if(p!=0 && p->info!=e)
{
prev=p;
p=p->next;
}
if(p!=0)
{
prev->next=p->next;
if(p==tail)
{
tail=prev;
}
delete p;
}
else
{
cout<<"Elemenet not found "<<endl;
}
}
}
}
dlist::~dlist()
{
dnode *temp=head;
while(head!=0)
{
head=head->next;
delete temp;
temp=head;
}
}
int main()
{
dlist d1;
char t;
do
{
cout<<endl<<"Enter y or Y for continue the program
";
cin>>t;
cout<<"~~~~~~~~~~"<<endl;
cout<<"MENU"<<endl;
cout<<"1.add to head"<<endl;
cout<<"2.add to tail"<<endl;
cout<<"3.delete from head"<<endl;
cout<<"4.delete from tail"<<endl;
cout<<"5.isempty"<<endl;
cout<<"6.display"<<endl;
cout<<"7.isinlist"<<endl;
cout<<"8.length"<<endl;
cout<<"9.delete node"<<endl;
cout<<"10.destructor"<<endl;
cout<<"~~~~~~~~~~"<<endl;
int s;
cout<<"Enter your choice: ";
cin>>s;
if(s==1)
{
int r;
cout<<"Enter the element ";
cin>>r;
d1.addtohead(r);
cout<<"After adding to the head "<<endl;
d1.display();
}
else if(s==2)
{
int k;
cout<<"Enter the element ";
cin>>k;
d1.addtotail(k);
cout<<"After adding to the head "<<endl;
d1.display();
}
else if(s==3)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int c;
c=d1.deletefromhead();
cout<<c<<endl;
cout<<"After deleting from the head
"<<endl;
d1.display();
}
}
else if(s==4)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int d;
d=d1.deletefromhead();
cout<<d<<endl;
cout<<"After deleting from the tail
"<<endl;
d1.display();
}
}
else if(s==5)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
cout<<"list is not empty"<<endl;
cout<<"List"<<endl;
d1.display();
}
}
else if(s==6)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
d1.display();
}
}
else if(s==7)
{
if(d1.isempty()==0)
{
int u;
cout<<"Enter thr element: ";
cin>>u;
if(d1.isinlist(u)==true)
{
cout<<"Element found"<<endl;
}
else
cout<<"Element not found"<<endl;
}
else
cout<<"List is empty"<<endl;
}
else if(s==8)
{
d1.length();
}
else if(s==9)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
int f;
cout<<"Enter the info for deleting: ";
cin>>f;
d1.deletenode(f);
}
}
else
{
if(d1.isempty()==1)
{
cout<<"List is empty"<<endl;
}
else
d1.~dlist();
}
}
while(t=='Y'||t=='y');
if(t!='Y' || t!='y')
{
cout<<"Thanks"<<endl;
}
return 0;
}
/*
Output:
--------------------------------
Process exited after 219.9 seconds with return value 0
Press any key to continue . . .
*/
3. Write a program to implement circular linked list as an ADT which supports the following
operations: i. Insert an element x in the list ii. Remove an element from the list iii. Search for
an element x in the list and return its pointer
Code:
#include<iostream>
//#include<std bool.h>
using namespace std;
class cnode{
public:
int info;
cnode *next;
cnode *prev;
cnode()
{
prev=next=0;
}
cnode(int x,cnode* n=0,cnode *p=0)
{info=x;
next=n;
}
};
};
int clist::isempty(){
if(head==0){
return 1;
}
else{
return 0;
}
}
void clist::addtohead(int x){
cnode *p=new cnode(x);
if(isempty()){
head=tail=p;
}
else{
p->next=head;
head->prev=p;
head=p;
}
}
void clist::addtotail(int x){
cnode *p=new cnode(x);
if(isempty())
{head=tail=p;
}
else{
tail->next=p;
p->prev=tail;
tail=p;
}
}
int clist::deletefromhead(){
int x=head->info;
if(head==tail){
delete head;
head=tail=0;
}
else{
cnode *p=head;
head=head->next;
head->prev=0;
delete p;
}
return x;
}
int clist::deletefromtail(){
int x=tail->info;
if(head==tail)
{
delete tail;
head=tail=0;
}
else{
cnode *p=tail;
tail=tail->prev;
tail->next=0;
delete p;
}
return x;
}
void clist::display(){
cnode *p=head;
while(p!=0){
cout<<p->info<<" -->";
p=p->next;
}
}
bool clist::isinlist(int x){
cnode *p=head;
while(p!=0){
if(p->info==x){
return true;
}
else{
p=p->next;
}
}
if(p==0){
return false;
}
}
void clist::length(){
cnode *p=head;
int count;
while(p!=0)
{
count++;
p=p->next;
}
cout<<"The length of the given linked list is"<< count<< " "<<" "<<endl;
}
void clist::deletenode(int e)
{
if(head==tail && head->info==e)
{
delete head;
head=tail=0;
}
else
{
if(head->info==e)
{
cnode *p=head;
head=head->next;
delete p;
}
else
{
cnode *prev=head;
cnode *p=head->next;
if(p!=0 && p->info!=e)
{
prev=p;
p=p->next;
}
if(p!=0)
{
prev->next=p->next;
if(p==tail)
{
tail=prev;
}
delete p;
}
else
{
cout<<"Elemenet not found "<<endl;
}
}
}
}
clist::~clist()
{
cnode *temp=head;
while(head!=0)
{
head=head->next;
delete temp;
temp=head;
}
}
int main()
{
clist c1;
char t;
do
{
cout<<endl<<"Enter y or Y for continue the program ";
cin>>t;
cout<<"~~~~~~~~~~"<<endl;
cout<<"MENU"<<endl;
cout<<"1.add to head"<<endl;
cout<<"2.add to tail"<<endl;
cout<<"3.delete from head"<<endl;
cout<<"4.delete from tail"<<endl;
cout<<"5.isempty"<<endl;
cout<<"6.display"<<endl;
cout<<"7.isinlist"<<endl;
cout<<"8.length"<<endl;
cout<<"9.delete node"<<endl;
cout<<"10.destructor"<<endl;
cout<<"~~~~~~~~~~"<<endl;
int s;
cout<<"Enter your choice: ";
cin>>s;
if(s==1)
{
int r;
cout<<"Enter the element ";
cin>>r;
c1.addtohead(r);
cout<<"After adding to the head "<<endl;
c1.display();
}
else if(s==2)
{
int k;
cout<<"Enter the element ";
cin>>k;
c1.addtotail(k);
cout<<"After adding to the head "<<endl;
c1.display();
}
else if(s==3)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int c;
c=c1.deletefromhead();
cout<<c<<endl;
cout<<"After deleting from the head "<<endl;
c1.display();
}
}
else if(s==4)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int d;
d=c1.deletefromhead();
cout<<d<<endl;
cout<<"After deleting from the tail "<<endl;
c1.display();
}
}
else if(s==5)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
cout<<"list is not empty"<<endl;
cout<<"List"<<endl;
c1.display();
}
}
else if(s==6)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
c1.display();
}
}
else if(s==7)
{
if(c1.isempty()==0)
{
int u;
cout<<"Enter thr element: ";
cin>>u;
if(c1.isinlist(u)==true)
{
cout<<"Element found"<<endl;
}
else
cout<<"Element not found"<<endl;
}
else
cout<<"List is empty"<<endl;
}
else if(s==8)
{
c1.length();
}
else if(s==9)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
int f;
cout<<"Enter the info for deleting: ";
cin>>f;
c1.deletenode(f);
}
}
else
{
if(c1.isempty()==1)
{
cout<<"List is empty"<<endl;
}
else
c1.~clist();
}
}
while(t=='Y'||t=='y');
if(t!='Y' || t!='y')
{
cout<<"Thanks"<<endl;
}
return 0;
}
Output:
/*
--------------------------------
Process exited after 170.6 seconds with return value 0
Press any key to continue . . .
*/
void stack1::display(){
for(int i=top;i>=0;i--){
cout<<a[i]<<" ";
}
}
int stack1::pop(){
int x=a[top];
top--;
return x;
}
int stack1::topel(){
int x=a[top];
return x;
}
int main(){
string post_exp ;
cout<<"enter the postfix expression:";
getline(cin,post_exp);
int result=calculate_Postfix(post_exp);
cout<<endl;
cout<<"The answer after calculating the postfix expression is : ";
cout<<result;
return 0;
}
Output:
/*
enter the postfix expression:598+58*+9-*
int cqueue[5];
int front = -1, rear = -1, n=5;
if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}
/*
1)Enqueue
2)Dequeue
3)isfull
4)isempty
5)Display
6)Exit
Enter choice :
1
Input for insertion:
3
Enter choice :
1
Input for insertion:
5
Enter choice :
1
Input for insertion:
6
Enter choice :
1
Input for insertion:
8
Enter choice :
1
Input for insertion:
7
Enter choice :
1
Input for insertion:
8
Queue Overflow
Enter choice :
2
Element deleted from queue is : 3
Enter choice :
2
Element deleted from queue is : 5
Enter choice :
3
Queue elements are :
687
Enter choice :
4
Exit
--------------------------------
Process exited after 44.94 seconds with return value 0
Press any key to continue . . .
*/
6. Write a program to implement Binary Search Tree as an ADT which supports the following
operations: i. Insert an element x ii. Delete an element x iii. Search for an element x in the
BST iv. Display the elements of the BST in preorder, inorder, and postorder traversal .
Code:
#include <iostream>
#include <algorithm>
using namespace std;
class bstnode {
public:
int info;
bstnode *left;
bstnode *right;
bstnode() {
info = 0;
left = right = 0;
}
class bst {
bstnode *root;
public:
bst();
void insert(int);
void preorder();
void preorder(bstnode*);
void inorder();
void inorder(bstnode*);
void postorder();
void postorder(bstnode*);
void deletenode(bstnode*&, int);
void deleteNode(int);
int height();
int height(bstnode*);
bool search(int);
};
bst::bst() {
root = 0;
}
while (current != 0) {
parent = current;
if (key < current->info)
current = current->left;
else
current = current->right;
}
while (temp != 0) {
if (key == temp->info)
return true;
else if (key < temp->info)
temp = temp->left;
else
temp = temp->right;
}
return false;
}
void bst::inorder() {
inorder(root);
cout << endl;
}
void bst::preorder() {
preorder(root);
cout << endl;
}
void bst::postorder() {
postorder(root);
cout << endl;
}
int bst::height() {
return height(root);
}
int main() {
bst tree;
int choice, key;
do {
cout << "\nBinary Search Tree Operations" << endl;
cout << "1. Insert a node" << endl;
cout << "2. Search for a key" << endl;
cout << "3. Inorder traversal" << endl;
cout << "4. Preorder traversal" << endl;
cout << "5. Postorder traversal" << endl;
cout << "6. Delete a node" << endl;
cout << "7. Calculate the height of the tree" << endl;
cout << "8. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter key to insert: ";
cin >> key;
tree.insert(key);
break;
case 2:
cout << "Enter key to search: ";
cin >> key;
if (tree.search(key)) {
cout << "Key " << key << " is present in the BST." << endl;
} else {
cout << "Key " << key << " is not present in the BST." << endl;
}
break;
case 3:
cout << "Inorder traversal: ";
tree.inorder();
break;
case 4:
cout << "Preorder traversal: ";
tree.preorder();
break;
case 5:
cout << "Postorder traversal: ";
tree.postorder();
break;
case 6:
cout << "Enter key to delete: ";
cin >> key;
tree.deleteNode(key);
break;
case 7:
cout << "Height of the BST: " << tree.height() << endl;
break;
case 8:
cout << "Exiting the program..." << endl;
break;
default:
cout << "Invalid choice! Please enter a valid option." << endl;
break;
}
} while (choice != 8);
return 0;
}
Output:
/*
--------------------------------
Process exited after 83.4 seconds with return value 0
Press any key to continue . . .
*/