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

Lab Stack Queue Link List Tree

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab Stack Queue Link List Tree

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Date Structures using C++ LAB 2018-2019

Week 3: Write C++ programs to implement the following using an array. Stack ADT b) Queue ADT

sort
Aim: To implement Stack ADT and Queue ADT using an array

Description:
Stack:It is an ordered collection of data elements into which new elements may be inserted and
from which elements may be deleted at one end called the “TOP” of stack.
 A stack is a last-in-first-out ( LIFO ) structure.
 Insertion operation is referred as “PUSH” and deletion operation is referred as “POP”.
 The most accessible element in the stack is the element at the position “TOP”.
 Stack must be created as empty.
 Whenever an element is pushed into stack, it must be checked whether the stack is
full or not.
 Whenever an element is popped form stack, it must be checked whether the stack is
empty or not.
We can implement the stack ADT either with array or linked list.

ALGORITHM: push()

Step 1: if top> =max-1 then


Step 2: Display the stack overflows
Step 3: else then
Step 4: top ++
Step 5: assign stack[top]=x
Step 6: Display element is inserted

ALGORITHM pop()

Step 1: if top = =-1 then


Step 2: Display the stack is underflows
Step 3: else
Step 4: assign x=stack[top]
Step 5: top- -
Step 6: return x
Source code: To implement Stack ADT using an array

#include<iostream>
using namespace std;
#include<stdlib.h>
#define max 50
template <class T>

Page 12
Date Structures using C++ LAB 2018-2019

class stack
{
private:
T top,stk[50],item;
public:
stack();
void push();
void pop();
void display();
};
template <class T>
stack<T>::stack()
{
top=-1;
}
//code to push an item into stack;
template <class T>
void stack<T>::push()
{
if(top==max-1)
cout<<"Stack Overflow...\n";
else
{
cout<<"Enter an item to be pushed:";
top++;
cin>>item;
stk[top]=item;
cout<<"Pushed Sucesfully....\n";
}
}
template <class T>
void stack<T>::pop()
{
if(top==-1)
cout<<"Stack is Underflow";
else
{
item=stk[top];
top--;
cout<<item<<" is poped Sucesfully....\n";
}
}
template <class T>
void stack<T>::display()
{
if(top==-1)
cout<<"Stack Under Flow";
else
{
for(int i=top;i>-1;i--)

Page 13
Date Structures using C++ LAB 2018-2019

{
cout<<"|"<<stk[i]<<"|\n";
cout<<"----\n";
}
}
}
int main()
{
int choice;
stack<int>st;
while(1)
{
cout<<"\n\n*****Menu for Skack operations*****\n\n";
cout<<"1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n";
cout<<"Enter Choice:";
cin>>choice;
switch(choice)
{
case 1:
st.push();
break;
case 2:
st.pop();
break;
case 3: cout<<"Elements in the Stack are....\n";
st.display();
break;
case 4:
exit(0);
default:cout<<"Invalid choice...Try again...\n";
}
}
}

Results

Page 14
Date Structures using C++ LAB 2018-2019

QUEUE
DESCRIPTION:
Queue is a data structure in which the elements are added at one end, called the rear, and
deleted from the other end, called the front. A First In First Out data structure (FIFO).The
rear of the queue is accessed whenever a new element is added to the queue, and the front of
the queue is accessed whenever an element is deleted from the queue. As in a stack, the
middle elements in the queue are in accessible, even if the queue elements are sorted in an
array.
BASIC QUEUE OPERATIONS:
1. initializeQueue(): Initializes the queue to an empty state.
2. Determines whether the queue is empty. If the queue is empty, it returns the value
true; otherwise, it returns the value false.
3. Determines whether the queue is full. If the queue is empty, it returns the value
true; otherwise, it returns the value false.
4. rear: Returns the last element of the queue. Prior to this operation, the queue must
exit.
5. front: Returns the front, that is, the first element of the queue. Priority to this
operation, the queue must exit.
Queue can be stored either in an array or in linked list. We will consider both
implementations. Because elements are added at one end and remove from the other
end, we need two pointers to keep track of the front and rear of the queue, called
queueFront and queueRear. Queues are restricted versions of arrays and linked lists.
The middle terms of queue should not be accessed directly.

Source code: To implement Queue ADT using an array

#include<stdlib.h>
#include<iostream>
using namespace std;
#define max 5
template <class T>
class queue
{
private:T q[max],item;
int front,rear;
public: queue();
void insert_q();
void delete_q();
void display_q();
};

Page 15
Date Structures using C++ LAB 2018-2019

template <class T>


queue<T>::queue()
{
front=rear=-1;
}

//code to insert an item into queue;


template <class T>
void queue<T> ::insert_q()
{
if(rear>=max-1)
cout<<"queue Overflow...\n";
else
{
if(front>rear)
front=rear=-1;
else
{ if(front==-1)
front=0;
rear++;
cout<<"Enter an item to be inserted:";
cin>>item;
q[rear]=item;
cout<<"inserted Sucesfully..into queue..\n";
}
}
}
template <class T>
void queue<T>::delete_q()
{
if(front==-1||front>rear)
{
front=rear=-1;
cout<<"queue is Empty....\n";
}
else
{
item=q[front];
front++;
cout<<item<<" is deleted Sucesfully....\n";
}
}
template <class T>
void queue<T>::display_q()
{
if(front==-1||front>rear)
{
front=rear=-1;
cout<<"queue is Empty....\n";
}

Page 16
Date Structures using C++ LAB 2018-2019

else
{
for(int i=front;i<=rear;i++)
cout<<"|"<<q[i]<<"|<--";
}
}
int main()
{
int choice;
queue<int> q;
while(1)
{
cout<<"\n\n*****Menu for QUEUE operations*****\n\n";
cout<<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n";
cout<<"Enter Choice:";
cin>>choice;
switch(choice)
{
case 1: q.insert_q();
break;
case 2: q.delete_q();
break;
case 3: cout<<"Elements in the queue are....\n";
q.display_q();
break;
case 4: exit(0);
default: cout<<"Invalid choice...Try again...\n";
}

}
return 0;
}
Results

Assignment :-

Task Date Sign remark


1.Write a program to perform matching of parenthesis using stack.
2.Write a program to perform evaluation of postfix expression
3.Write a program to convert given infix expression to post fix

Page 17
Date Structures using C++ LAB 2018-2019

Week 4: C++ programs to implement list ADT to perform following operations


a) Insert an element into a list. b) Delete an element from list
c) Search for a key element in list d)count number of nodes in list

sort
Aim: To implement list ADT to perform following operations
a) Insert an element into a list. b) Delete an element from list
c) Search for a key element in list d)count number of nodes in list

Description:
List ADT
A linked list is a data structure consisting of a group of nodes which together represent a
sequence. Each node is composed of a data part and a reference (in other words, a link) to the next
node in the sequence.

The Linked List is a collection of elements called nodes, each node of which stores two items of
information, i.e., data part and link field.
The data part of each node consists the data record of an entity.
The link field is a pointer and contains the address of next node.
The beginning of the linked list is stored in a pointer termed as head which points to the first
node.
The head pointer will be passed as a parameter to any method, to perform an operation.
First node contains a pointer to second node, second node contains a pointer to the third node
and so on.
The last node in the list has its next field set to NULL to mark the end of the list.
 There are several variants of linked lists. These are as follows:
 Singly linked list
 Circular linked list
 Doubly linked list
 Doubly circular linked list

SINGLE LINKED LIST:

Single Linked List is a collection of nodes. Each node contains 2 fields: I) info where the
information is stored and ii) link which points to the next node in the list.

The node is like this:


Node
Info (or) data Link (or) next

Department of CSE Page 18


Date Structures using C++ LAB 2018-2019

The operations that can be performed on single linked lists includes: insertion, deletion and
traversing the list.
Various operations on a single linked list are
1.Insertion of a node into list
2.Deletion of a node from list
3.Traversal of the list

Source code:To Implement LIST ADT in C++

#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
class node
{
public:
int data;
node *next;
};
class List
{
int item;
node *head;
public: List( );
void insert_front( );
void insert_end( );
void delete_front( );
void delete_end( );
void display( );
int node_count();
void delete_before_pos();
void delete_after_pos();
};
List::List( )
{
head=NULL;
}
//code to insert an item at front List;
void List::insert_front( )
{
node *p;
cout<<"Enter an element to be inserted:";
cin>>item;
p=new node;
p->data=item;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else

Page 19
Date Structures using C++ LAB 2018-2019

{ p->next=head;
head=p;
}
cout<<"\nInserted at front of Linked List Sucesfully....\n";
}

//code to insert an item at end List


void List::insert_end( )
{
node *p;
cout<<"Enter an element to be inserted:";
cin>>item;
p=new node;
p->data=item;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
node*t;
t=head;
while(t->next!=NULL)
t=t->next;
t->next=p;
}
cout<<"\nInserted an element at end of Linked List Sucesfully....\n";
}

void List::delete_front( )
{
node*t;
if(head==NULL)
cout<<"\nList is Underflow";
else
{ item=head->data;
t=head;
head=head->next;
cout<<"\n"<<item<<" is deleted Sucesfully from List....\n";
delete(t);
}
}

void List::delete_end( )
{
node*t,*prev;
if(head==NULL)
cout<<"\nList is Underflow";

Department of CSE Page 20


Date Structures using C++ LAB 2018-2019

else
{
t=head;
if(head->next==NULL)
{
cout<<"\n"<<t->data<<" is deleted Sucesfully from List....\n";
delete(t);
head=NULL;
}
else
{
while(t->next!=NULL)
{
prev=t;
t=t->next;
}
prev->next=NULL;
cout<<"\n"<<t->data<<" is deleted Sucesfully from List....\n";
delete(t);
}
}
}
//Delete a node before a position
void List::delete_before_pos( )
{
int i=1;
int pos;
node*t,*prev;
if(head==NULL)
cout<<"\nList is Underflow";
else
{ cout<<"Enter position at which node has to be deleted:";
cin>>pos;
t=head;
int nc=node_count();
if(pos>nc||pos<=0)
cout<<"invalid position ...try again\n";
else
{
cout<<"Before Deletion elements in the List are..\n";
display();

while(i<pos)
{
prev=t;
t=t->next;
i++;
}
if(i==1)
{

Page 21
Date Structures using C++ LAB 2018-2019

cout<<"\n"<<t->data<<" is deleted Sucesfully from List....\n";


if(head->next==NULL)
head=NULL;
else
{
t=head;
head=head->next;
cout<<"\n"<<t->data<<"is deleted Sucesfully from List....\n";
delete(t);
}
}
else
{
prev->next=t->next;
cout<<"\n"<<t->data<<" is deleted Sucesfully from List....\n";
delete(t);
}
cout<<"After Deletion elements in the List are..\n";
display();
}
}
}
//Delete a node after a position
void List::delete_after_pos( )
{
int i=1;
int pos;
node*t,*prev;
if(head==NULL)
cout<<"\nList is Underflow";
else
{ cout<<"Enter position at which node has to be deleted:";
cin>>pos;
t=head;
int nc=node_count();
if(pos>nc||pos<=0)
cout<<"invalid position ...try again\n";
else
{
cout<<"Before Deletion elements in the List are..\n";
display();
while(i<pos)
{
prev=t;
t=t->next;
i++;
}
if(i==1)
{
cout<<"\n"<<t->data<<" is deleted Sucesfully from List....\n";

Page 22
Date Structures using C++ LAB 2018-2019

if(head->next==NULL)
head=NULL;
else
{
t=head;
head=head->next;
cout<<"\n"<<t->data<<" is deleted Sucesfully from List....\n";
delete(t);
}
}
else
{
prev->next=t->next;
cout<<"\n"<<t->data<<" is deleted Sucesfully from List....\n";
delete(t);
}
cout<<"After Deletion elements in the List are..\n";
display();
}
}
}

void List::display()
{
node*t;
if(head==NULL)
cout<<"\nList Under Flow";
else
{
cout<<"\nElements in the List are....\n";
t=head;
while(t!=NULL)
{
cout<<"|"<<t->data<<"|->";
t=t->next;
}
}
}
//code to count no of nodes
int List::node_count( )
{
int nc=0;
node*t;
if(head==NULL)
{
cout<<"\nList Under Flow"<<endl;
// cout<<"No Nodes in the Linked List are: "<<nc<<endl;
}
else
{

Page 23
Date Structures using C++ LAB 2018-2019

t=head;
while(t!=NULL)
{
nc++;
t=t->next;
}
// cout<<"No Nodes in the Linked List are: "<<nc<<endl;
}
return nc;
}
int main( )
{
int choice;
List LL;
while(1)
{
cout<<"\n\n***Menu for Linked List operations***\n\n";
cout<<"1.Insert Front\n2.Insert end\n3.Delete front\n4.Delete End\n5.DISPLAY\n";
cout<<"6.Node Count\n7.Del before a position\n8.Del after position\n";
cout<<"9.Clear Scrn\n10.Exit\nEnter Choice:";
cin>>choice;
switch(choice)
{
case 1: LL.insert_front( );
break;
case 2: LL.insert_end( );
break;
case 3: LL.delete_front( );
break;
case 4: LL.delete_end( );
break;
case 5: LL.display( );
break;
case 6:cout<<"No of nodes in List:"<<LL.node_count();
break;
case 7:LL.delete_before_pos();
break;
case 8:LL.delete_after_pos();
break;
case 9:clrscr();
break;
case 10:exit(0);
default:cout<<"Invalid choice...Try again...\n";
}
}
}

Page 24
Date Structures using C++ LAB 2018-2019

Results

Assignment:-

Task Date Sign Remark


1.Write a program to concatenate two linked lists
2.Write a program to reverse a given linked list
3.Write a program to generate two lists from a given linked list
such that first list contains all elements in odd places and second
list consists of all elements in even places

Page 25
Date Structures using C++ LAB 2018-2019

Week 5: Write C++ programs to implement the following using a singly linked list.
a) Stack ADT b) Queue ADT

sort To implement Stack ADT and Queue ADT using a singly linked list.
Aim:

Description:
A Stack is a collection of items in which new items may be deleted at end to
implement stack using linked list we need to define a node which in turn consist of data a
pointer to the next node. The advantage of representing stack using linked lists is that we can
decide which end should be top of a stack. And since the array size is fixed, in the array
(linear) representation of stack, only fixed number of elements can be pushed onto the stack.
If in a program the number of elements to be pushed exceeds the size of the array, the
program may terminate in an error. We must overcome these problems.
By using linked lists we can dynamically organize data (such as an ordered
list).Therefore , ;ogically the stack is never full. The stack is full only if we run out of
memory space. In the below program we select front end as top if stack in which we cab add
or remove data.

Source code: To implement Stack ADT using a singly linked list.

#include<stdlib.h>
#include<iostream>
using namespace std;
template <class T>
class node
{
public:
T data;
node<T>*next;
};

template <class T>


class stack
{
private:
T item;
node<T> *top;
public: stack();
void push();
void pop();
void display();
};
template <class T>
stack<T>::stack()
{
top=NULL;
}
//code to push an item into stack;
template <class T>

Page 26
Date Structures using C++ LAB 2018-2019

void stack<T>::push()
{
node<T>*t;
node<T>*p;
cout<<"Enter an item to be pushed:";
cin>>item;
p=new node<T>;
p->data=item;
p->next=top;
top=p;
cout<<"\nPushed Sucesfully....\n";
}

template <class T>


void stack<T>::pop()
{
node<T>*t;
if(top==NULL)
cout<<"\nStack is Underflow";
else
{
item=top->data;
top=top->next;
cout<<"\n"<<item<<" is poped Sucesfully....\n";
}

}
template <class T>
void stack<T>::display()
{
node<T>*t;
if(top==NULL)
cout<<"\nStack Under Flow";
else
{ cout<<"\nElements in the Stack are....\n";
t=top;
while(t!=NULL)
{
cout<<"|"<<t->data<<"|\n";
cout<<"----\n";
t=t->next;
}
}
}
int main()
{
int choice;
stack<int>st;
while(1)
{

Page 27
Date Structures using C++ LAB 2018-2019

cout<<"\n\n***Menu for Skack operations***\n\n";


cout<<"1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n";
cout<<"Enter Choice:";
cin>>choice;
switch(choice)
{
case 1:
st.push();
break;
case 2:
st.pop();
break;
case 3: st.display();
break;
case 4:
exit(0);
default:cout<<"Invalid choice...Try again...\n";
}
}
}

Results

Description: Queue is a data structure in which the elements are added at one end, called the
rear, and deleted from the other end, called the front. A First In First Out data structure
(FIFO). The rear of the queue is accessed whenever a new element is added to the queue, and
the front of the queue is accessed whenever an element is deleted from the queue. As in a
stack, the middle elements in the queue are in accessible, even if the queue elements are
sorted in an array.
Source code: To implement QUEUE ADT using a singly linked list

#include<stdlib.h>
#include<iostream.h>
template <class T>
class node
{
public:
T data;
node<T>*next;
};

Department of CSE Page 28


Date Structures using C++ LAB 2018-2019

template <class T>


class queue
{
private:
T item;
friend class node<T>;
node<T> *front,*rear;
public: queue();
void insert_q();
void delete_q();
void display_q();
};
template <class T>
queue<T>::queue()
{
front=rear=NULL;
}
//code to push an item into queue;
template <class T>
void queue<T>::insert_q()
{
node<T>*p;
cout<<"Enter an element to be inserted:";
cin>>item;
p=new node<T>;
p->data=item;
p->next=NULL;
if(front==NULL)
{
rear=front=p;
}
else
{
rear->next=p;
rear=p;
}
cout<<"\nInserted into Queue Sucesfully....\n";
}
//code to delete an element
template <class T>
void queue<T>::delete_q()
{
node<T>*t;
if(front==NULL)
cout<<"\nqueue is Underflow";
else
{
item=front->data;
t=front;
front=front->next;

Page 29
Date Structures using C++ LAB 2018-2019

cout<<"\n"<<item<<" is deleted Sucesfully from queue....\n";


}
delete(t);
}
//code to display elements in queue
template <class T>
void queue<T>::display_q()
{
node<T>*t;
if(front==NULL)
cout<<"\nqueue Under Flow";
else
{
cout<<"\nElements in the queue are....\n";
t=front;
while(t!=NULL)
{
cout<<"|"<<t->data<<"|<-";
t=t->next;
}
}
}
int main()
{
int choice;
queue<int>q1;

while(1)
{
cout<<"\n\n***Menu for Queue operations***\n\n";
cout<<"1.Insert\n2.Delete\n3.DISPLAY\n4.EXIT\n";
cout<<"Enter Choice:";
cin>>choice;
switch(choice)
{
case 1: q1.insert_q();
break;
case 2: q1.delete_q();
break;
case 3: q1.display_q();
break;
case 4: exit(0);
default:cout<<"Invalid choice...Try again...\n";
}
}
return 0;
}

Page 30
Date Structures using C++ LAB 2018-2019

Results

Assignment:-

Task Date Sign Remark


1.Submit an analysis report on stack implemented by static
allocation and dynamic allocation
2.Submit an analysis report on Queue implemented by static
allocation and dynamic allocation
3.Submit a report on application of stack and queue with
explanation

Page 31
Date Structures using C++ LAB 2018-2019

Week 6: Write C++ programs to implement the de queue (double ended queue) ADT using a
doubly linked list and an array.

Aim: To implement the de queue (double ended queue) ADT using a doubly linked list and
an array.
sort
Source code: To implement the de queue (double ended queue) ADT

#include <iostream.h>
#include<stdlib.h>
#include <conio.h>
template<class T>
class node
{
public:
T data;
node*prev;
node*next;
};
template<class T>
class dll
{
node<T>*head;
public:
dll();
void insert_front();
void insert_end();
void delete_front();
void delete_end();
void display();
void insert_at_pos();
int node_count();
};
template<class T>
dll<T>::dll()
{
head=NULL;
}
//code to insert node at front of list...
template<class T>
void dll<T>::insert_front()
{
node<T>*new_node;
int x;
new_node=new node<T>;
cout<<"Enter data into node:\n";
cin>>x;
new_node->data=x;
new_node->prev=NULL;
new_node->next=NULL;

Page 32
Date Structures using C++ LAB 2018-2019

if(head==NULL)
head=new_node;
else
{
new_node->next=head;
head->prev=new_node;
head=new_node;
}
cout<<"Inserted node sucesfully...";
}
//code to insert node at end of list...
template<class T>
void dll<T>::insert_end()
{
node<T>*new_node;
node<T>*t;
int x;
new_node=new node<T>;
cout<<"Enter data into node:\n";
cin>>x;
new_node->data=x;
new_node->next=NULL;
new_node->prev=NULL;
if(head==NULL)
head=new_node;
else
{
t=head;
while(t->next!=NULL)
t=t->next;
t->next= new_node;
new_node->prev=t;
}
cout<<"Inserted node sucesfully...";
}
template<class T>
void dll<T>::delete_front()
{
node<T>*temp;
if(head==NULL)
cout<<"List is empty....\n ";
else if(head->next==NULL)
{
temp=head;
cout<<"Deleted element from Doubly Linked List is "<<temp->data<<endl;
delete temp;
head=NULL;

}
else

Page 33
Date Structures using C++ LAB 2018-2019

{
temp=head;
head=head->next;
head->prev=NULL;
cout<<"Deleted element from Doubly Linked List is "<<temp->data<<endl;
delete temp;
cout<<"Elements after deletion from Front are...\n";
display();
}
}
template<class T>
void dll<T>::delete_end()
{
node<T>*t1;
node<T>*t2;
if(head==NULL)
cout<<"List is empty....\n ";
else
{
t1=t2=head;
if(head->next==NULL)
{
head=NULL;
cout<<"Deleted element from Doubly Linked List is "<<t1->data<<endl;
delete t1;
}
else
{
while(t1->next!=NULL)
{
t2=t1;
t1=t1->next;
}
t2->next=NULL;
cout<<"Deleted element from Doubly Linked List is "<<t1->data<<endl;
delete t1;
cout<<"Elements after Deletion from End are...\n";
display();
}
}
}
template<class T>
void dll<T>::insert_at_pos()
{
node<T>*new_node;
node<T>*t1;
node<T>*t2;
int x,pos,nc;
new_node=new node<T>;
cout<<"Enter data into node:\n";

Page 34
Date Structures using C++ LAB 2018-2019

cin>>x;
cout<<"enter Pos at which node has to be inserted:";
cin>>pos;
new_node->data=x;
new_node->next=NULL;
new_node->prev=NULL;
nc=node_count();
cout<<"node count="<<nc<<endl;
if(pos<=0||pos>nc+1)
cout<<"invalid position";
else
{
if(pos==1)
{

if(head==NULL)
head=new_node;
else
{

new_node->next=head;
head->prev=new_node;
head=new_node;
}
}

else
{
t1=t2=head;
int i=1;
while(i<pos)
{
t2=t1;
t1=t1->next;
i++;
}

if(t1==NULL)
{
new_node->next=NULL;
}
else
{
t1->prev=new_node;
}
t2->next= new_node;
new_node->prev=t2;
}
cout<<"Inserted node sucesfully...";
}

Page 35
Date Structures using C++ LAB 2018-2019

template<class T>
int dll<T>:: node_count()
{
int i=0;
node<T> *t;
t=head;
while(t!=NULL)
{
t=t->next;
i++;
}
return i;
}
template<class T>
void dll<T>::display()
{
node<T>*t;
int count;
t=head;
if(head==NULL)
{
cout<<"Doubly linked list is empty.....\n";
}
else
{
cout<<"Elements in the list are........\n";
while(t!=NULL)
{
cout<<"|"<<t->data<<"|-> ";
t=t->next;
}
}
count=node_count();
cout<<"\n Total No of nodes in Doubly linked List are:"<<count<<endl;

}
int main()
{
dll<int> d;
int choice;
while(1)
{ cout<<"\n***Menu for Doubly linked list operations***\n";
cout<<"\n1.insert front";
cout<<"\n2.insert end";
cout<<"\n3.delete front";
cout<<"\n4.delete end";
cout<<"\n5.Display";
cout<<"\n6.insert at pos";

Page 36
Date Structures using C++ LAB 2018-2019

cout<<"\n7.Exit";
cout<<"\nEnter Choice:";
cin>>choice;
switch(choice)
{
case 1:d.insert_front();
break;
case 2:d.insert_end();
break;
case 3:d.delete_front();
break;
case 4:d.delete_end();
break;
case 5:d.display();
break;
case 6:d.insert_at_pos();
break;
case 7:exit(0);
}
}
//return 0;
}

Results

Assignment:-

Task Date Sign Remark


1.Implement queue using doubly linked list
2.Implement circular Queue using doubly linked list
3.

Page 37
Date Structures using C++ LAB 2018-2019

Week 7: Write a C++ program to perform the following operations:


a) Insert an element into a binary search tree.
b) Delete an element from a binary search tree.
c) Search for a key element in a binary search tree.

Description:

Binary Search Tree:


So to make the searching algorithm faster in a binary tree we will go for building the
binary search tree. The binary search tree is based on the binary search algorithm. While
creating the binary search tree the data is systematically arranged.
That means values at
left sub-tree < root node value < right sub-tree values.

10

7 15

5 9 12 18

Source code:

#include<stdlib.h>
#include<iostream.h>
class node
{
public:
int data;
node*lchild;
node*rchild;
};
class bst:public node
{ int item;
node *root;
public: bst();
void insert_node();
void delete_node();
void display_bst();
void inorder(node*);
Page 38
Date Structures using C++ LAB 2018-2019

};
bst::bst()
{
root=NULL;
}
void bst:: insert_node()
{
node *new_node,*curr,*prev;
new_node=new node;
cout<<"Enter data into new node";
cin>>item;
new_node->data=item;
new_node->lchild=NULL;
new_node->rchild=NULL;
if(root==NULL)
root=new_node;
else
{ curr=prev=root;
while(curr!=NULL)
{ if(new_node->data>curr->data)
{ prev=curr;
curr=curr->rchild;
}
else
{ prev=curr;
curr=curr->lchild;
}
}
cout<<"Prev:"<<prev->data<<endl;
if(prev->data>new_node->data)
prev->lchild=new_node;
else
prev->rchild=new_node;
}
}
//code to delete a node
void bst::delete_node()
{
if(root==NULL)
cout<<"Tree is Empty";
else
{
int key;
cout<<"Enter the key value to be deleted";
cin>>key;
node* temp,*parent,*succ_parent;
temp=root;
while(temp!=NULL)
{ if(temp->data==key)
{ //deleting node with two childern

Page 39
Date Structures using C++ LAB 2018-2019

if(temp->lchild!=NULL&&temp->rchild!=NULL)
{ //search for inorder sucessor
node*temp_succ;
temp_succ=temp->rchild;
while(temp_succ->lchild!=NULL)
{
succ_parent=temp_succ;
temp_succ=temp_succ->lchild;
}
temp->data=temp_succ->data;
succ_parent->lchild=NULL;
cout<<"Deleted sucess fully";
return;
}
//deleting a node having one left child
if(temp->lchild!=NULL&temp->rchild==NULL)
{
if(parent->lchild==temp)
parent->lchild=temp->lchild;
else
parent->rchild=temp->lchild;
temp=NULL;
delete(temp);
cout<<"Deleted sucess fully";
return;
}
//deleting a node having one right child
if(temp->lchild==NULL&temp->rchild!=NULL)
{
if(parent->lchild==temp)
parent->lchild=temp->rchild;
else
parent->rchild=temp->rchild;
temp=NULL;
delete(temp);
cout<<"Deleted sucess fully";
return;
}
//deleting a node having no child
if(temp->lchild==NULL&temp->rchild==NULL)
{
if(parent->lchild==temp)
parent->lchild=NULL;
else
parent->rchild=NULL;
temp=NULL;
delete(temp);
cout<<"Deleted sucess fully";
return;
}

Page 40
Date Structures using C++ LAB 2018-2019

}
else if(temp->data<key)
{ parent=temp;
temp=temp->rchild;
}
else if(temp->data>key)
{ parent=temp;
temp=temp->lchild;
}
}//end while
}//end if
}//end delnode func
void bst::display_bst()
{
if(root==NULL)
cout<<"\nBST Under Flow";
else
inorder(root);
}
void bst::inorder(node*t)
{
if(t!=NULL)
{
inorder(t->lchild);
cout<<" "<<t->data;
inorder(t->rchild);
}
}

int main()
{
bst bt;
int i;
while(1)
{
cout<<"****BST Operations****";
cout<<"\n1.Insert\n2.Display\n3.del\n4.exit\n";
cout<<"Enter Choice:";
cin>>i;
switch(i)
{
case 1:bt.insert_node();
break;
case 2:bt.display_bst();
break;
case 3:bt.delete_node();
break;
case 4:exit(0);
default: cout<<"Enter correct choice";
}

Page 41
Date Structures using C++ LAB 2018-2019

}
}

Results

Assignment:-

Task Date Sign Remark


1.What is a binary tree& binary search tree
2.Applications of binary search tree

Page 42

You might also like