Professional Documents
Culture Documents
Lab Manual CS301
Lab Manual CS301
1|Page
Lab 1
Lab Title: Learn to implement linked list data structure
Objectives: Get the knowledge of implementing linked list data structure using C++
programming language.
Description:
Write the C++ code to implement the List class. You need to write only add( ). remove( ), and
find( ) functions of the list class.
2|Page
Lab 2
Lab Title: Learn to implement stack data structure using array
Objectives: Get the knowledge of implementing stack list data structure using array in C++
programming language
Description:
Write a C++ program to implement data structure Stack. You have to write all the stack
functions and using those functions insert the numbers 1 to 10 into the stack. The stack should be
able to show the error messages if the push( ) operation is initiated but the stack is full or the
pop( ) operation is initiated but the stack is empty.
3|Page
Lab 3
Lab Title: Learn to implement queue data structure using link list
Objectives: Get the knowledge of implementing queue data structure using link list in C++
programming language.
Description:
Write the C++ Code for the enqueue( ), dequeue( ), front( ) and the rear( ) functions of the class
queue.
4|Page
Lab 4
Lab Title: Learn to draw binary search tree and implement in-order traversal
Objectives: Learn to draw binary search tree and perform in-order traversal using C++
programming language.
Description:
Task 1:
Task 2:
Write C++ code that will perform inOrder traversal on the above given BST.
#include <iostream>
#include "TreeNode.cpp"
#include <queue>
void inOrder(TreeNode<int>* );
5|Page
int main()
root->set(&Array[0]);
insert(root, &Array[i]);
inOrder( root );
} // end of main
inOrder(treeNode->getLeft());
inOrder(treeNode->getRight());
} //End of inOrder.
6|Page
Lab 5
Lab Title: Learn to implement binary search tree for string type data
Objectives: Get the knowledge of implementing binary search tree for string data type using
C++ programming language.
Description:
Write C++ code that will draw BST using string type of data:
#include <iostream>
#include <cstring>
#include "Template_BSTNode.cpp"
#include "Template_Queue_LinkedList.cpp"
void inOrderTraverse(BSTNode<T> * );
void preOrderTraverse(BSTNode<T> * );
7|Page
template <typename T>
void postOrderTraverse(BSTNode<T> * );
void levelOrderTraverse(BSTNode<T> * );
int main()
"abash","accuse","economy","adhere","advise","cease","debunk","feeder","genius","fetch","chai
n", NULL};
insertChar(Root, x[i]);
cout<< endl << endl<< "the inOrder BST is ... " << endl;
inOrderTraverse(Root);
cout<< endl << endl<< "the preOrder BST is ... " << endl;
preOrderTraverse(Root);
cout<< endl<< endl << "the postOrder BST is ... " << endl;
postOrderTraverse(Root);
cout<< endl << endl<< "the levelOrder BST is ... " << endl;
levelOrderTraverse(Root);
8|Page
} // end of main.
tempRoot = tempLeaf;
if(x< tempRoot->getData())
tempLeaf = tempRoot->getLeft();
else
tempLeaf = tempRoot->getRight();
} // end of while , this loops only break when x = data hold by current node or it is leaf;
if(x == tempRoot->getData())
delete newNode;
tempRoot->setLeft(newNode);
9|Page
else
tempRoot->setRight(newNode);
tempRoot = tempLeaf;
if(strcmp(x, tempRoot->getData())<0 )
tempLeaf = tempRoot->getLeft();
else
tempLeaf = tempRoot->getRight();
} // end of while , this loops only break when x = data hold by current node or it is leaf;
if(strcmp(x, tempRoot->getData())==0 )
delete newNode;
10 | P a g e
}
tempRoot->setLeft(newNode);
else
tempRoot->setRight(newNode);
if(node!=NULL)
inOrderTraverse(node->getRight() );
if(node!=NULL)
preOrderTraverse(node->getRight() );
if(node!=NULL)
postOrderTraverse(node->getRight() );
myQueue.push(node);
while (!myQueue.isEmpty())
node = myQueue.peak();
12 | P a g e
myQueue.pop();
if(node->getLeft() != NULL)
myQueue.push(node->getLeft());
if(node->getRight() != NULL)
myQueue.push(node->getRight());
system("pause");
13 | P a g e
Lab 6
Lab Title: Learn to implement function call by value, reference and pointer
Objectives: Get the knowledge of implementing function calls for call by value, call by
reference and call by pointer.
Description:
Write C++ program that will demonstrate how the value in a caller function is affected when it is
passed to a function by using call by value, by using pointers and by using call by reference
methods.
#include <iostream.h>
//Function 1, call by value
int intMinus1( int oldVal)
{
oldVal = oldVal – 1;
return oldVal;
}
// Function 2, call by using pointers
int intMinus2( int* oldVal)
{
*oldVal = *oldVal – 2;
return *oldVal;
}
// Function 3, call by reference
int intMinus3( int& oldVal)
{
oldVal = oldVal – 3;
return oldVal;
}
void main ()
{
int myInt = 31;
int retVal;
retVal = intMinus1( myInt ); //call by value
cout << “After returning from the called function intMinus1” << endl ;
cout << ”The value returned by the called function (retVal) is : ” << retVal ;
cout << endl ;
cout << ”The value of the calling function’s variable (myInt) is : ” << myInt ;
cout << endl << endl;
14 | P a g e
// now pass the argument by using pointer, also initialize the value of myInt
myInt = 31 ;
retVal = intMinus2( &myInt ); //call by passing a pointer
cout << “After returning from the called function intMinus2” << endl;
cout << ”The value returned by the called function (retVal) is : ” << retVal ;
cout << endl;
cout << ”The value of the calling function’s variable (myInt) is : ” << myInt ;
cout << endl << endl;
// now pass the argument by as reference, also initialize the value of myInt
myInt = 31 ;
retVal = intMinus3( myInt ); //call by passing a reference
cout << “After returning from the called function intMinus3” << endl;
cout << ”The value returned by the called function (retVal) is : ” << retVal ;
cout << endl;
cout << ”The value of the calling function’s variable (myInt) is : ” << myInt ;
}
15 | P a g e
Lab 7
Lab Title: Learn to delete nodes from BST
Description:
Draw Binary Search Tree from the given data. Also draw Binary Search Tree after removal of
value ‘9’.
9 4 6 17 2 8 4 15 41 47 29
16 | P a g e
BST after removal of ‘9’.
17 | P a g e
OR
18 | P a g e
Lab 8
Lab Title: Learn to draw AVL
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.
19 | P a g e
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 14, 4 and 10 from given AVL three and perform necessary rotation to balance
the tree after deletion of each node.
8 8
3 10 3 10
1 6 14 1 6
7 4 7
4
20 | P a g e
8
6
6 10
3 8
3 7
1 4 7 10
1 4
6 6
3 8 3 8
4 7 10 4 7
21 | P a g e
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
22 | P a g e
4
0 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
23 | P a g e
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.
#include <iostream>
class Heap {
public:
bool isFull();
void traverse();
public:
int capacity;
};
24 | P a g e
Heap::Heap (int capacity) {
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() {
25 | P a g e
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();
26 | P a g e
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>
class Heap {
public:
bool isFull();
void traverse();
public:
int capacity;
27 | P a g e
};
currentSize = 0;
int child;
child = hole*2;
child++;
if (array[child]< temp)
array[hole] = array[child];
else break;
array[hole] = temp;
array[i] = anArray[i-1];
28 | P a g e
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();
29 | P a g e
Lab 13
Lab Title: Learn to build union tree
Objectives: Get the knowledge of building union tree of any given data.
Description:
Consider the following sequence of union commands on the set of elements {1, 2, 3, 4, 5, 6, 7, 8,
9, 10}.
4 1 5
2 7 6
3 8
30 | P a g e
Lab 14
Lab Title: Learn to implement binary search algorithm
Objectives: Get the knowledge of implementing binary search algorithm using C++
programming language.
Description:
Write a program in C++ language to find a number (element) from an array using binary search
algorithm.
You can use 18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97 as data of array.
#include <iostream>
if (r >= l)
if (arr[mid] == num)
return mid;
31 | P a g e
return binarySearch(arr, mid+1, r, num);
return -1;
main() {
int size = 14, arr[size] = {18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97};
if(result == -1){
else{
32 | P a g e
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:
Consider the data given below and build a Hash table using linear probing technique to resolve
collision.
Data 18, 20, 23, 31, 37, 42, 47, 51, 79, 82, 85, 94, 96, 97
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
F(x) 20 97 42 3 82 85 47 11 51 94 96 37 18 79
33 | P a g e
Lab 16
Lab Title: Learn to sort array using bubble sort algorithm
Objectives: Get the knowledge of sorting array using bubble sort algorithm.
Description:
Consider the data given below as an array and sort by implementing bubble sort algorithm using
C++ language.
Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29
#include <iostream>
int swapped = 1;
swapped = 0;
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
swapped = i;
34 | P a g e
}
bound = swapped;
main() {
int size = 16, arr[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
bubbleSort(arr, size);
cout<<arr[i]<<"\t";
Students and teacher communicate through Skype/Adobe Connect. Students perform the task
using the recommended tool given in each lab.
35 | P a g e