Data Structures Lab Manual
Data Structures Lab Manual
LAB MANUAL
Session 2022-2026
Sr. # Content
1. Objectives
2. Outcomes
3. Recommended System / Software
Requirements
4. Text Books
5. References
6. Weekly Plan
7. Lab Work details
Prepared by:
Text Book:
Data Structures using C++ by C M Aslam, Saeed Ahmad, Aqsa Aslam and Atif Mansoor.
Refereces :
1. Data Abstraction and Problem Solving with C++, 2nd ed, Frank M. Carrano, Paul Helman, Robert
Veroff, Addison-Wesley, 1998.
2. Data Structures and Algorithms (SAMS teach yourself), Lafore, Sams Publishing, 1999.
3. Fundamentals of Data Structures in C++, Horowitz, Sahni, and Mehta, Computer Science Press, 1995.
4. Data Structures in JAVA, Standish, Addison Wesley, 2000.
Weekly Plan
Week Program
1 Write C++ programs to implement i) Linear search ii) Binary search.
2 Write a C++ program to implement Hashing.
3 Write C++ programs to implement i) Selection Sort ii) Bubble Sort
4 Write C++ a program to implement Insertion Sort
5 Write C++ programs to implement i) Shell Sort. ii) Radix Sort.
6 Write C++ program to convert Decimal Number to Binary using stack.
7 Write C++ programs to convert infix expression to postfix and also prefix notation.
8 Write C++ program for tower of Hanoi using recursion.
9 Write C++ program to implement Queue
10 Write C++ program to implement circular Queue
11 Write C++ program to implement deque (double ended queue).
12 Write C++ programs to implement i) Merge Sort ii) Quick Sort. iii) Bucket Sort.
13 Write C++ program to implement list ADT to perform following operations a) Insert an element
into a linked list. b) Delete an element from list c) Search for a key element in list d)count number
of nodes in list.
14 Write C++ programs to implement doubly & circular linked list.
15 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
16 Project
Week 1
Write C++ programs to implement i) Linear search ii) Binary search.
i) Linear Search:
// C++ code to linearly search
// x in arr[]. If x is present
// then return its location,
// otherwise return -1
#include <iostream>
using namespace std;
// Driver code
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, n, x);
(result == -1) ?
cout << "Element is not present in array" :
cout << "Element is present at index " <<
result;
return 0;
}
ii) Binary Search
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
if(result == -1) cout << "Element is not present in array";
else cout << "Element is present at index " << result;
return 0;
}
Week 2:
Write a C++ program to implement Hashing.
Week 3:
Write C++ programs to implement i) Selection Sort ii) Bubble Sort.
i) Selection Sort
// C++ program for implementation of
// selection sort
#include <bits/stdc++.h>
using namespace std;
// Driver program
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
Week 4
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Week 5
Write C++ programs to implement i) Shell Sort. ii) Radix Sort.
i) Shell Sort:
// C++ implementation of Shell Sort
#include <iostream>
using namespace std;
int main()
{
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);
shellSort(arr, n);
return 0;
}
Radix Sort:
// C++ implementation of Radix Sort
#include <iostream>
using namespace std;
// Output array
int output[n];
int i, count[10] = { 0 };
// Driver Code
int main()
{
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
radixsort(arr, n);
print(arr, n);
return 0;
}
Week 6
Write C++ program to convert Decimal Number to Binary using stack.
#include <siostream.h>
#include <conio.h>
class stack
{
private: int top;
int stk (15];
public:
stack(void) {top = -1;}
// Prototypes of the functions of the class
void bin (int x):
void display(void);
}
Note: See text book and Implement program 5.2.
Week 7
Write C++ programs to convert infix expression to postfix and also prefix notation.
infix expression to postfix
#include <jostream.h>
#include <conio.h>
#Include <ctype.h>
int priority(char); // prototype of function to check precedence of operator
class infix postfix
{
private:
char stack|20]:
int top;
public:
infix_ postfix() { top = -1; }
void scan(char [] );
void push(char);
char pop(void);
}
Note: See text book and Implement program 5.4.
infix expression to prefix
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
class infix_prefix
{
private:
char stack[20];
int top;
public:
infix_prefix() { top = - 1;}
void scan(char []):
void push(char);
char pop(void):
};
void main(void)
infix prefix obj;
clrscr();
char exp[100], rev_exp[100];
cout<<"Enter infix expression without spaces: *;
cin>>exp:
//convert infix expression in reverse order
Strpy(rev_exp, reverse(exp));
obj scan(rev_oxp):
getch():
} // end of main()
Note: See text book and Implement program 5.5.
Week 8
Write C++ program for tower of Hanoi using recursion.
#include<iostream.h>
#include<conio.h>
//Tower of Hanoi function implementation
void TOH(int n, char Sour, char Aux, char Des)
{
if(n==1)
{
cout<<"Move Disk "<<n<<" from "<<Sour<<" to *<<Des<<endl;
return;
}
TOH(n-1,Sour,Des,Aux):
cout<<Move Disk "«<n<< from "<<Sour<<* to *<<Des<<endi;
TOH(n-1,Aux,Sour, Des);
}
void main(void)
{
int n;
clrscr();
coul<<"Enter no. of disks:";
cin>>n;
//calling the TOH
TOH(n,’A’,’B’,’C’);
getch();
}
Week 9
Write C++ program to implement Queue
// Driver code
int main()
{
Queue* queue = createQueue(1000);
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
return 0;
}
Note: See text book and Implement program 7.1.
Week 10
Write C++ program to implement circular Queue
class Queue
{
// Initialize front and rear
int rear, front;
// Circular Queue
int size;
int *arr;
public:
Queue(int s)
{
front = rear = -1;
size = s;
arr = new int[s];
}
else
{
rear++;
arr[rear] = value;
}
}
return data;
}
q.displayQueue();
q.enQueue(9);
q.enQueue(20);
q.enQueue(5);
q.displayQueue();
q.enQueue(20);
return 0;
}
Note: See text book and Implement program 7.2.
Week 11
Write C++ program to implement deque (double ended queue).
public:
Deque(int size)
{
front = -1;
rear = 0;
this->size = size;
}
// Operations on Deque:
void insertfront(int key);
void insertrear(int key);
void deletefront();
void deleterear();
bool isFull();
bool isEmpty();
int getFront();
int getRear();
};
// Driver code
int main()
{
Deque dq(5);
// Function calls
cout << "Insert element at rear end : 5 \n";
dq.insertrear(5);
dq.deleterear();
cout << "After delete rear element new rear"
<< " become " << dq.getRear() << endl;
dq.deletefront();
cout << "After delete front element new "
<< "front become " << dq.getFront() << endl;
return 0;
}
Note: See text book and Implement program 7.3.
Week 12
Write C++ programs to implement i) Merge Sort ii) Quick Sort. iii) Bucket Sort.
i) Merge Sort
#include siostream.h>
#include < conlo.h>
void MergeSort(Int 0, int, int);
void Merge(int 0, Int, int, int);
void main(void )
{
int i, arr[] = {12, 11, 13, 5, 6, 7};
clrscr();
cout<<"The given array before sorting”<<end;
for(i = 0; 1 < 6; |++)
cout<<arrij[];
coul<<endl;
MorgeSort(arr, 0, 5);
coul<<"Array after sorting" <<endl;
for(i = 0; 1 < 6; 1++)
cout<<arr[l];
getch():
} end of main()
Note: See text book and Implement program 8.12.
ii) Quick Sort
include <conio.h>
include <iostream.h>
quicksort(int 0, Int, Int );
int partition(int 0, int, int);
void main (void)
{
int i, arr[] = {2, 15, 1, 61, 11, 47, 8};
cout<<"Array before sorting"<<endl;
for(i = 0; i<=6; i++)
cout<<arr[i]<< "\t";
cout<<endl;
quicksort(arr, 0, 6);
cout<<"Array after sorting"<<endl;
for(i = 0; i<=6; i++)
couts<ar[i]<<"\t";
cout<<end;
getch();
} //end of main()
Note: See text book and Implement program 8.13.
iii) Bucket Sort
#include <conio.h>
#nclude <jostream.h>
#include <math.h>
class bucket_sort
{
private:
int ar[10];
int BO[10], B1[10], B2[10], B3[10], B4[10];
int counter[5];
public:
void input(void);
int largest(void);
vold distribute_sort(int);
void sort(int [], int);
void concatenate(void);
void display(void);
}
void main(void)
{
bucket_sort obj;
int large;
clrscr();
obj.input();
large = obj.largest();
obj.distribute_sort(large);
obj. concatenate();
obj.display();
getch();
} //end of main()
Note: See text book and Implement program 8.15.
Week 13
Write C++ program to implement list ADT to perform following operations a) Insert an element into a
linked list. b) Delete an element from list c) Search for a key element in list d) count number of nodes
in list.
#include <iostream.h>
#include <string.h>
#include <conio.h>
{
struct node
float data;
node *link;
};
class list
{
private:
node *start;
public:
list(void)
{
start = NULL;
}
void insert_begin(void):
void insert_end(void);
vold insert_location(vold):,
void delete_begin(vold);
void delete_end(vold);.
vold delete_location(void);
void search(void);
void display(void);
Int count(vold);
void bubble_sort(void);
};
Note: See text book and Implement program 9.2.
Week 14
Write C++ programs to implement doubly & circular linked list.
#include <iostream.h>
#include <conio.h>
struct node
{
Node *previous;
int data;
node *next;
};
Class list
{
private:
node *start, *current, *temp;
public:
list()
{ start = NULL; }
void add_item(int);
void display(void);
};
void main (void)
{
list obj;
int val;
clrscr();
cout<-"Enter five values \n";
for(int i = 1; I<=5; i++)
{
Cin>val;
Obj.add_item(val);
}
Obj.display();
getch();
}
Note: See text book and Implement program 9.3.
Circular linked list:
#include <iostream.h>
#include <conio.h>
struct node
{
int n;
node *link;
};
Class list
{
private:
Node *S, *C, *P;
public:
List(void)
{
S=NULL;
}
void insert(int);
void display(void);
};
Week 15
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
#include<iostream.h>
#include<conio.h>
Struct node
{
int data;
node *right;
node *left;
};
class bst
{
private:
node *start, *cur, *temp, *parent;
public:
bst(void)
{
start = NULL;
}
void create(int);
void delete_node(void);
void delete_leaf_node(node*, int);
void delete_onechild_node(node*, int);
void delete_twochild_node(node*, int);
void preorder(void);
void preorder(node*);
};
Note: See text book and Implement program 10.9.
c) Search for a key element in a binary search tree
#include<iostream.h>
#include<conio.h>
Struct node
{
int data;
node *right;
node *left;
};
class bst
{
private:
node *start, *cur, *temp, *parent;
public:
bst(void)
{
start = NULL;
}
void create(int);
node* search(node*, int);
};
Note: See text book and Implement program 10.2.
SEMESTER PROJECT
Security Lock
In the bank, a security lock is used to access some rooms. This lock accepts two inputs: the employee
identification number (16 bits) and his/her password (4 bits). If the bank has 20 employees, construct their
database and store it in the memory. Then write a program to access these rooms.
The inputs of the program are the employee identification and the password.