0% found this document useful (0 votes)
5 views10 pages

Data Structures and Algorithms

The document outlines a series of experiments related to Data Structures and Algorithms for a lab course, including operations on 1-D and 2-D arrays, linked lists, stacks, queues, and various sorting algorithms. It also describes methods for solving equations using Gauss Jordan and Gauss Elimination techniques. Each experiment includes code snippets and sample outputs demonstrating the implementation of these data structures and algorithms.

Uploaded by

aa98261645
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Data Structures and Algorithms

The document outlines a series of experiments related to Data Structures and Algorithms for a lab course, including operations on 1-D and 2-D arrays, linked lists, stacks, queues, and various sorting algorithms. It also describes methods for solving equations using Gauss Jordan and Gauss Elimination techniques. Each experiment includes code snippets and sample outputs demonstrating the implementation of these data structures and algorithms.

Uploaded by

aa98261645
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structures And Algorithms

April 11, 2026

Name: Ajay Kumar Sharma | Roll Number: 252432001 | Subject: DSA Lab | Branch: Mathematics And Computing

List of Experiments
1. 1-Dimensional Arrays and its various operations like insertion, deletion, searching, sorting etc.
2. 2-Dimensional Arrays and its various operations like inserting or deleting a particular row or column.
3. Finding solutions of a given equation using Gauss Jordan Method.
4. Finding solutions of a given equation using Gauss Elimination Method.
5. Singly Linked List and its various operations like insertion, deletion and reversing.
6. Doubly Linked List and its various operations like insertion, deletion and reversing.
7. Circular Linked List and its various operations like insertion, deletion and reversing.
8. Doubly Circular Linked list and its various operations like insertion, deletion and reversing.
9. Implementation of Stacks and its various operations using arrays (Push, Pop and Peek).

10. Implementation of Stacks and its various operations using linked list (Push, Pop and Peek).
11. Implementation of Queues using Arrays and perform operations like enqueue, dequeue etc.
12. Implementation of Queues using Linked List and perform operations like enqueue, dequeue etc.
13. Implementation of Circular Queues using arrays and perform operations like enqueue, dequeue etc.
14. Implementation of Circular Queues using Linked List and perform operations like enqueue, dequeue etc.
15. Implementation of Binary Trees and perform operations for traversals and to find the height of the tree.
16. Implementation of Binary Search Trees and perform operations for traversals and to find the height of the tree.
17. Implementation of AVL Trees and perform operations of searching, insertion, deletion and to get the height of the
tree.
18. Implementation of Various Sorting Algorithms on Arrays
a) Bubble Sort
b) Optimized Bubble Sort
c) Insertion Sort
d) Selection Sort

e) Quick Sort
f) Merge Sort
EXPERIMENT 1

1.1_D.Array
[ ]: 1.1_D.Array
#include <iostream>
using namespace std;

int main() {
int arr[100], n = 0, choice;

while (true) {
cout << "\n--- ARRAY OPERATIONS MENU ---\n";
cout << "1. Insert element\n";
cout << "2. Delete element\n";
cout << "3. Search element\n";
cout << "4. Sort array\n";
cout << "5. Display array\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
// Insertion
int pos, value;
cout << "Enter position (0 to " << n << "): ";
cin >> pos;
cout << "Enter value: ";
cin >> value;

for (int i = n; i > pos; i--) {


arr[i] = arr[i - 1];
}
arr[pos] = value;
n++;
cout << "Element inserted successfully.\n";
}

else if (choice == 2) {
// Deletion
int pos;

cout << "Enter position to delete (0 to " << n - 1 << "): ";
cin >> pos;

for (int i = pos; i < n - 1; i++) {


arr[i] = arr[i + 1];
}
n--;
cout << "Element deleted successfully.\n";
}

else if (choice == 3) {
// Searching (Linear Search)
int key, found = 0;
cout << "Enter element to search: ";
cin >> key;

for (int i = 0; i < n; i++) {


if (arr[i] == key) {
cout << "Element found at index " << i << endl;
found = 1;
break;
}
}
if (!found)
cout << "Element not found.\n";
}

else if (choice == 4) {
// Sorting (Bubble Sort)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "Array sorted unsuccessfully.\n";
}

else if (choice == 5) {
// Display
cout << "Array elements: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
}

cout << endl;


}

else if (choice == 6) {
cout << "Exiting program...\n";
break;
}

else {
cout << "Invalid choice! Try again.\n";
}
}

return 0;
}

#1 Output

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 1
Enter position (0 to 0): 0
Enter value: 25
Element inserted successfully.

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 1 1 35
Enter position (0 to 1): Enter value: Element inserted successfully.

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit

Enter your choice: 1 2 45


Enter position (0 to 2): Enter value: Element inserted successfully.

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 1 3 55
Enter position (0 to 3): Enter value: Element inserted successfully.

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 5
Array elements: 25 35 45 55

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 2
Enter position to delete (0 to 3): 2
Element deleted successfully.

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 5
Array elements: 25 35 55

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element

3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 3
Enter element to search: 25
Element found at index 0

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 4
Array sorted unsuccessfully.

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 5
Array elements: 25 35 55

--- ARRAY OPERATIONS MENU ---


1. Insert element
2. Delete element
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 6
Exiting program...
EXPERIMENT 2

2.2_D Array
[ ]: #2.2_D Array
#include <iostream>
using namespace std;

int main() {
int a[20][20];
int r, c;

cout << "Enter number of rows and columns: ";

cin >> r >> c;

cout << "Enter matrix elements:\n";


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> a[i][j];
}
}

int choice;
cout << "\n1. Insert Row\n2. Delete Row\n3. Insert Column\n4. Delete
Column\n";
cout << "Enter choice: ";
cin >> choice;

if (choice == 1) {
// Insert Row
int pos;
cout << "Enter row position to insert (0 to " << r << "): ";
cin >> pos;

for (int i = r; i > pos; i--) {


for (int j = 0; j < c; j++) {
a[i][j] = a[i - 1][j];
}
}

cout << "Enter elements of new row:\n";


for (int j = 0; j < c; j++) {
cin >> a[pos][j];
}
r++;
}

else if (choice == 2) {
// Delete Row
int pos;
cout << "Enter row position to delete (0 to " << r - 1 << "): ";
cin >> pos;

for (int i = pos; i < r - 1; i++) {


for (int j = 0; j < c; j++) {
a[i][j] = a[i + 1][j];
}
}
r--;
}
}

else if (choice == 3) {
// Insert Column
int pos;
cout << "Enter column position to insert (0 to " << c << "): ";
cin >> pos;

for (int j = c; j > pos; j--) {


for (int i = 0; i < r; i++) {
a[i][j] = a[i][j - 1];
}
}

cout << "Enter elements of new column:\n";


for (int i = 0; i < r; i++) {
cin >> a[i][pos];
}
c++;
}

else if (choice == 4) {
// Delete Column
int pos;
cout << "Enter column position to delete (0 to " << c - 1 << "): ";
cin >> pos;

for (int j = pos; j < c - 1; j++) {


for (int i = 0; i < r; i++) {
a[i][j] = a[i][j + 1];
}
}
c--;
}

cout << "\nUpdated Matrix:\n";


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}

return 0;
}

#20Output

Enter number of rows and columns: 3 3

Enter matrix elements:


011
202
330

1. Insert Row
2. Delete Row
3. Insert Column
4. Delete Column
Enter choice: 1
Enter row position to insert (0 to 3): 3
Enter elements of new row:
5
54

Updated Matrix:
011
202
330
554

Enter number of rows and columns: 3 3


Enter matrix elements:
011
202
330

1. Insert Row
2. Delete Row
3. Insert Column
4. Delete Column
Enter choice: 2
Enter row position to delete (0 to 2): 1

Updated Matrix:
011
330

Enter number of rows and columns: 3 3


Enter matrix elements:
011
202
330

1. Insert Row
2. Delete Row
3. Insert Column

4. Delete Column
Enter choice: 4
Enter column position to delete (0 to 2): 2

Updated Matrix:
01
20
33
EXPERIMENTS 3 & 4

Gauss Jordan & Gauss Elimination


#[Link] Jordan

[ ]: #[Link] Jordan
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
int n;
float a[20][21];

cout << "Enter number of variables: ";


cin >> n;

cout << "Enter augmented matrix (row-wise) for Gauss jordan method:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
cin >> a[i][j];
}
}

// Gauss-Jordan Elimination
for (int i = 0; i < n; i++) {
// Make diagonal element = 1
float pivot = a[i][i];
for (int j = 0; j <= n; j++) {
a[i][j] /= pivot;
}

// Make other elements in column = 0


for (int k = 0; k < n; k++) {
if (k != i) {
float factor = a[k][i];
for (int j = 0; j <= n; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
}
}

cout << "\nSolution:\n";


for (int i = 0; i < n; i++) {
cout << "x" << i + 1 << " = " << fixed << setprecision(2)
<< a[i][n] << endl;
}

return 0;
}

#[Link]
Enter number of variables: 3
Enter augmented matrix (row-wise) for Gauss jordan method:
-33 16 0 0.25
16 -33 16 0.5
0 16 -33 0.75

Solution:
x1 = -0.03
x2 = -0.06
x3 = -0.05

#4. Gauss Elimination

[ ]: #4. Gauss Elimination


#include <iostream>
#include <iomanip>
using namespace std;

int main() {
int n;
float a[20][21], x[20];

cout << "Enter number of variables: ";


cin >> n;

cout << "Enter augmented matrix (row-wise)for Gauss Elimination method:\n";


for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
cin >> a[i][j];
}
}

// Forward Elimination
for (int i = 0; i < n - 1; i++) {
for (int k = i + 1; k < n; k++) {
float factor = a[k][i] / a[i][i];
for (int j = i; j <= n; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
}

}
}

// Back Substitution
for (int i = n - 1; i >= 0; i--) {
x[i] = a[i][n];
for (int j = i + 1; j < n; j++) {
x[i] -= a[i][j] * x[j];
}
x[i] /= a[i][i];
}

cout << "\nSolution:\n";


for (int i = 0; i < n; i++) {
cout << "x" << i + 1 << " = "
<< fixed << setprecision(2) << x[i] << endl;
}

return 0;
}

#[Link]
Enter number of variables: 3
Enter augmented matrix (row-wise)for Gauss Elimination method:
-33 16 0 0.25
16 -33 16 0.5
0 16 -33 0.75

Solution:
x1 = -0.03
x2 = -0.06
x3 = -0.05
EXPERIMENT 5

Singly Linked List


[ ]: #[Link] Linked List
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

Node* createNode(int val) {


Node* temp = new Node;
temp->data = val;

temp->next = NULL;
return temp;
}

void insertBeg(int val) {


Node* temp = createNode(val);
temp->next = head;
head = temp;
}

void insertEnd(int val) {


Node* temp = createNode(val);

if (head == NULL) {
head = temp;
return;
}

Node* curr = head;


while (curr->next != NULL)
curr = curr->next;

curr->next = temp;
}

void insertPos(int val, int pos) {


if (pos == 1) {
insertBeg(val);
return;
}

Node* curr = head;


for (int i = 1; i < pos - 1 && curr != NULL; i++)
curr = curr->next;

if (curr == NULL) {
cout << "Invalid Position\n";
return;
}

Node* temp = createNode(val);


temp->next = curr->next;
curr->next = temp;
}

void deleteBeg() {
if (head == NULL) {

cout << "List Empty\n";


return;
}

Node* temp = head;


head = head->next;
delete temp;
}

void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

if (head->next == NULL) {
delete head;
head = NULL;
return;
}

Node* curr = head;


while (curr->next->next != NULL)
curr = curr->next;

delete curr->next;
curr->next = NULL;
}

void deletePos(int pos) {


if (head == NULL) {
cout << "List Empty\n";
return;
}

if (pos == 1) {
deleteBeg();
return;
}

Node* curr = head;


for (int i = 1; i < pos - 1 && curr != NULL; i++)
curr = curr->next;

if (curr == NULL || curr->next == NULL) {


cout << "Invalid Position\n";
return;
}
}

Node* temp = curr->next;


curr->next = temp->next;
delete temp;
}

void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

Node* curr = head;


cout << "List: ";
while (curr != NULL) {
cout << curr->data << " -> ";
curr = curr->next;
}
cout << "NULL\n";
}

int countNodes() {
int count = 0;
Node* curr = head;
while (curr != NULL) {
count++;
curr = curr->next;
}
return count;
}

int main() {
int choice, val, pos;
do
{
cout << "\n---- SINGLY LINKED LIST MENU ----\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Position\n";
cout << "7. Display List\n";
cout << "8. Count Nodes\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
}

cin >> choice;


switch (choice) {
case 1:
cout << "Enter value: ";
cin >> val;
insertBeg(val);
break;

case 2:
cout << "Enter value: ";
cin >> val;
insertEnd(val);
break;

case 3:
cout << "Enter value and position: ";
cin >> pos >> val;
insertPos(val, pos);
break;

case 4:
deleteBeg();
break;

case 5:
deleteEnd();
break;

case 6:
cout << "Enter position: ";
cin >> pos;
deletePos(pos);
break;

case 7:
display();
break;

case 8:
cout << "Total Nodes: " << countNodes() << endl;
break;

case 0:
cout << "Exiting...\n";
break;
}
} while (choice != 0);

return 0;
}

#5. Output

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
9. Exit
Enter choice: 1
Enter value: 10

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
9. Exit
Enter choice: 1
Enter value: 15

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
9. Exit
Enter choice: 2
Enter value: 50

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 2
Enter value: 55

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 3
Enter value and position: 32 2
Invalid Position

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 15 -> 10 -> 50 -> 55 -> NULL

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes

0. Exit
Enter choice: 3
Enter value and position: 32 2
Invalid Position

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 3
Enter value and position: 35 32
Invalid Position

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 3
Enter value and position: 11 10
Invalid Position

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 6
Enter position: 1

---- SINGLY LINKED LIST MENU ----

1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 10 -> 50 -> 55 -> NULL

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 8
Total Nodes: 3

---- SINGLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 0
Exiting...
EXPERIMENT 6

Doubly Linked List


[ ]: #[Link] Linked List
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* prev;
Node* next;
};

Node* head = NULL;

Node* createNode(int val) {


Node* temp = new Node;
temp->data = val;
temp->prev = NULL;
temp->next = NULL;
return temp;
}

void insertBeg(int val) {


Node* temp = createNode(val);
if (head != NULL) {
head->prev = temp;
temp->next = head;
}
head = temp;
}

void insertEnd(int val) {


Node* temp = createNode(val);

if (head == NULL) {
head = temp;
return;
}

Node* curr = head;


while (curr->next != NULL)
curr = curr->next;

curr->next = temp;
temp->prev = curr;
}

void insertPos(int val, int pos) {


if (pos == 1) {
insertBeg(val);
return;
}

Node* curr = head;


for (int i = 1; i < pos - 1 && curr != NULL; i++)
curr = curr->next;

if (curr == NULL) {

cout << "Invalid Position\n";


return;
}

Node* temp = createNode(val);


temp->next = curr->next;
temp->prev = curr;

if (curr->next != NULL)
curr->next->prev = temp;

curr->next = temp;
}

void deleteBeg() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

Node* temp = head;


head = head->next;

if (head != NULL)
head->prev = NULL;

delete temp;
}

void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

if (head->next == NULL) {
delete head;
head = NULL;
return;
}

Node* curr = head;


while (curr->next != NULL)
curr = curr->next;

curr->prev->next = NULL;
delete curr;
}

void deletePos(int pos) {


if (head == NULL) {
cout << "List Empty\n";
return;
}

if (pos == 1) {
deleteBeg();
return;
}

Node* curr = head;


for (int i = 1; i < pos && curr != NULL; i++)
curr = curr->next;

if (curr == NULL) {
cout << "Invalid Position\n";
return;
}

if (curr->next != NULL)
curr->next->prev = curr->prev;

if (curr->prev != NULL)
curr->prev->next = curr->next;

delete curr;
}

void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

Node* curr = head;


cout << "List: ";
while (curr != NULL) {
cout << curr->data << " <-> ";
curr = curr->next;
}
cout << "NULL\n";
}

int countNodes() {

int count = 0;
Node* curr = head;
while (curr != NULL) {
count++;
curr = curr->next;
}
return count;
}

int main() {
int choice, val, pos;

do {
cout << "\n---- DOUBLY LINKED LIST MENU ----\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Position\n";
cout << "7. Display List\n";
cout << "8. Count Nodes\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value: ";
cin >> val;
insertBeg(val);
break;

case 2:
cout << "Enter value: ";
cin >> val;
insertEnd(val);
break;

case 3:
cout << "Enter value and position: ";
cin >> pos >> val;
insertPos(val, pos);
break;

case 4:
deleteBeg();

break;

case 5:
deleteEnd();
break;

case 6:
cout << "Enter position: ";
cin >> pos;
deletePos(pos);
break;

case 7:
display();
break;

case 8:
cout << "Total Nodes: " << countNodes() << endl;
break;

case 0:
cout << "Exiting...\n";
break;
}

} while (choice != 0);

return 0;
}

#6. Output

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 1
Enter value: 10

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End

3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 1 15 1 20 2 25 2 30
Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning

5. Delete from End


6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 <-> 15 <-> 10 <-> 25 <-> 30 <-> NULL

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 3 1 45
Enter value and position:
---- DOUBLY LINKED LIST MENU ----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 45 <-> 20 <-> 15 <-> 10 <-> 25 <-> 30 <-> NULL

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 4

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning

2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 <-> 15 <-> 10 <-> 25 <-> 30 <-> NULL

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 5

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 <-> 15 <-> 10 <-> 25 <-> NULL

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 8

Total Nodes: 4

---- DOUBLY LINKED LIST MENU ----


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
9. Exit
Enter choice: 0
Exiting...
EXPERIMENT 7

Circular Linked List


[ ]: #[Link] Linked List
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

Node* createNode(int val) {


Node* temp = new Node;
temp->data = val;
temp->next = NULL;
return temp;
}

void insertBeg(int val) {


Node* temp = createNode(val);

if (head == NULL) {
head = temp;
temp->next = head;
return;
}

Node* curr = head;


while (curr->next != head)
curr = curr->next;

curr->next = temp;

temp->next = head;
head = temp;
}

void insertEnd(int val) {


Node* temp = createNode(val);

if (head == NULL) {
head = temp;
temp->next = head;
return;
}

Node* curr = head;


while (curr->next != head)
curr = curr->next;

curr->next = temp;
temp->next = head;
}

void insertPos(int pos, int val) {


if (pos == 1) {
insertBeg(val);
return;
}

Node* curr = head;


for (int i = 1; i < pos - 1 && curr->next != head; i++)
curr = curr->next;

if (curr->next == head && pos != 2) {


cout << "Invalid Position\n";
return;
}

Node* temp = createNode(val);


temp->next = curr->next;
curr->next = temp;
}

void deleteBeg() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
}

if (head->next == head) {
delete head;
head = NULL;
return;
}

Node* curr = head;


while (curr->next != head)
curr = curr->next;

Node* temp = head;


curr->next = head->next;
head = head->next;
delete temp;
}

void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

if (head->next == head) {
delete head;
head = NULL;
return;
}

Node* curr = head;


while (curr->next->next != head)
curr = curr->next;

delete curr->next;
curr->next = head;
}

void deletePos(int pos) {


if (head == NULL) {
cout << "List Empty\n";
return;
}

if (pos == 1) {
deleteBeg();
return;
}
}

Node* curr = head;


for (int i = 1; i < pos - 1 && curr->next != head; i++)
curr = curr->next;

if (curr->next == head) {
cout << "Invalid Position\n";
return;
}

Node* temp = curr->next;


curr->next = temp->next;
delete temp;
}

void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

Node* curr = head;


cout << "List: ";
do {
cout << curr->data << " -> ";
curr = curr->next;
} while (curr != head);
cout << "(HEAD)\n";
}

int countNodes() {
if (head == NULL) return 0;

int count = 0;
Node* curr = head;
do {
count++;
curr = curr->next;
} while (curr != head);

return count;
}

int main() {
int choice, val, pos;

do {
cout << "\n--- CIRCULAR SINGLY LINKED LIST ---\n";
}

cout << "1. Insert at Beginning\n";


cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Position\n";
cout << "7. Display List\n";
cout << "8. Count Nodes\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
cin >> val;
insertBeg(val);
break;
case 2:
cin >> val;
insertEnd(val);
break;
case 3:
cout << "Enter position then value: ";
cin >> pos >> val;
insertPos(pos, val);
break;
case 4:
deleteBeg();
break;
case 5:
deleteEnd();
break;
case 6:
cin >> pos;
deletePos(pos);
break;
case 7:
display();
break;
case 8:
cout << "Total Nodes: " << countNodes() << endl;
break;
}
} while (choice != 0);

return 0;
}

#[Link]

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 1 10 1 15 1 20 1 25 2 30 2 32

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit

Enter choice:
--- CIRCULAR SINGLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR SINGLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR SINGLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 25 -> 20 -> 15 -> 10 -> 30 -> 32 -> (HEAD)

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 4

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 -> 15 -> 10 -> 30 -> 32 -> (HEAD)

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 5

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 -> 15 -> 10 -> 30 -> (HEAD)

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List

8. Count Nodes
0. Exit
Enter choice: 6
1

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 15 -> 10 -> 30 -> (HEAD)

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 8
Total Nodes: 3

--- CIRCULAR SINGLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 0
Exiting...
EXPERIMENT 8

Doubly Circular Linked List


[ ]: #[Link] Circular Linked List
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* prev;
Node* next;
};

Node* head = NULL;

Node* createNode(int val) {


Node* temp = new Node;
temp->data = val;
temp->prev = temp->next = temp;
return temp;
}

void insertBeg(int val) {


Node* temp = createNode(val);

if (head == NULL) {
head = temp;
return;
}

Node* last = head->prev;


temp->next = head;
temp->prev = last;
last->next = temp;
head->prev = temp;
head = temp;
}

void insertEnd(int val) {


Node* temp = createNode(val);

if (head == NULL) {
head = temp;
return;
}

Node* last = head->prev;


last->next = temp;
temp->prev = last;
temp->next = head;
head->prev = temp;
}

void insertPos(int pos, int val) {


if (pos == 1) {
insertBeg(val);
return;
}

Node* curr = head;


for (int i = 1; i < pos - 1 && curr->next != head; i++)
curr = curr->next;

if (curr->next == head) {
cout << "Invalid Position\n";
return;
}

Node* temp = createNode(val);


temp->next = curr->next;
temp->prev = curr;
curr->next->prev = temp;
curr->next = temp;
}

void deleteBeg() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

if (head->next == head) {
delete head;
head = NULL;
return;
}

Node* last = head->prev;


Node* temp = head;
head = head->next;
last->next = head;
head->prev = last;
delete temp;
}

void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
}

if (head->next == head) {
delete head;
head = NULL;
return;
}

Node* last = head->prev;


last->prev->next = head;
head->prev = last->prev;
delete last;
}

void deletePos(int pos) {


if (head == NULL) {
cout << "List Empty\n";
return;
}

if (pos == 1) {
deleteBeg();
return;
}

Node* curr = head;


for (int i = 1; i < pos && curr->next != head; i++)
curr = curr->next;

if (curr == head) {
cout << "Invalid Position\n";
return;
}

curr->prev->next = curr->next;
curr->next->prev = curr->prev;
delete curr;
}

void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}

Node* curr = head;


cout << "List: ";
do {

cout << curr->data << " <-> ";


curr = curr->next;
} while (curr != head);
cout << "(HEAD)\n";
}

int countNodes() {
if (head == NULL) return 0;

int count = 0;
Node* curr = head;
do {
count++;
curr = curr->next;
} while (curr != head);

return count;
}

int main() {
int choice, val, pos;

do {
cout << "\n--- CIRCULAR DOUBLY LINKED LIST ---\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Position\n";
cout << "7. Display List\n";
cout << "8. Count Nodes\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
cin >> val;
insertBeg(val);
break;
case 2:
cin >> val;
insertEnd(val);
break;
case 3:
cout << "Enter position then value: ";

cin >> pos >> val;


insertPos(pos, val);
break;
case 4:
deleteBeg();
break;
case 5:
deleteEnd();
break;
case 6:
cin >> pos;
deletePos(pos);
break;
case 7:
display();
break;
case 8:
cout << "Total Nodes: " << countNodes() << endl;
break;
}
} while (choice != 0);

return 0;
}

#[Link]

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 1 10 1 15 1 20 2 30 2 33 2 36

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List

8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 <-> 15 <-> 10 <-> 30 <-> 33 <-> 36 <-> (HEAD)

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 3
Enter position then value: 2 24

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 <-> 24 <-> 15 <-> 10 <-> 30 <-> 33 <-> 36 <-> (HEAD)

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List

8. Count Nodes
0. Exit
Enter choice: 4

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 24 <-> 15 <-> 10 <-> 30 <-> 33 <-> 36 <-> (HEAD)

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 5

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 24 <-> 15 <-> 10 <-> 30 <-> 33 <-> (HEAD)

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning

5. Delete from End


6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 6
2

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 24 <-> 10 <-> 30 <-> 33 <-> (HEAD)

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 8
Total Nodes: 4

--- CIRCULAR DOUBLY LINKED LIST ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 0
EXPERIMENTS 9 & 10

Stack using Array & Stack Using Linked List


#[Link] using Array

[ ]: #[Link] using Array


#include <bits/stdc++.h>
using namespace std;

#define MAX 100

int stackArr[MAX];
int top = -1;

void push(int val) {


if (top == MAX - 1) {
cout << "Stack Overflow\n";
return;
}
stackArr[++top] = val;
cout << "Pushed: " << val << endl;
}

void pop() {
if (top == -1) {
cout << "Stack Underflow\n";
return;
}
cout << "Popped: " << stackArr[top--] << endl;
}

void peek() {
if (top == -1) {
cout << "Stack is Empty\n";
return;
}
cout << "Top Element: " << stackArr[top] << endl;
}

int main() {
int choice, val;

do {
cout << "\n--- STACK USING ARRAY ---\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {

case 1:
cout << "Enter value: ";
cin >> val;
push(val);
break;

case 2:
pop();
break;

case 3:
peek();
break;

case 0:
cout << "Exiting...\n";
break;
}

} while (choice != 0);

return 0;
}

#9. Output

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
4. Exit
Enter choice: 1 5 1 10 1 20 1 26
Enter value: Pushed: 5

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
4. Exit
Enter choice: Enter value: Pushed: 10

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
4. Exit
Enter choice: Enter value: Pushed: 20

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: Enter value: Pushed: 26

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 3
Top Element: 26

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 2
Popped: 26

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 3
Top Element: 20

--- STACK USING ARRAY ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 0
Exiting...

#[Link] Using Linked List

[ ]: #[Link] Using Linked List


#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* next;
};

};

Node* top = NULL;

void push(int val) {


Node* temp = new Node;
temp->data = val;
temp->next = top;
top = temp;
cout << "Pushed: " << val << endl;
}

void pop() {
if (top == NULL) {
cout << "Stack Underflow\n";
return;
}

Node* temp = top;


cout << "Popped: " << top->data << endl;
top = top->next;
delete temp;
}

void peek() {
if (top == NULL) {
cout << "Stack is Empty\n";
return;
}

cout << "Top Element: " << top->data << endl;


}

int main() {
int choice, val;

do {
cout << "\n--- STACK USING LINKED LIST ---\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value: ";

cin >> val;


push(val);
break;

case 2:
pop();
break;

case 3:
peek();
break;

case 0:
cout << "Exiting...\n";
break;

} while (choice != 0);

return 0;
}

#[Link]

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 1 10 1 16 1 20 1 25
Enter value: Pushed: 10

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: Enter value: Pushed: 16

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: Enter value: Pushed: 20

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: Enter value: Pushed: 25

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 3
Top Element: 25

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 2
Popped: 25

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 2
Popped: 20

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 3
Top Element: 16

--- STACK USING LINKED LIST ---


1. Push
2. Pop
3. Peek
0. Exit
Enter choice: 0
Exiting...
EXPERIMENTS 11 & 12

Queue_Array & Queue_Linked list


#11.Queue_Array

[ ]: #11.Queue_Array
#include <iostream>
using namespace std;

#define SIZE 5

class Queue {
int arr[SIZE];
int front, rear;

public:
Queue() {
front = -1;
rear = -1;
}

void enqueue(int value) {


if (rear == SIZE - 1) {
cout << "Queue Overflow\n";
return;
}
if (front == -1) front = 0;

arr[++rear] = value;
cout << value << " inserted\n";
}

void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow\n";
return;
}
cout << arr[front++] << " deleted\n";
}

void display() {
if (front == -1 || front > rear) {
cout << "Queue is empty\n";
return;
}
cout << "Queue: ";
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
cout << endl;
}
};

int main() {
Queue q;
int choice, value;

do {
cout << "\n--- Queue using Array ---\n";
cout << "1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value: ";
cin >> value;
[Link](value);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 4);

return 0;
}

#11. Output

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 1 10 1 20 1 25 1 30
Enter value: 10 inserted

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display

4. Exit
Enter choice: Enter value: 20 inserted

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: Enter value: 25 inserted

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: Enter value: 30 inserted

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 10 20 25 30

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 2
10 deleted

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 20 25 30

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 2

20 deleted

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 25 30

--- Queue using Array ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 4
Exiting...

#12.Queue_Linked list

[ ]: #12.Queue_Linked list
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

class Queue {
Node *front, *rear;

public:
Queue() {
front = rear = NULL;
}

void enqueue(int value) {


Node* temp = new Node;
temp->data = value;
temp->next = NULL;

if (rear == NULL) {
front = rear = temp;
} else {
rear->next = temp;
rear = temp;
}
}
}

cout << value << " inserted\n";


}

void dequeue() {
if (front == NULL) {
cout << "Queue Underflow\n";
return;
}

Node* temp = front;


cout << temp->data << " deleted\n";

front = front->next;
if (front == NULL) rear = NULL;

delete temp;
}

void display() {
if (front == NULL) {
cout << "Queue is empty\n";
return;
}

Node* temp = front;


cout << "Queue: ";
while (temp != NULL) {
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
};

int main() {
Queue q;
int choice, value;

do {
cout << "\n--- Queue using Linked List ---\n";
cout << "1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value: ";

cin >> value;


[Link](value);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 4);

return 0;
}

#12. Output

--- Queue using Linked List ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 1 10 1 15 1 5 1 30
Enter value: 10 inserted

--- Queue using Linked List ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: Enter value: 15 inserted

--- Queue using Linked List ---


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: Enter value: 5 inserted

--- Queue using Linked List ---


1. Enqueue
2. Dequeue

3. Display
4. Exit
Enter choice: Enter value: 30 inserted

--- Queue using Linked List ---

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 10 15 5 30

--- Queue using Linked List ---

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 2
10 deleted

--- Queue using Linked List ---

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 15 5 30

--- Queue using Linked List ---

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 2
15 deleted

--- Queue using Linked List ---

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 5 30

--- Queue using Linked List ---

1. Enqueue
2. Dequeue

3. Display
4. Exit
Enter choice: 4
Exiting...

You might also like