Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data structure throuh cpp lab manual, Thesis of Data Structures and Algorithms

cpp lab manual in c++ data structures

Typology: Thesis

2014/2015

Uploaded on 10/08/2015

Sowmiya.Narayanan
Sowmiya.Narayanan 🇮🇳

4.3

(6)

1 document

1 / 84

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures Through C++ Lab LAB MANUAL
vidya vikas Institute of technology 1
1. Write C++ programs to implement the following using an array.
a) Stack ADT
b) Queue ADT
2. Write C++ programs to implement the following using a singly linked list.
a) Stack ADT
b) Queue ADT
3. Write C++ program to implement the deque (double ended queue) ADT using
a doubly linked list.
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 a C++ program to implement circular queue ADT using an array.
6. Write C++ programs that use non-recursive functions to traverse the given
binary tree in
a) Preorder
b) inorder and
c) postorder.
7. Write a C++ programs for the implementation of bfs and dfs for a given graph.
8. Write C++ programs for implementing the following sorting methods:
a) Quick sort
b) Merge sort
c) Heap sort
9. Write a C++ program to perform the following operations
a) Insertion into a B-tree
b) Deletion from a B-tree
10. Write a C++ program to perform the following operations
a) Insertion into an AVL-tree
b) Deletion from an AVL-tree
11. Write a C++ program to implement Kruskal’s algorithm to generate a minimum
spanning tree.
12. Write a C++ program to implement Prim’s algorithm to generate a minimum
spanning tree.
13. Write a C++ program to implement all the functions of a dictionary (ADT) using
hashing.
Templates
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54

Partial preview of the text

Download Data structure throuh cpp lab manual and more Thesis Data Structures and Algorithms in PDF only on Docsity!

  1. Write C++ programs to implement the following using an array. a) Stack ADT b) Queue ADT
  2. Write C++ programs to implement the following using a singly linked list. a) Stack ADT b) Queue ADT
  3. Write C++ program to implement the deque (double ended queue) ADT using a doubly linked list.
  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 a C++ program to implement circular queue ADT using an array.
  6. Write C++ programs that use non-recursive functions to traverse the given binary tree in a) Preorder b) inorder and c) postorder.
  7. Write a C++ programs for the implementation of bfs and dfs for a given graph.
  8. Write C++ programs for implementing the following sorting methods: a) Quick sort b) Merge sort c) Heap sort
  9. Write a C++ program to perform the following operations a) Insertion into a B-tree b) Deletion from a B-tree
  10. Write a C++ program to perform the following operations a) Insertion into an AVL-tree b) Deletion from an AVL-tree
  11. Write a C++ program to implement Kruskal’s algorithm to generate a minimum spanning tree.
  12. Write a C++ program to implement Prim’s algorithm to generate a minimum spanning tree.
  13. Write a C++ program to implement all the functions of a dictionary (ADT) using hashing.

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 Item maximal(Item a, Item b) { if (a > b) return a; else return b; } The first line is the template prefix , which tells the compiler that Item is a data type that will be filled in later. The "unspecified type" Item is called the template parameter. When a template function is called , the compiler examines the types of the arguments and at that point determines the data type of Item.

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 class stack { T x[100]; int top;

front rear

top E D C

B A

A B C D E

public: stack(); void push(T); T pop(); void display(); }; template stack::stack() { top=-1; } template void stack::push(T n) { if(top==max-1) cout<<"stack is overflow"<<endl; else x[++top]=n; } template T stack::pop() { if(top==-1) { cout<<"stack is empty"<<endl; return 0; } else return x[top--]; } template void stack::display() { int i; if(top==-1) cout<<"stack is empty"<<endl; else { cout<<"\nStack Elements are"<<endl; for(i=top;i>=0;i--) cout<<x[i]<<endl;

} } void main() { stack obj; int ch,n; clrscr(); do

public: queue(); void insert(T); T remove(); void display(); }; template queue::queue() { front=0; rear=-1; } template void queue::insert(T n) { if(rear==max-1) cout<<"queue is overflow"<<endl; else x[++rear]=n; } template T queue::remove() { if(rear<front) { cout<<"queue is empty"<<endl; return 0; } else return x[front++]; } template void queue::display() { int i; if(rear<front) cout<<"queue is empty"<<endl; else { cout<<"queue Elements are"<<endl; for(i=front;i<=rear;i++) cout<<x[i]<<"\t";

} } void main() { queue obj; int ch,n; clrscr();

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 stack::~stack() { node *temp; while(top!=0) { temp=top; top=top->link; delete temp; } } void menu1() { cout<<"1.Interger stack"<<endl; cout<<"2.Float stack"<<endl; cout<<"3.Character stack"<<endl; } void menu2() { cout<<"1.Insert"<<endl; cout<<"2.Delete"<<endl; cout<<"3.Display"<<endl; cout<<"4.Exit"<<endl; } template void stackop(stack st) { int ch; T x,r; menu2(); cin>>ch; while(ch<4) { switch(ch) { case 1: cout<<"Enter element"<<endl; cin>>x; st.push(x); break; case 2:

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 s; stackop(s); break; } case 2: { stack s; stackop(s); break; } case 3: { stack s; stackop(s); break; } default : break; } } getch(); }

return -1; else { node *temp=f; T x=temp->data; f=f->link; return x; } } template void queue ::display() { if(f==0) cout<<"Empty queue"<<endl; else { node *temp=f; while(temp!=0) { cout<<temp->data<<" "; temp=temp->link; } cout<<endl; } } template T queue::first() { if(f==0) return -1; else return f->data; } template T queue::last() { if(f==0) return -1; else return r->data; } void menu1() { cout<<"1.Interger queue"<<endl; cout<<"2.Float queue"<<endl; cout<<"3.Character queue"<<endl; } void menu2() { cout<<"1.Insert"<<endl;

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 void queueop(queue q) { int ch; T x,r; menu2(); cin>>ch; while(ch<6) { switch(ch) { case 1: cout<<"Enter element"<<endl; cin>>x; q.insert(x); break; case 2: r=q.del(); if(r==-1) cout<<"Element cannot be deleted as stack is empty"<<endl; else cout<<"Deleted element is "<<r<<endl; break; case 3: q.display(); break; case 4: r=q.first(); if(r==-1) cout<<"Empty queue"<<endl; else cout<<"first element is"<<r<<endl; break; case 5: r=q.last(); if(r==-1) cout<<"Empty queue"<<endl; else cout<<"last element is"<<r<<endl; break; } menu2(); cin>>ch;

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.

  • The header node goes before the first list element. It has a valid next link but a null prev link.
  • The trailer node goes after the last element. It has a valid prev reference but a null next reference.

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.

TREES

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 n0 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