












































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
cpp lab manual in c++ data structures
Typology: Thesis
1 / 84
This page cannot be seen from the preview
Don't miss anything!
Templates
Template Functions Suppose we want to write a function that finds the maximum of two objects. To find the maximum of two integers we would write: int maximal(int a, int b) { if (a > b) return a; else return b; } and to find the maximum of two doubles we would write: double maximal(double a, double b) { if (a > b) return a; else return b; } and so on. You can imagine that we would have to write a separate function for every data type (chars, strings, dates, etc.), but notice that the body of each function is exactly the same!!!
Template Function Definition
The definition of a template function depends on an underlying data type. For example: template
Using Template Functions
Template functions are used (called) in exactly the same way as regular functions. For example, if we want to output the maximum of the integers 1000 and 2000, we would write: cout << maximal(1000, 2000) << endl; // will print 2000
The maximal function is said to be instantiated with the data type Item set to int. Similarly, we could do: cout << maximal('a', 'z') << endl; // will print 'z'
One of the most common forms of data organization in computer programs is the ordered or linear list, which is often written as a = (a 1 , a 2 , ….., a (^) n ). A stack is an ordered list in which all insertions and deletions are made at one end, called the top. A queue is an ordered list in which all insertions take place at one end, the rear, whereas all deletions take place at the other end, the front.
The operations of a stack imply that if the elements A, B, C, D and E are inserted into a stack, in that order, then the first element to be removed must be E. Equivalently we say that the last element to be inserted into the stack is the first to be removed. For this reason stacks are sometimes referred to as Last In First Out (LIFO) lists. The operations of a queue require that the first element that is inserted into the queue is the first one to be removed. Thus queues are known as First In First Out (FIFO) lists.
Stack Queue
Above figure shows the examples of a stack and queue each containing the same five elements inserted in the same order.
The simplest way to represent a stack is by using a one-dimensional array, say stack[0 : n-1], where n is the maximum number of allowable entries. The first or bottom element in the stack is stored at stack[0], the second at stack[1], and ith at stack[i-1]. Associated with the array is a variable, typically called top, which points to the top element, to test whether the stack is empty, we ask “if (top<0)”. If not, the topmost element is at stack[top]. Checking whether the stack is full can be done by asking “if (top n-1)”. Two more substantial operations are inserting and deleting elements. The corresponding algorithms are Add and Delete.
1. Write C++ programs to implement the following using an array. a) Stack ADT
#include<iostream.h> #include<conio.h> #include<stdlib.h> #define max 100 template
front rear
top E D C
B A
public: stack(); void push(T); T pop(); void display(); }; template
} } void main() { stack
public: queue(); void insert(T); T remove(); void display(); }; template
} } void main() { queue
do { cout<<"\n1.insert \n 2.remove \n 3.Display \n 4.exit"<<endl; cout<<"enter your choice"<<endl; cin>>ch; switch(ch) { case 1: cout<<"enter element to insert to the queue"<<endl; cin>>n; obj.insert(n); break; case 2:cout<<"removed element is"<<obj.remove()<<endl; case 3:obj.display(); break; case 4:exit(0); default: cout<<"invalid choice"; } }while(1); }
node* temp=top; while(temp!=0) { cout<<temp->data<<" "; temp=temp->link; } cout<<endl; } } template
r=st.pop(); if(r==-1) cout<<"Element cannot be deleted as stack is empty"<<endl; else cout<<"Deleted element is "<<r<<endl; break; case 3: st.display(); break; } menu2(); cin>>ch; } }
void main() { clrscr(); int ch; menu1(); cin>>ch; if(ch<4) { switch(ch) { case 1: { stack
return -1; else { node
cout<<"2.Delete"<<endl; cout<<"3.Display"<<endl; cout<<"4.display first elemant"<<endl; cout<<"5.display last element"<<endl; cout<<"6.Exit"<<endl; } template
Doubly Linked Lists One shortcoming of singly linked list is we can only move forwards through the list. A doubly linked list is a linked list, which also has pointers from each element to the preceding element. Doubly linked list make manipulation of lists easier.
DEQUE
A double-ended queue is a linear list for which insertions and deletions can occur at either end i.e., deque supports insertion and deletion from the front and back.
The Deque Abstract Data Type insertFirst(e): Insert e at the beginning of deque. insertLast(e): Insert e at end of deque removeFirst(): Removes and returns first element removeLast(): Removes and returns last element
Additionally supported methods include:
To Implement Deque with Doubly Linked Lists we use a doubly linked list with special header and trailer nodes
When implementing a doubly linked list, we add two special nodes to the ends of the lists: the header and trailer nodes.
NOTE : the header and trailer nodes are sentinel or “dummy” nodes because they do not store elements. Here’s a diagram of our doubly linked list:
03.Write C++ program to implement the deque (double ended queue) ADT using a doubly linked list.
#include<iostream.h> #include<conio.h> #include<stdlib.h>
class node { public: int data; class node *next; class node *prev; };
class dqueue: public node { node head,tail; int top1,top2; public: dqueue() { top1=0; top2=0; head=NULL; tail=NULL; } void push(int x){ node *temp; int ch; if(top1+top2 >=5) { cout <<"dqueue overflow"; return ; } if( top1+top2 == 0) { head = new node; head->data=x; head->next=NULL; head->prev=NULL; tail=head; top1++; } else {
void display() { int ch; node *temp; cout <<"display from 1.Staring 2.Ending\n Enter ur choice"; cin >>ch; if(top1+top2 <=0) { cout <<"under flow"; return ; } if (ch==1) { temp=head;
while(temp!=NULL) { cout << temp->data <<" "; temp=temp->next; } } else { temp=tail; while( temp!=NULL) { cout <<temp->data << " "; temp=temp->prev; } } } };
main() { dqueue d1; int ch,x; clrscr(); do { cout <<"\n1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\n "; cout<<"enter your choice"<<endl; cin >>ch; switch(ch) { case 1: cout <<"enter element"; cin >> x;
d1.push(x); break; case 2: d1.pop(); break; case 3: d1.display(); break; case 4: exit(1); } }while(1); }
4. 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. 5.Write C++ programs that use non-recursive functions to traverse the given binary tree in a) Preorder b) inorder and c) postorder.
A tree is a finite set of one or more nodes such that there is a specially designated node called the root and the remaining nodes are partitioned into n0 disjoint sets T 1 , ….,Tn , where each of these sets is a tree. The sets are called the subtrees of the root.
Binary Trees
Binary trees are characterized by the fact that any node can have at most two children; i.e., there is no node with degree greater than two. A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left and right subtrees.
ADT of binary tree
AbstractDataType binaryTree { instances collection of elements; if not empty, the collection is partitioned into a root, left subtree, and right subtree; each subtree is also a binary tree;
operations
empty( ) : return true if the stack is empty, return false otherwise; size( ) : return the number of elements / nodes in the tree
preorder(visit) : preorder traversal of binary tree; visit is the visit function to use; inorder(visit) : inorder traversal of a binary tree