CS301 P Lab Exercises
CS301 P Lab Exercises
(Practical)
Page
Lab No. Lab Topic
No.
9 Lab 9 : Learn to delete nodes from AVL with the help of rotations 55
13 Lab 13: Learn to a build union tree by using union by size method 67
1|Page
2
Lab 1
Lab 1 Problem Statement
Description:
Write a C++ program to create a Linked List that will store and display the data of faculty members
of a university. The user will enter the data of 5 faculty members (name and age). On the basis of
age, the post of the faculty member will be decided and then this data will be added into the linked
list. Each node of the linked list will contain object of Faculty class in data part and next pointer.
Structure of Classes:
1. Faculty
2. Node
3. List
Faculty Class:
Data Members:
Functions:
Node Class:
Data Members:
2|Page
3
Functions:
Faculty get()
Node * getNext()
List Class:
Data Members:
Member Functions:
List();
void add (Faculty addObject); (To Add the data of faculty member into
the linked list node)
Faculty get();
bool next();
friend void traverse(List list); (To visit each node and display the data of
a faculty members stored in that node)
Solution :
#include <iostream>
class Faculty{
string name;
3|Page
4
string post;
int age;
public:
this->name= name;
this->post= post;
this->age=age;
string getName(){
return name;
string getPost(){
return post;
int getAge(){
return age;
4|Page
5
};
class Node {
public:
Faculty get() {
return object;
this->object.setName(object.getName());
this->object.setPost(object.getPost());
this->object.setAge(object.getAge());
Node * getNext(){
return nextNode;
this->nextNode = nextNode;
private:
Faculty object;
Node * nextNode;
};
5|Page
6
class List {
public:
List();
Faculty get();
bool next();
private:
int size;
Node * headNode;
Node * currentNode;
};
/* Constructor */
List::List() {
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
6|Page
7
newNode->set(addObject);
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode );
currentNode = newNode;
else
newNode->setNext(NULL);
headNode->setNext(newNode);
currentNode = newNode;
size ++;
Faculty List::get() {
7|Page
8
if (currentNode != NULL)
return currentNode->get();
bool List::next() {
currentNode = currentNode->getNext();
return false;
else
return true;
list.currentNode = list.headNode;
cout<<"=============Display Faculty
Information================="<<endl<<endl;
{
8|Page
9
Faculty member;
member=list.get();
list.currentNode = savedCurrentNode;
int main() {
Faculty member;
string name,post;
List list;
int age;
for(int a=0;a<5;a++){
cin>>age;
cin>>name;
post="Lecturer";
9|Page
10
post="Assistant Professor";
post="Professor";
member.setName(name);
member.setAge(age);
member.setPost(post);
list.add(member);
traverse(list);
10 | P a g e
11
Lab 2
Lab Title: Learn to implement stack data structure
Objectives: Get the knowledge of implementing stack data structure using linked list and array
in C++ programming language.
Description:
Write C++ program to create two stacks (one using linked list and one using array). Both stacks will store
the integer values and display these values in reverse order. In stack implemented through linked list, user
can add any number of nodes but in second stack implemented with array, user will enter only 5 numbers.
Structure of Classes:
1- Node
2- Stack_array
3- Stack_linkedlist
Node Class
Stack_array Class
➔ Four private variables (int object, int current, int size, int A [5])
➔ Functions:
int pop_array ()
void push_array (int x)
int isFull ()
int isEmpty ()
Stack_linkedlist Class
11 | P a g e
12
Sample Output:
Note: Numbers must be shown in reverse order only by using pop () method
12 | P a g e
13
Lab 2 Solution
#include <iostream>
class Node{
private:
int object;
Node *nextNode;
public:
};
class stack_array
public:
13 | P a g e
14
int isEmpty(){return ( current == -1 );} // Will return true when stack is empty
int isFull(){ return ( current == size-1);} // Will return true when stack is full
private:
};
class stack_linkedlist{
private:
Node *head;
public:
int pop_list();
};
14 | P a g e
15
newNode->num_set(n);
newNode->setNext(head);
head = newNode;
if(head==NULL){
cout<<"List is empty!"<<endl;
int x =head->num_get();
Node * p = head;
head=head->getNext();
delete p;
cout<<" \n"<< x;
return x;
int main(){
15 | P a g e
16
stack_linkedlist s;
stack_array sa;
while (n < 5) {
cout << "3 : Pop Stack Elements in linked list and display \n";
cout << "4 : Pop Stack Elements in array and display \n";
scanf("%d", &n);
switch (n) {
case 1:
cout << "Enter the total number you want to entered \n";
cin>>k;
cin>>j;
16 | P a g e
17
break;
case 2:
else {
cin>>m;
break;
case 3:
s.pop_list();
break;
case 4:
17 | P a g e
18
else
default :
break;
return 0;
18 | P a g e
19
Lab 3
Lab Title: Learn to implement queue data structure
Objectives: Get the knowledge of implementing queue data structure using linked list in C++
programming language.
Description:
Write a C++ program to create a queue (using linked list), Your program should meet the following
requirements:
1. The queue will store only even numbers and just display those numbers without removing them.
2. Then you have to delete the first even element from the queue and show the remaining numbers of
queue.
Structure of Classes:
4- Node
5- Queue
Node Class
Queue Class
19 | P a g e
20
Sample Output:
Solution
#include<iostream>
class Node{
public:
int object;
Node *nextNode;
20 | P a g e
21
};
class Queue{
public:
Node *front,*rear;
int dequeue();
void display();
int isEmpty();
};
int Queue::isEmpty()
void Queue::enqueue(int x) {
newNode->set(x);
newNode->setNext(NULL);
if(front==NULL){
front=rear=newNode;
else{
rear->setNext(newNode);
21 | P a g e
22
rear=newNode;
void Queue::display(){
if(isEmpty()) {
cout<<"Queue is empty."<<endl;
return;
Node *temp=front;
while(temp != NULL){
cout<<temp->object<<" \n";
temp=temp->nextNode;
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
22 | P a g e
23
int main(){
int n,k;
Queue Q;
cout << "Enter the total numbers you want to enter \n";
cin>>k;
cin>>n;
if(n%2==0)
Q.display();
Q.display();
return 0;
23 | P a g e
24
Lab 4
Lab Title: Learn to implement Binary Search Tree Data Structure
Objectives: Get the knowledge of how to construct a Binary Search Tree and perform basic
functions on a BST in C++ programming language.
Description:
Consider the following string array in which different names are stored. The length of each name is different
from the others.
Write a C++ program to construct a Binary Search Tree which will store the length of each name in its
nodes. For example, the length of “Ali” is 3 so 3 will be inserted in BST node. After the construction of
BST, you are required to print the values of maximum and minimum nodes of Binary Search Tree.
Structure of Program:
24 | P a g e
25
Sample Output:
Solution
#include <iostream>
#include <stdlib.h>
#include <conio.h>
public:
BSTNode() {
this->object = NULL;
this->left = NULL;
this->right = NULL;
};
BSTNode(Object *object ) {
this->object = object;
this->left = NULL;
this->right = NULL;
};
Object *getInfo() {
return this->object;
};
BSTNode *getLeft() {
return left;
};
void setLeft(BSTNode *left) {
this->left = left;
};
BSTNode *getRight() {
26 | P a g e
27
return right;
};
void setRight(BSTNode *right) {
this->right = right;
};
int isLeaf( ) {
if(this->left == NULL && this->right == NULL)
return 1;
return 0;
};
private:
Object *object;
BSTNode *left;
BSTNode *right;
};
27 | P a g e
28
}
else {
b = a->getRight();
}
}
if(*info == *( a->getInfo()) ) {
cout<<endl<<endl<<*info<<"is duplicate in this Binary Search Tree (BST). Can not be inserted .... ";
getch();
delete node;
}
else if( *info < *(a->getInfo()) ) {
a->setLeft(node);
}
else {
a->setRight(node);
}
cout<<endl;
}
28 | P a g e
29
int main() {
string names [] = {"Saleh","Ali","Umar","Musaddiq","Rehman","Hassaan"};
int totalNames = sizeof(names)/sizeof(names[0]);
BSTNode <int> *NTree = new BSTNode<int>();
int treeData[totalNames];
totalNames++;
for(int a=0;a<totalNames;a++){
if(a==6){
treeData[a]=-1;
}
else{
treeData[a]= names[a].length();
}
}
NTree->setInfo( &treeData[0] );
cout << "Inserting the length of names in BST Nodes one by one" <<endl;
cout << "-------------------------------------------------------------" << endl;
cout<<"Length of "<<names[0]<<" is: "<<*NTree->getInfo( )<<endl;
for(int i = 1; treeData[i] > 0; i++) {
cout<<"Length of "<<names[i]<<" is: "<<treeData[i];
insert(NTree, &treeData[i]);
}
cout<<"\nValue of BST minumum node is: "<<minNode(NTree)<<endl;
29 | P a g e
30
30 | P a g e
31
Lab 5
Lab Title: Learn to implement Binary Search Tree Traversals (In-Order,Pre-Order,Post-Order)
Objectives: Get the knowledge of how to implement Binary Search Tree with character data
type and perform different traversal methods on a BST in C++ programming language.
Description:
Write a C++ program that stores the name of a person and build the binary search tree from the
characters of that name. After the construction of the Binary Search Tree, print the characters of name
in pre-order, post-order, and in-order traversal.
Structure of Classes:
6- TreeNode
TreeNode Class
31 | P a g e
32
Non-Member functions
Sample Output:
Solution
#include <iostream>
#include <cstring>
class TreeNode {
public:
// constructors
TreeNode()
32 | P a g e
33
this->object = NULL;
};
TreeNode(Object* object)
this->object = object;
};
Object* getInfo()
return this->object;
};
this->object = object;
};
TreeNode* getLeft()
33 | P a g e
34
return left;
};
this->left = left;
};
TreeNode* getRight()
return right;
};
this->right = right;
};
int isLeaf()
34 | P a g e
35
return 1;
return 0;
};
private:
Object* object;
TreeNode* left;
TreeNode* right;
p = q = root;
p = q;
q = p->getLeft();
else
35 | P a g e
36
q = p->getRight();
if (*info == *(p->getInfo())) {
cout << "attempt to insert duplicate: " << *info << endl;
delete node;
p->setLeft(node);
else
p->setRight(node);
if (treeNode != NULL) {
inorder(treeNode->getLeft());
inorder(treeNode->getRight());
} //End of inOrder.
36 | P a g e
37
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
} //End of preorder
postorder(treeNode->getLeft());
postorder(treeNode->getRight());
int main()
37 | P a g e
38
int length;
cin>>length;
int i;
for(int i=0;i<length;i++)
cin>>word[i];
root->setInfo(&word[0]);
insert(root, &word[i]);
inorder(root);
preorder( root);
postorder( root );
return 0;
} // end of main.
38 | P a g e
39
Lab 6
Lab 6 Problem Statement
Lab Title: Learn to implement function call by value, by reference and by pointer
Description:
Write C++ program to swap the values of two variables of different data types using different
function calls. Your program should swap the values using function call by value, reference and
pointers. The swapping functions should receive the swapping variables as arguments. The
functions should be declared as template functions so that they can entertain any data type.
Structure of Program:
Sample Output:
39 | P a g e
40
Solution
#include <iostream>
template<class T>
T temp = obj1;
obj1 = obj2;
obj2 = temp;
template<class T>
T temp = obj1;
obj1 = obj2;
obj2 = temp;
template<class T>
T temp= *obj1;
*obj1 = *obj2;
*obj2 = temp;
40 | P a g e
41
int main(){
int a=5,b=10;
cout<<"Before Swapping"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
swapping_by_value(a,b);
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"Before Swapping"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
swapping_by_reference(a,b);
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"Before Swapping"<<endl;
cout<<"a = "<<a<<endl;
41 | P a g e
42
cout<<"b = "<<b<<endl;
swapping_by_pointers(&a,&b);
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
return 0;
42 | P a g e
43
Lab 7
Lectures Covered: 19-22
Objectives: Learn to build/draw AVL tree and understand different types of rotations performed
while constructing an AVL tree.
Description:
You are required to construct AVL tree from the following data:
Solution:
43 | P a g e
44
44 | P a g e
45
45 | P a g e
46
46 | P a g e
47
47 | P a g e
48
48 | P a g e
49
49 | P a g e
50
50 | P a g e
51
51 | P a g e
52
52 | P a g e
53
53 | P a g e
54
Lab 8
Lab Title: Learn to draw AVLtree
Objectives: Learn to build/draw AVL tree and understand different types of rotations
Description:
Build AVL tree from the given Data: 3 5 6 7 9 10 11 21 20 18 19
Note: You have to show only final AVL tree after insertion of each node value.
54 | P a g e
55
Lab 9
Lab Title: Learn to delete nodes from AVL tree
Objectives: Learn to delete nodes from AVL with the help of rotations
Description:
Delete the node 8,7,11,14,17 from given AVL tree and perform necessary rotation(s) to balance
the tree after deletion of each node. Show final tree after each deletion.
Solution
Delete Node 8
55 | P a g e
56
No effect on tree after Node 8 deletion, the tree is balanced. Now we have to delete Node 7
After deleting node 7 the node 4 becomes left child of node 11 and tree is still balanced after deletion
Now we have to delete node 11. The following will be the resultant tree after node 11 Deletion , Node
4 has replaced the node 11
56 | P a g e
57
Now if we check the balance after deletion we find that tree is not balanced . Node 4 is unbalanced and
hence rotation is required . Here we will apply Left rotation
Now we have to delete node 14 . After deleting Node 14 it will be replaced by node node 13. Node 13
will now be the parent node. The tree is balanced after deletion of Node 14
57 | P a g e
58
Now we have to make the final deletion I.e. Node 17. After deletion of Node 17 it will be replaced by
Node 16 . This is our final tree
58 | P a g e
59
Lab 10
Lab Title: Learn to build frequency table and Huffman encoding tree
Objectives: Get the knowledge of building frequency table and Huffman encoding tree.
Description:
Consider the message “the clouds are dark and its about to rain” and construct frequency
table and Huffman encoding tree.
Frequency Table:
a 5 n 2
b 1 o 3
c 1 r 3
d 3 s 2
e 2 t 4
h 1 u 2
i 2 SP 8
k 1 NL 1
l 1
59 | P a g e
60
0 4
1
1 2
0
1 0 1
7 7 1 1
0 1 0 1 0 0
1 1
r 4 o 4 S 7 a 8
0 1 0 1 1 0 1
0
s 2 n 2 d 4 t 4
0 1 0 1 0 1
0 1
b c h k i 2
u e
0 1
l N
60 | P a g e
61
Lab 11
Lab Title: Learn to implement min heap using insert( ) method
Objectives: Get the knowledge of implementing min heap using insert( ) method with the help
of C++ programming language.
Description:
Consider the Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29 and write the C++
code to construct min heap using insert method.
Solution:
#include <iostream>
class Heap {
public:
bool isFull();
void traverse();
public:
int capacity;
};
61 | P a g e
62
currentSize = 0;
if(isFull()) {
return 0;
array[hole]= array[hole/2];
array[hole] = x;
void Heap::traverse() {
bool Heap::isEmpty() {
return currentSize == 0;
bool Heap::isFull() {
62 | P a g e
63
return currentSize==capacity;
main() {
int size = 16, arr[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
Heap heap(size);
heap.traverse();
63 | P a g e
64
Lab 12
Lab Title: Learn to implement min heap using buildHeap( ) method
Objectives: Get the knowledge of implementing min heap using buildHeap( ) and
perculateDown( ) methods with the help of C++ programming language.
Description:
Consider the Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29 and write the C++
code to construct min heap using buildHeap( ) method.
#include <iostream>
Solution
class Heap {
public:
bool isFull();
void traverse();
public:
int capacity;
64 | P a g e
65
};
currentSize = 0;
int child;
child = hole*2;
child++;
if (array[child]< temp)
array[hole] = array[child];
else break;
array[hole] = temp;
65 | P a g e
66
array[i] = anArray[i-1];
currentSize = n;
percolateDown(i);
void Heap::traverse() {
bool Heap::isEmpty() {
return currentSize == 0;
bool Heap::isFull() {
return currentSize==capacity;
main() {
int size = 16, arr[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
Heap heap(size);
heap.buildHeap(&arr[0],size);
heap.traverse();
66 | P a g e
67
Lab 13
Lab Title: Learn to a build union tree by using union by size method
Objectives: Get the knowledge of building union tree of any given data with the help of union
by size method.
Description:
Consider the following set of elements,
Apply the following sequence of union commands on the above set of elements and draw the
tree by using the union by size method. Show the resultant tree only. Also, update the given
array by the union of size method.
• union (2,6)
• union (1,3),
• union (4,2)
• union (1,2)
• union (5,2)
Solution:
• union (2,6)
67 | P a g e
68
-1 -2 -1 -1 -1 2 -1 -1
1 2 3 4 5 6 7 8
• union (1,3)
-2 -2 1 -1 -1 2 -1 -1
1 2 3 4 5 6 7 8
union (4,2)
4 6
-2 -3 1 2 -1 2 -1 -1
68 | P a g e
69
1 2 3 4 5 6 7 8
• union (1,2)
4 6 3
2 -5 1 2 -1 2 -1 -1
1 2 3 4 5 6 7 8
• union (5,2)
2
5
1
4 6 3
2 -6 1 2 2 2 -1 -1
1 2 3 4 5 6 7 8
69 | P a g e
70
Lab 14
Lab 14 (Lectures Covered 38-40)
Description:
Write a program in C++ language to find a number (element) from an array using binary search
algorithm. Your program should start search from the first element of array and it should search
every third element of the array.
You can use 18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97 as data of array.
Solution :
#include <iostream>
if (arr[mid] == val)
return 1;
low = mid + 1;
else
high = mid - 1;
return 0;
}
70 | P a g e
71
int main() {
int size = 14, arr[size] = {18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97};
for(int i=0;i<=13;i+=3){
if(result == 1)
else
return 0;
71 | P a g e
72
Lab 15
Lab Title: Build hash table using linear probing collision resolution technique
Objectives: Learn to build Hash table using linear probing technique to resolve collision
Tool: MS Word
Description:
Insert the values 1, 2, 9, 11,7 into the following table using hash function (2x + 3)mod8. You
have to apply linear probing technique to resolve collision.
Solution:
0
1 11
2 7
5 1
6 9
7 2
72 | P a g e
73
Lab 16
Lab Title: Learn to sort array using elementary sorting algorithms.
Objectives: Get the knowledge of sorting array using Selection Insertion, and Bubble sort
algorithm.
Tool: MS Word
Description:
Consider the data given below as an array and sort by implementing selection, insertion, and
bubble sort algorithm.
You are required to sort the given data in ascending order using selection sort.
8 5 10 3 1
0 1 2 3 4
Solution:
Step 1:
1 5 10 3 8
0 1 2 3 4
Step 2:
1 3 10 5 8
0 1 2 3 4
Step 3:
1 3 5 10 8
0 1 2 3 4
Step 4:
1 3 5 8 10
73 | P a g e
74
0 1 2 3 4
You are required to sort the given data in ascending order using insertion sort algorithm
10 4 8 16 11
Solution:
List: 10 4 8 16 11
Step1: 4 10 8 16 11
Step2: 4 8 10 16 11
Step3: 4 8 10 16 11
Step4: 4 8 10 11 16
0 1 2 3 4
You are required to sort the above data in ascending order using Bubble sort..
Solution:
1st iteration:
Step 1:
6 1 3 2 9
0 1 2 3 4
Step 2:
1 6 3 2 9
0 1 2 3 4
74 | P a g e
75
Step 3:
1 3 6 2 9
0 1 2 3 4
Step 4:
1 3 2 6 9
0 1 2 3 4
Step 5:
1 3 2 6 9
0 1 2 3 4
2nd iteration:
Step 1:
1 3 2 6 9
0 1 2 3 4
Step 2:
1 3 2 6 9
0 1 2 3 4
Step 3:
1 2 3 6 9
0 1 2 3 4
Step 4:
1 2 3 6 9
0 1 2 3 4
Step 5:
1 2 3 6 9
75 | P a g e
76
0 1 2 3 4
76 | P a g e