data structures - lab file (Extra Experiments)
data structures - lab file (Extra Experiments)
CIC-255
Faculty name: Student name
Roll No.:
Semester:
MISSION
The Institute shall endeavor to incorporate the following basic missions in
the teaching methodology:
Engineering Hardware – Software Symbiosis
Practical exercises in all Engineering and Management disciplines shall be
carried out by Hardware equipment as well as the related software enabling deeper
understanding of basic concepts and encouraging inquisitive nature.
Life – Long Learning
The Institute strives to match technological advancements and encourage
students to keep updating their knowledge for enhancing their skills and inculcating
their habit of continuous learning.
Liberalization and Globalization
The Institute endeavors to enhance technical and management skills of
students so that they are intellectually capable and competent professionals with
Industrial Aptitude to face the challenges of globalization.
Diversification
The Engineering, Technology and Management disciplines have diverse
fields of studies with different attributes. The aim is to create a synergy of the above
attributes by encouraging analytical thinking.
Digitization of Learning Processes
The Institute provides seamless opportunities for innovative learning in all
Engineering and Management disciplines through digitization
of learning processes using analysis, synthesis, simulation, graphics, tutorials
and related tools to create a platform for multi- disciplinary approach.
Entrepreneurship
The Institute strives to develop potential Engineers and Managers by
enhancing their skills and research capabilities so that they become successful
entrepreneurs and responsible citizens.
MAHARAJAAGRASENINSTITUTEOF
TECHNOLOGY
COMPUTERSCIENCE&ENGINEERING DEPARTMENT
VISION
“To be centre of excellence in education, research and technology transfer in the field of
computer engineering and promote entrepreneurship and ethical values.”
MISSION
“To foster an open, multidisciplinary and highly collaborative research environment
to produce world-class engineers capable of providing innovative solutions to real
life problems and fulfil societal needs.
LIST OF EXPERIMENTS (As prescribed by G.G.S.I.P.U)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Practical-
Aim: Write a program to merge two arrays(sorted in ascending order) and create a ghird
Algorithm:
Source Code:
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout << "Enter the number of elements in the first sorted array: ";
cin >> n1;
int arr1[n1]; cout << "Enter elements of the first sorted array in
ascending order:\n"; for (int i = 0; i < n1; i++) {
cin >> arr1[i];
}
cout << "Enter the number of elements in the second sorted array: ";
cin >> n2;
int arr2[n2]; cout << "Enter elements of the second sorted array in
ascending order:\n"; for (int i = 0; i < n2; i++) {
cin >> arr2[i];
} int arr3[n1 + n2];
Output:
Practical-
Aim: In a sales company, there are 5 salesman and each salesman is supposed to sell 3
product. WAP using 2D array to print the total sales by each salesman and the total
sales of each item.
Algorithm:
Source Code:
#include <iostream>
using namespace std;
int main() {
const int salesmen = 5;
const int products = 3; int
sales[salesmen][products];
cout << "Enter the sales data for 5 salesmen and 3 products:\n";
for (int i = 0; i < salesmen; i++) { cout << "Salesman " << i + 1
<< ":\n"; for (int j = 0; j < products; j++) {
cout << "Product " << j + 1 << ": ";
cin >> sales[i][j];
}
}
return 0;
}
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
int rows = 3;
int cols = 3;
int main() {
int A[rows][cols], B[rows][cols], result[rows][cols];
return 0;
}
Output:
Practical-
Aim: Write a program to fill a square matrix with '0' in diagonal, '1' in upper triangle and
'-1' in lower triangle
Algorithm:
Source Code:
#include <iostream>
using namespace std;
int main() {
int n;
return 0;
}
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
int main() {
int size, key;
int arr[size];
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found" << endl;
return 0;
}
Output:
Practical-
Aim: Create two arrays and merge them into third one in sorted manner
Algorithm:
Source Code:
#include <iostream>
using namespace std;
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int arr3[]) {
for (int i = 0; i < n1; i++) arr3[i] = arr1[i]; for
(int i = 0; i < n2; i++) arr3[n1 + i] = arr2[i];
int main() {
int n1, n2; cout << "Enter size of
first array: "; cin >> n1;
int arr1[n1]; cout << "Enter elements of
first array:\n"; for (int i = 0; i < n1; i++)
cin >> arr1[i];
Aim: Write a program to find all the subarrays having sum zero
Algorithm:
Source Code:
#include <iostream>
using namespace std;
return 0;
}
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
#define MAX 1000
class Stack {
int top;
public:
int arr[MAX]; Stack() { top = -
1; } void push(int x) { if (top <
MAX - 1) arr[++top] = x; else
cout << "Stack Overflow\n";
} int pop()
{
return (top >= 0) ? arr[top--] : -1;
} bool isEmpty()
{
return top == -1;
}
};
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
#define MAX 1000
class Queue {
int front, rear;
int arr[MAX];
public:
Queue() { front = -1;
rear = -1;
} void enqueue(int x)
{ if (rear < MAX - 1)
arr[++rear] = x; else
cout << "Queue Overflow\n";
} int dequeue()
{
return (front < rear) ? arr[++front] : -1;
} bool isEmpty()
{
return front == rear;
}
};
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
Node* head = NULL;
int n, value;
// Creating linked list cout <<
"Enter number of nodes: "; cin >>
n; for (int i = 0; i < n; i++) {
cout << "Enter value for node " << i+1 << ": ";
cin >> value; insertAtBeginning(&head, value);
//Insert at the beginning
}
head = reverseList(head);
cout <<
"Reversed
List: ";
printList(head
);
return 0;
}
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
#define MAX 1000
class TwoStacks {
int arr[MAX];
int top1, top2;
public:
TwoStacks() { top1 = -1;
top2 = MAX;
} void push1(int x) {
if (top1 < top2 - 1)
arr[++top1] = x;
} void push2(int x) {
if (top1 < top2 - 1)
arr[--top2] = x;
Output:
Practical-
Aim: Write a program to create linked list and find the count of duplicate elements (if
any)
Algorithm:
Source Code:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insertAtEnd(Node** head_ref, int new_data)
{ Node* new_node = new Node(); new_node-
>data = new_data; new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
last->next = new_node;
}
return duplicates;
}
void printList(Node* node) {
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
int main() {
Node* head = NULL;
int n, value;
return 0;
}
Output:
Practical-
Aim: Write a program to create a stack and perform push, pop, peek and traverse
operations on the stack using a linked list.
Algorithm:
Source Code:
#include <iostream>
using namespace
std; struct Node { int
data;
Node* next;
}; class
Stack {
private:
Node* top;
public:
Stack() { top =
nullptr; }
void push(int value) {
Node* newNode = new
Node(); newNode->data =
value; newNode->next = top;
top = newNode;
cout << "Pushed: " << value << endl;
} void pop()
{
if (top == nullptr) {
cout << "Stack Underflow!" << endl;
return;
}
Node* temp = top; top = top->next; cout
<< "Popped: " << temp->data << endl; delete
temp; } void peek() {
if (top == nullptr) {
cout << "Stack is empty!" << endl;
} else { cout << "Top element is: " << top->data
<< endl;
} } void
traverse() {
if (top == nullptr) {
cout << "Stack is empty!" << endl;
return;
}
Node* temp = top; cout <<
"Stack elements: "; while
(temp != nullptr) { cout <<
temp->data << " "; temp =
temp->next;
}
cout << endl;
}
}; int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.peek();
stack.traverse();
stack.pop();
stack.peek();
stack.traverse()
; return 0; }
Output:
Practical-
Aim: Write a program to create a circular linked list having information about a college
and perform insertion at front and deletion at end.
Algorithm:
Source Code:
#include <iostream>
#include <string>
using namespace
std; struct Node {
string name; string
department; int year;
Node* next;
};
class CircularLinkedList {
private:
Node* last;
public:
CircularLinkedList() {
last = nullptr;
}
void deleteEnd() {
if (last == nullptr) {
cout << "List is empty!" << endl;
return; } if (last-
>next == last) {
cout << "Deleted: " << last->name << ", " << last->department << ", " <<
last->year << endl;
delete last;
last = nullptr;
}
else {
Node*
temp =
last-
>next;
while
(temp-
>next !=
last) {
temp = temp->next;
}
temp->next = last->next; cout << "Deleted: " << last->name << ", " <<
last->department << ", " <<
last->year << endl;
delete last;
last = temp;
} } void display() {
if (last == nullptr) {
cout << "List is empty!" << endl;
return;
}
int main() {
CircularLinkedList cll;
cll.insertFront("MAIT", "Electrical", 1999);
cll.insertFront("DTU", "Computer Science", 1941);
cll.insertFront("NSUT”, "Mechanical", 1983);
cll.display(); cll.deleteEnd(); cll.display();
cll.deleteEnd();
cll.display();
return 0;
}
Output:
Practical-
Aim: Write a program to create a doubly linked list having information about an
employee and perform insertion at front and deletion at the end
Algorithm:
Source Code:
#include <iostream>
#include <string>
using namespace std;
struct Node {
string name; int
id;
string department;
Node* prev;
Node* next;
}; class
DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() {
head = nullptr; tail
= nullptr;
} void insertFront(string name, int id, string
dept) { Node* newNode = new Node();
newNode->name = name; newNode->id = id;
newNode->department = dept; newNode->prev
= nullptr; newNode->next = head; if (head !=
nullptr) {
head->prev = newNode;
} else { tail =
newNode;
} head = newNode; cout << "Inserted at front: " << name << ", " << id <<
", " << dept << endl;
} void deleteEnd()
{
if (tail == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = tail;
if (head == tail) {
head = nullptr;
tail = nullptr;
} else { tail = tail-
>prev; tail->next =
nullptr;
} cout << "Deleted from end: " << temp->name << ", " << temp->id << ", "
<<
temp->department << endl;
delete temp;
} void display()
{
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head; cout << "Doubly Linked List:" << endl; while (temp !=
nullptr) { cout << temp->name << ", " << temp->id << ", " << temp->department
<< endl; temp = temp->next;
}
}
}; int
main() {
DoublyLinkedList dll;
dll.insertFront("Aman", 101, "HR");
dll.insertFront("Shankar", 102, "Finance");
dll.insertFront("Alisha", 103, "Engineering");
dll.display(); dll.deleteEnd(); dll.display();
dll.deleteEnd(); dll.display(); return 0;
}
Output:
Practical-
Aim: Write a program to create a linked list with nodes having info about a student and
perform:
(a) insertion of new node at a specified position
(b)deletion of node with specified roll number
(c) reversal of linked list Software
Algorithm:
Source Code:
#include <iostream>
#include <string>
struct StudentNode
{ int rollNo;
string name;
StudentNode* next;
if (temp == nullptr) {
cout << "Roll number not found." << endl;
return; }
if (prev == nullptr) {
head = temp->next;
} else { prev->next = temp-
>next;
}
delete temp;
}
int main() {
StudentNode* head = nullptr;
int choice, position, rollNo;
string name;
while (true) {
cout << "\nMenu:\n"; cout << "1.
Insert at Position\n"; cout << "2.
Delete by Roll No\n"; cout <<
"3. Reverse List\n"; cout << "4.
Display List\n"; cout << "5.
Exit\n"; cout << "Enter your
choice: "; cin >> choice;
switch (choice) {
case 1:
cout << "Enter position: "; cin >> position; cout
<< "Enter roll number: "; cin >> rollNo; cout <<
"Enter name: "; cin.ignore(); // To ignore leftover
newline character getline(cin, name);
insertAtPosition(head, position, rollNo, name);
break;
case 2:
cout << "Enter roll number to delete:
"; cin >> rollNo; deleteByRollNo(head,
rollNo); break; case 3:
reverse(head); cout << "List
reversed." << endl; break;
case 4:
display(head);
break;
case 5:
while (head != nullptr) {
StudentNode* temp = head;
head = head->next; delete
temp; } return 0;
default:
cout << "Invalid choice. Try again." << endl;
}
}
return 0;
}
Output:
Practical-
Aim: Write a program to create a linear queue using linked list and implement
insertion, deletion and display.
Software Used: Visual Studio Code
Algorithm:
Source Code:
#include <iostream>
using namespace std;
public:
// Constructor to initialize the queue
Queue() : front(nullptr), rear(nullptr) {}
// Add the new node at the end and update the rear
rear->next = newNode;
rear = newNode;
cout << value << " enqueued into the queue." << endl;
}
cout << "Dequeued " << temp->data << " from the queue." << endl;
delete temp; // Free the memory of the dequeued node
}
while (true) {
cout << "\nMenu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display Queue\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
q.enqueue(value);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
// Node structure for the sparse matrix
struct Node {
int row; // Row index
int col; // Column index
int value; // Non-zero value
Node* next; // Pointer to the next node
public:
// Constructor to initialize an empty sparse matrix
SparseMatrix() : head(nullptr) {}
return 0;
}
Output:
Practical-
Aim: Write a program to binary tree and Traverse (pre-order, in-order, post-order)
using recursion.
Software Used: Visual Studio Code
Algorithm:
Source Code:
#include <iostream>
using namespace std;
// Node structure for the binary tree
struct Node {
int data;
Node* left;
Node* right;
// Main function
int main() {
BinaryTree tree;
return 0;
}
Output:
Practical-
Aim: Write a program to binary search tree with information about automobile and
perform insertion, deletion and traversal.
Software Used: Visual Studio Code
Algorithm:
Source Code:
#include <iostream>
#include <string>
using namespace std;
// Find the minimum node in the right subtree (used for deletion)
TreeNode* findMin(TreeNode* node) {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
// Main function
int main() {
BST tree; // Create a Binary Search Tree
int choice, year;
string modelName;
double price;
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert Automobile\n";
cout << "2. Delete Automobile by Year of Manufacture\n";
cout << "3. In-order Traversal\n";
cout << "4. Pre-order Traversal\n";
cout << "5. Post-order Traversal\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter Model Name: ";
cin.ignore();
getline(cin, modelName);
cout << "Enter Year of Manufacture: ";
cin >> year;
cout << "Enter Price: ";
cin >> price;
tree.root = tree.insert(tree.root, Automobile(modelName, year, price));
break;
case 2:
cout << "Enter Year of Manufacture to delete: ";
cin >> year;
tree.root = tree.deleteNode(tree.root, year);
break;
case 3:
cout << "In-order Traversal:\n";
tree.inOrder(tree.root);
break;
case 4:
cout << "Pre-order Traversal:\n";
tree.preOrder(tree.root);
break;
case 5:
cout << "Post-order Traversal:\n";
tree.postOrder(tree.root);
break;
case 6:
exit(0);
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}
Output:
Practical-
Algorithm:
Source Code:
#include <iostream>
using namespace std;
// Node structure for linked list
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
// If loop exists
if (slow == fast) {
// Move one pointer to head and keep the other at the meeting point
slow = head;
// Break the loop by setting the next of the loop-end node to nullptr
fast->next = nullptr;
}
}
// Main function
int main() {
Node* head = nullptr;
// Creating a linked list: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> NULL
insert(head, 10);
insert(head, 20);
insert(head, 30);
insert(head, 40);
insert(head, 50);
insert(head, 60);
return 0;
}
Output:
Practical-
Aim: Write a program to segregate even and odd node in linked list.
Software Used: Visual Studio Code
Algorithm:
Source Code:
#include <iostream>
using namespace std;
// Node structure for linked list
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
// Main function
int main() {
Node* head = nullptr;
// Creating a linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> NULL
append(head, 1);
append(head, 2);
append(head, 3);
append(head, 4);
append(head, 5);
append(head, 6);
append(head, 7);
return 0;
}
Output: