DS Lab Manual CD 303
DS Lab Manual CD 303
of
“Data Structures (CD 303)”
Submitted by
Name:
Enrolment No.:
Semester:
Session:
Submitted to:
Contents
Program Outcomes
Course Outcomes
List of Experiment
Vission & Mission of the Institute
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools, including prediction and modeling to complex
engineering activities, with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
Program Educational Objectives
1. To impart in depth concepts of the subject in terms of both theoretical and practical aspects
to achieve excellent University results.
2. To produce technically sound engineering graduates who would fulfil the latest requirements
of computer science and IT industry at modest cost with the caliber of solving intricacies of
deeper programming concepts.
3. To inculcate the lessons of communication skills, teamwork spirit and professional attitude
to the budding engineering graduates.
4. In order to get versatile growth of students, participation of students in extracurricular
activities is also made compulsory; and also to develop ethical attitudes in the graduates so
that they can also become good citizens of the nation.
5. To develop leadership and entrepreneurship qualities in budding graduates so that they will
cherish and nourish the society and the nation with modern trends of digitization and
blossom it with their unparalleled technical expertise.
Course Outcomes
CO1: Design, implement, and manipulate fundamental data structures such as arrays,
linked lists, stacks, queues, trees and graphs.
CO2: Develop the ability to analyze problems and choose appropriate data structures
and algorithms for efficient solutions, emphasizing algorithmic complexity and
optimization.
CO3: Gain expertise in memory management and understand the impact of data
structure choises on resource utilization, enabling efficient handling of large datasets
and optimizing program performance.
CO5: Cultivate analytical thinking to solve complex problems through the application
of various data structures, fostering a deep understanding of algorithmic paradigms and
their practical implications.
General Regulatory & Safety Rules
The following Regulations and Safety Rules must be observed in all concerned laboratory
locations.
1. It is the duty of all concerned parties who use any electrical laboratory to take all
reasonable steps to safeguard the HEALTH and SAFETY of themselves and all other
users and visitors.
2. Make sure that all equipment is properly working before using them for laboratory
exercises. Any defective equipment must be reported immediately to the Lab.
Instructors or Lab Technical Staff.
3. Students are allowed to use only the equipment provided in the experiment manual or
equipment used for senior project laboratory.
4. Power supply terminals connected to any circuit are only energized with the presence
of the Instructor or Lab. Staff.
5. Students should keep a safe distance from the circuit breakers, electric circuits or any
moving parts during the experiment.
6. Avoid any part of your body to be connected to the energized circuit and ground.
7. Switch off the equipment and disconnect the power supplies from the circuit before
leaving the laboratory.
8. Observe cleanliness and proper laboratory housekeeping of the equipment and other
related accessories.
9. Wear proper clothes and safety gloves or goggles required in working areas that
involves fabrications of printed circuit boards, chemicals process control system,
antenna communication equipment and laser facility laboratories.
10. Double check your circuit connections specifically in handling electrical power
machines,AC motors and generators before switching “ON” the power supply.
11. Make sure that the last connection to be made in your circuit is the power supply and
first thing to be disconnected is also the power supply.
12. Equipment should not be removed, transferred to any location without permission from
the laboratory staff.
13. Software installation in any computer laboratory is not allowed without the permission
from the Laboratory Staff.
14. Computer games are strictly prohibited in the computer laboratory.
15. Students are not allowed to use any equipment without proper orientation and actual
hands on equipment operation.
List of Experiments
#include <iostream>
using namespace std;
int main() {
int arr[] = {4,8,10,14,21,22,36,62,77,81,91};
int size = sizeof(arr)/sizeof(arr[0]);
int result = BinarySearch(arr, 62, 0, size-1);
if(result!=-1)
cout << "Binary Search: Found at index " << result <<
endl;
else
cout << "Binary Search: Not Found\n";
Output
Program 02
Write a C++ program to sort an array using :
(a) Merge Sort
(b) Quick Sort.
#include <iostream>
using namespace std;
while(i<j) {
while(i <= high && arr[i] <= pivot) i++;
while(j >= low && arr[j] > pivot) j--;
if(i<j) {
swap(arr[i], arr[j]);
}
}
swap(arr[low], arr[j]);
return j;
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
++i;
} else {
arr[k] = R[j];
++j;
}
++k;
}
delete[] L;
delete[] R;
}
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int arr_size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Output
Program 03
Write a C++ code to insert to and delete node from a Singly
Linked List.
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
int main()
{
insertAtBeginning(head,3);
insertAtBeginning(head,4);
insertAtBeginning(head,5);
insertAtPosition(head,2,10);
insertAtPosition(head,22,12);
show(head);
deleteNode(head,2);
show(head);
appendNode(head,2);
appendNode(head,1);
show(head);
return 0;
}
Output
Program 04
Write a C++ code to insert to and delete node from a Doubly
Linked List.
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;
};
void printFwd()
{
Node *ptr = head;
if(ptr==NULL)
{
cout << "Linked list is empty\n";
return;
}
while(ptr!=NULL)
{
cout << ptr->data << " | ";
ptr = ptr->next;
}
cout << endl;
}
if(ptr->prev == nullptr)
{
head = head->next;
delete ptr;
head->prev = nullptr;
return;
}
if(ptr->next == nullptr)
{
tail = tail->prev;
delete ptr;
tail->next = nullptr;
return;
}
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
delete ptr;
}
int main()
{
int arr[] = {10,20,30,40,50};
for(int i=0;i<5;i++)
{
appendNode(arr[i]);
}
printFwd();
insertNode(100,7);
printFwd();
deleteNode(6);
printFwd();
insertNode(45,10);
printFwd();
}
Output
Program 05
Write a C++ code to implement a Stack using an array.
#include <iostream>
using namespace std;
#define SIZE 10
pop(&s);pop(&s);pop(&s);
top(&s);
}
Output
Program 06
Write a C++ code to implement a Queue using an array.
#include <iostream>
using namespace std;
dq(&x);
Output
Program 07
Write a C++ code to implement a Queue using Singly Linked List.
#include <iostream>
using namespace std;
int dq() {
if(front==NULL) {
return -1;
}
Node *temp = front;
int x = temp->data;
front = front->next;
free(temp);
return x;
}
void display() {
Node *temp = front;
while(temp!=NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
nq(10);
nq(20);
nq(30);
nq(40);
display();
cout << dq() << " ";
cout << dq() << endl;
display();
}
Output
Program 08
Write a C++ code to convert an infix expression into a postfix
expression using a stack.
#include <iostream>
#include <stack>
#include <vector>
#include <cctype>
int main()
{
string infix = "A/(B+(C-D))*E";
vector<char> postfix;
stack<char> s;
for(int i=0;i<infix.length();i++)
{
char curr = infix[i];
if(isalnum(curr))
{
postfix.push_back(curr);
} else {
if(curr == '(') {
s.push(curr);
} else if(curr == ')') {
while(!s.empty() && s.top()!='(') {
postfix.push_back(s.top());
s.pop();
}
s.pop();
} else {
while(!s.empty() && preced(curr) <=
preced(s.top())) {
if(curr=='^' && s.top()=='^')
break;
postfix.push_back(s.top());
s.pop();
}
s.push(curr);
}
}
}
while(!s.empty()) {
postfix.push_back(s.top());
s.pop();
}
for(auto x:postfix)
cout << x << " ";
}
Output
Program 09
Write a C++ code to add integers into a Binary Search Tree. Also
write functions to traverse BST using
Pre-order, In-order and Post-order traversal techniques.
#include <iostream>
using namespace std;
struct Node {
Node* left;
Node* right;
int data;
};
int main()
{
Node *root = NULL;
int arr[] = {35,40,50,25,20,45,15};
int size = sizeof(arr)/sizeof(arr[0]);
root = insert(root, arr[0]);
for(int i=1;i<size;i++) {
insert(root, arr[i]);
}
Output
Program 10
Write a C++ code to traverse a Graph using :
(a) Depth First Search (DFS)
(b) Breadth First Search (BFS)
#include <stdio.h>
#include <stdlib.h>
#define MAX_NODES 100
// Structure to represent a node in the adjacency list
struct Node {
int vertex;
struct Node* next;
};
// Structure to represent the graph
struct Graph {
int numVertices;
struct Node* adjLists[MAX_NODES];
int visited[MAX_NODES];
};
// Queue structure for BFS
struct Queue {
int items[MAX_NODES];
int front;
int rear;
};
// Function prototypes
struct Graph* createGraph(int vertices);
void addEdge(struct Graph* graph, int src, int dest);
void DFS(struct Graph* graph, int vertex);
void BFS(struct Graph* graph, int startVertex);
struct Queue* createQueue();
void enqueue(struct Queue* q, int value);
int dequeue(struct Queue* q);
int isEmpty(struct Queue* q);
// Function to create a graph with the given number of vertices
struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct
Graph));
graph->numVertices = vertices;
for (int i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = dest;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// Since it's an undirected graph, add edge from dest to src
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = src;
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// Depth First Search (DFS) function
void DFS(struct Graph* graph, int vertex) {
graph->visited[vertex] = 1;
printf("%d ", vertex);
struct Node* temp = graph->adjLists[vertex];
while (temp != NULL) {
int connectedVertex = temp->vertex;
if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
// Breadth First Search (BFS) function
void BFS(struct Graph* graph, int startVertex) {
struct Queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
int currentVertex = dequeue(q);
printf("%d ", currentVertex);
struct Node* temp = graph->adjLists[currentVertex];
while (temp != NULL) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}
// Function to create a queue
struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = -1;
q->rear = -1;
return q;
}
// Function to check if the queue is empty
int isEmpty(struct Queue* q) {
return q->rear == -1;
}
// Function to add an element to the queue
void enqueue(struct Queue* q, int value) {
if (q->rear == MAX_NODES - 1) {
printf("\nQueue is full!");
} else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
// Function to remove an element from the queue
int dequeue(struct Queue* q) {
int item;
if (isEmpty(q)) {
printf("\nQueue is empty!");
return -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
return item;
}
}
// Function to reset the visited array
void resetVisited(struct Graph* graph) {
for (int i = 0; i < graph->numVertices; i++) {
graph->visited[i] = 0;
}
}
// Main function
int main() {
int vertices, edges, src, dest, startVertex;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
struct Graph* graph = createGraph(vertices);
printf("Enter the number of edges: ");
scanf("%d", &edges);
printf("Enter the edges (source and destination):\n");
for (int i = 0; i < edges; i++) {
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
}
printf("Enter the starting vertex for DFS: ");
scanf("%d", &startVertex);
printf("Depth First Search starting from vertex %d:\n",
startVertex);
DFS(graph, startVertex);
resetVisited(graph); // Reset visited array for BFS
printf("\n\nEnter the starting vertex for BFS: ");
scanf("%d", &startVertex);
printf("Breadth First Search starting from vertex %d:\n",
startVertex);
BFS(graph, startVertex);
return 0;
}
Output