0% found this document useful (0 votes)
19 views42 pages

DS Lab File 3rd Sem

Uploaded by

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

DS Lab File 3rd Sem

Uploaded by

Mehnaaz Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Mehnaaz Ansari

07701182023

INDIRA GANDHI DELHI TECHNICAL UNIVERSITY FOR WOMEN,


KASHMERE GATE, DELHI 110006

DEPARTMENT OF ELECTRONICS AND COMMUNICATION


ENGINEERING
3rd SEMESTER
2024-25
DATA STRUCTURES LAB FILE

SUBMITTED BY: SUBMITTED TO:


Mehnaaz Ansari Ms. Ramsha Suhail
07701182023 SUBJECT CODE:
ECE-AI 2 BCS-201

1
Mehnaaz Ansari
07701182023

INDEX
S.No. List of Experiments Date Remarks

2
Mehnaaz Ansari
07701182023

3
Mehnaaz Ansari
07701182023

4
Mehnaaz Ansari
07701182023

EXPERIMENT-1
Language- cpp
Compiler- vs code

AIM: Write a C program to implement Array data structure with following operations
a) Traversal
b) Insertion
c) Deletion
d) Sorting
e) Searching (linear Search)

SOURCE CODE:
#include <iostream>
using namespace std;

#define MAX_CAPACITY 100

// Traversal
void traverse(int arr[], int size)
{
if (size == 0)
{
cout << "Array is empty!" << endl;
return;
}
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

// Insertion
void insert(int arr[], int &size, int element, int index)
{
if (size >= MAX_CAPACITY)
{
cout << "Array is full, cannot insert!" << endl;
return;
}
if (index < 0 || index > size)
{
cout << "Invalid index!" << endl;
return;
}
for (int i = size; i > index; i--)
{

5
Mehnaaz Ansari
07701182023

arr[i] = arr[i - 1];


}
arr[index] = element;
size++;
}

// Deletion
void remove(int arr[], int &size, int index)
{
if (index < 0 || index >= size)
{
cout << "Invalid index!" << endl;
return;
}
for (int i = index; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
size--;
}

// Sorting (Bubble Sort)


void sort(int arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

// Searching (Linear Search)


int search(int arr[], int size, int element)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == element)
{
return i;
}
}
return -1;
}

6
Mehnaaz Ansari
07701182023

int main()
{
int arr[MAX_CAPACITY];
int size = 0;

// Insertion
insert(arr, size, 10, 0);
insert(arr, size, 20, 1);
insert(arr, size, 30, 2);
insert(arr, size, 40, 3);
insert(arr, size, 50, 4);

cout << "Array after insertion: ";


traverse(arr, size);

// Deletion
remove(arr, size, 2);
cout << "Array after deletion: ";
traverse(arr, size);

// Sorting
insert(arr, size, 35, 2);
sort(arr, size);
cout << "Array after sorting: ";
traverse(arr, size);

// Searching
int index = search(arr, size, 40);
if (index != -1)
{
cout << "Element 40 found at index: " << index << endl;
}
else
{
cout << "Element not found!" << endl;
}

return 0;
}

OUTPUT:

7
Mehnaaz Ansari
07701182023

EXPERIMENT-2

AIM: Write a C program to perform following operations on Matrices


a) Addition
b) Subtraction
c) Multiplication
d) Transpose

SOURCE CODE:
#include <iostream>
using namespace std;

void inputMatrix(int matrix[][10], int rows, int cols)


{
cout << "Enter elements of the matrix (" << rows << "x" << cols << "):" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> matrix[i][j];
}
}
}

void displayMatrix(int matrix[][10], int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

void addMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}

8
Mehnaaz Ansari
07701182023

void subtractMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}

void multiplyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows1, int
cols1, int cols2)
{
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols2; j++)
{
result[i][j] = 0;
for (int k = 0; k < cols1; k++)
{
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}

void transposeMatrix(int matrix[][10], int result[][10], int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[j][i] = matrix[i][j];
}
}
}

int main()
{
int matrix1[10][10], matrix2[10][10], result[10][10];
int rows1, cols1, rows2, cols2;
int choice;

cout << "Enter rows and columns for first matrix: ";
cin >> rows1 >> cols1;
inputMatrix(matrix1, rows1, cols1);

cout << "Enter rows and columns for second matrix: ";

9
Mehnaaz Ansari
07701182023

cin >> rows2 >> cols2;


inputMatrix(matrix2, rows2, cols2);

cout << "\nChoose operation:\n";


cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Transpose\n";
cin >> choice;

switch (choice)
{
case 1:
if (rows1 == rows2 && cols1 == cols2)
{
addMatrices(matrix1, matrix2, result, rows1, cols1);
cout << "Resultant Matrix after Addition:\n";
displayMatrix(result, rows1, cols1);
}
else
{
cout << "Matrices must have the same dimensions for addition!" << endl;
}
break;

case 2:
if (rows1 == rows2 && cols1 == cols2)
{
subtractMatrices(matrix1, matrix2, result, rows1, cols1);
cout << "Resultant Matrix after Subtraction:\n";
displayMatrix(result, rows1, cols1);
}
else
{
cout << "Matrices must have the same dimensions for subtraction!" << endl;
}
break;

case 3:
if (cols1 == rows2)
{
multiplyMatrices(matrix1, matrix2, result, rows1, cols1, cols2);
cout << "Resultant Matrix after Multiplication:\n";
displayMatrix(result, rows1, cols2);
}
else
{
cout << "Number of columns in first matrix must equal number of rows in second
matrix for multiplication!" << endl;
}

10
Mehnaaz Ansari
07701182023

break;

case 4:
transposeMatrix(matrix1, result, rows1, cols1);
cout << "Transpose of the first matrix:\n";
displayMatrix(result, cols1, rows1);

transposeMatrix(matrix2, result, rows2, cols2);


cout << "Transpose of the second matrix:\n";
displayMatrix(result, cols2, rows2);
break;

default:
cout << "Invalid choice!" << endl;
}

return 0;
}

OUTPUT:

11
Mehnaaz Ansari
07701182023

EXPERIMENT-3

AIM: Write a C program to perform following string operations


a) Concatenate two strings
b) Reverse a string
c) Find the no. of occurrences of a word in a string

SOURCE CODE:
#include <iostream>
#include <string>
using namespace std;

// Function to concatenate two strings


string concatenateStrings(const string &str1, const string &str2)
{
return str1 + str2;
}

// Function to reverse a string


string reverseString(const string &str)
{
string reversed = str;
int n = str.length();
for (int i = 0; i < n / 2; i++)
{
swap(reversed[i], reversed[n - i - 1]);
}
return reversed;
}

// Function to find the number of occurrences of a word in a string


int countOccurrences(const string &str, const string &word)
{
int count = 0;
size_t pos = str.find(word);

while (pos != string::npos)


{
count++;
pos = str.find(word, pos + word.length());
}

return count;
}

12
Mehnaaz Ansari
07701182023

int main()
{
string str1, str2, word;
int choice;

cout << "Choose an operation:\n";


cout << "1. Concatenate two strings\n";
cout << "2. Reverse a string\n";
cout << "3. Find the number of occurrences of a word in a string\n";
cin >> choice;
cin.ignore(); // To ignore the newline character after the choice input

switch (choice)
{
case 1:
cout << "Enter the first string: ";
getline(cin, str1);
cout << "Enter the second string: ";
getline(cin, str2);
cout << "Concatenated String: " << concatenateStrings(str1, str2) << endl;
break;

case 2:
cout << "Enter the string to be reversed: ";
getline(cin, str1);
cout << "Reversed String: " << reverseString(str1) << endl;
break;

case 3:
cout << "Enter the string: ";
getline(cin, str1);
cout << "Enter the word to find: ";
getline(cin, word);
cout << "The word '" << word << "' occurs " << countOccurrences(str1, word) << "
times in the string." << endl;
break;

default:
cout << "Invalid choice!" << endl;
}

return 0;
}

OUTPUT:

13
Mehnaaz Ansari
07701182023

14
Mehnaaz Ansari
07701182023

EXPERIMENT-4

AIM: Write a C program to perform following operations on a Single Linked List data
structure
a) Traversal
b) Insertion
1.Insertion after a particular node
2.Insertion before a particular node
c) Deletion
d) Reversal of a Liked List by revering the links

SOURCE CODE:
#include <iostream>
using namespace std;

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

void traverseList(Node *head)


{
Node *current = head;
while (current != nullptr)
{
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}

void insertAfter(Node *head, int target, int data)


{
Node *current = head;
while (current != nullptr && current->data != target)
{
current = current->next;
}

if (current != nullptr)
{
Node *newNode = new Node{data, current->next};
current->next = newNode;
}
else

15
Mehnaaz Ansari
07701182023

{
cout << "Node with data " << target << " not found!" << endl;
}
}

void insertBefore(Node *&head, int target, int data)


{
if (head == nullptr)
{
return;
}

if (head->data == target)
{
Node *newNode = new Node{data, head};
head = newNode;
return;
}

Node *current = head;


while (current->next != nullptr && current->next->data != target)
{
current = current->next;
}

if (current->next != nullptr)
{
Node *newNode = new Node{data, current->next};
current->next = newNode;
}
else
{
cout << "Node with data " << target << " not found!" << endl;
}
}

void deleteNode(Node *&head, int data)


{
if (head == nullptr)
{
cout << "List is empty!" << endl;
return;
}

if (head->data == data)
{
Node *temp = head;
head = head->next;
delete temp;
return;

16
Mehnaaz Ansari
07701182023

Node *current = head;


while (current->next != nullptr && current->next->data != data)
{
current = current->next;
}

if (current->next != nullptr)
{
Node *temp = current->next;
current->next = current->next->next;
delete temp;
}
else
{
cout << "Node with data " << data << " not found!" << endl;
}
}

void reverseList(Node *&head)


{
Node *prev = nullptr;
Node *current = head;
Node *next = nullptr;

while (current != nullptr)


{
next = current->next;
current->next = prev;
prev = current;
current = next;
}

head = prev;
}

int main()
{
Node *head = nullptr;

// Manual list creation


head = new Node{10, nullptr};
insertAfter(head, 10, 20);
insertAfter(head, 20, 30);

cout << "Initial List:" << endl;


traverseList(head);

// Inserting 25 after 20

17
Mehnaaz Ansari
07701182023

insertAfter(head, 20, 25);


cout << "List after inserting 25 after 20:" << endl;
traverseList(head);

// Inserting 15 before 20
insertBefore(head, 20, 15);
cout << "List after inserting 15 before 20:" << endl;
traverseList(head);

// Deleting 25
deleteNode(head, 25);
cout << "List after deleting 25:" << endl;
traverseList(head);

// Reversing the list


reverseList(head);
cout << "List after reversing:" << endl;
traverseList(head);

return 0;
}

OUTPUT:

18
Mehnaaz Ansari
07701182023

EXPERIMENT-5

AIM: Write a C program to add two Polynomial Equations using Linked List
SOURCE CODE:
#include <iostream>
using namespace std;

struct Node
{
int coeff;
int exp;
Node *next;
};

Node *createNode(int coeff, int exp)


{
Node *newNode = new Node;
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = nullptr;
return newNode;
}

void appendNode(Node *&head, int coeff, int exp)


{
Node *newNode = createNode(coeff, exp);
if (head == nullptr)
{
head = newNode;
}
else
{
Node *temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = newNode;
}
}

void displayPolynomial(Node *head)


{
Node *temp = head;
while (temp != nullptr)
{

19
Mehnaaz Ansari
07701182023

cout << temp->coeff << "x^" << temp->exp;


if (temp->next != nullptr)
{
cout << " + ";
}
temp = temp->next;
}
cout << endl;
}

Node *addPolynomials(Node *poly1, Node *poly2)


{
Node *result = nullptr;

while (poly1 != nullptr && poly2 != nullptr)


{
if (poly1->exp > poly2->exp)
{
appendNode(result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}
else if (poly1->exp < poly2->exp)
{
appendNode(result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
}
else
{
int sumCoeff = poly1->coeff + poly2->coeff;
if (sumCoeff != 0)
{ // Ignore terms with zero coefficient
appendNode(result, sumCoeff, poly1->exp);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}

while (poly1 != nullptr)


{
appendNode(result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}

while (poly2 != nullptr)


{
appendNode(result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
}

20
Mehnaaz Ansari
07701182023

return result;
}

int main()
{
Node *poly1 = nullptr;
Node *poly2 = nullptr;

// Creating the first polynomial: 5x^3 + 4x^2 + 2x^1


appendNode(poly1, 5, 3);
appendNode(poly1, 4, 2);
appendNode(poly1, 2, 1);

// Creating the second polynomial: 3x^3 + 1x^1 + 6x^0


appendNode(poly2, 3, 3);
appendNode(poly2, 1, 1);
appendNode(poly2, 6, 0);

cout << "First Polynomial: ";


displayPolynomial(poly1);

cout << "Second Polynomial: ";


displayPolynomial(poly2);

Node *result = addPolynomials(poly1, poly2);

cout << "Sum of the Polynomials: ";


displayPolynomial(result);

return 0;
}

OUTPUT:

21
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Language- C++
Compiler- VS code

AIM: Write a C program to perform following operations on a Doubly Linked List


a) Traversal
b) Insertion
c) Deletion

SOURCE CODE:
#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;
Node *prev;

Node(int data)
{
this->data = data;
this->next = nullptr;
this->prev = nullptr;
}
};

class DoublyLinkedList
{
private:
Node *head;

public:
DoublyLinkedList()
{
head = nullptr;
}

void traverseForward()
{
Node *temp = head;
while (temp != nullptr)
{
cout << temp->data << " -> ";
temp = temp->next;

22
Mehnaaz Ansari
07701182023

}
cout << "nullptr" << endl;
}

void traverseBackward()
{
if (head == nullptr)
return;

Node *temp = head;


while (temp->next != nullptr)
{
temp = temp->next;
}

while (temp != nullptr)


{
cout << temp->data << " -> ";
temp = temp->prev;
}
cout << "nullptr" << endl;
}

void insertAtBeginning(int data)


{
Node *newNode = new Node(data);
if (head != nullptr)
{
head->prev = newNode;
}
newNode->next = head;
head = newNode;
}

void insertAtEnd(int data)


{
Node *newNode = new Node(data);
if (head == nullptr)
{
head = newNode;
return;
}

Node *temp = head;


while (temp->next != nullptr)
{
temp = temp->next;
}

temp->next = newNode;

23
Mehnaaz Ansari
07701182023

newNode->prev = temp;
}

void deleteNode(int data)


{
Node *temp = head;

while (temp != nullptr && temp->data != data)


{
temp = temp->next;
}

if (temp == nullptr)
{
cout << "Node with data " << data << " not found!" << endl;
return;
}

if (temp->prev != nullptr)
{
temp->prev->next = temp->next;
}
else
{
head = temp->next;
}

if (temp->next != nullptr)
{
temp->next->prev = temp->prev;
}

delete temp;
}
};

int main()
{
DoublyLinkedList dll;

cout << "Insert 10 at the beginning." << endl;


dll.insertAtBeginning(10);
dll.traverseForward(); // Output: 10 -> nullptr

cout << "Insert 20 at the end." << endl;


dll.insertAtEnd(20);
dll.traverseForward(); // Output: 10 -> 20 -> nullptr

cout << "Insert 15 at the end." << endl;


dll.insertAtEnd(15);

24
Mehnaaz Ansari
07701182023

dll.traverseForward(); // Output: 10 -> 20 -> 15 -> nullptr

cout << "Delete node with value 20." << endl;


dll.deleteNode(20);
dll.traverseForward(); // Output: 10 -> 15 -> nullptr

cout << "Traverse the list backward." << endl;


dll.traverseBackward(); // Output: 15 -> 10 -> nullptr

return 0;
}

OUTPUT:

25
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Language- C++
Compiler- VS code

AIM: Write a C program to perform following operations on a Circular Linked List


a) Traversal
b) Insertion
c) Deletion

SOURCE CODE:
#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;

Node(int data)
{
this->data = data;
this->next = nullptr;
}
};

class CircularLinkedList
{
private:
Node *head;

public:
CircularLinkedList()
{
head = nullptr;
}

// Traversal of Circular Linked List


void traverse()
{
if (head == nullptr)
{
cout << "List is empty." << endl;
return;
}

26
Mehnaaz Ansari
07701182023

Node *temp = head;


do
{
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);

cout << "HEAD" << endl;


}

// Insertion at the end of Circular Linked List


void insertAtEnd(int data)
{
Node *newNode = new Node(data);
if (head == nullptr)
{
head = newNode;
newNode->next = head;
}
else
{
Node *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}

// Insertion at the beginning of Circular Linked List


void insertAtBeginning(int data)
{
Node *newNode = new Node(data);
if (head == nullptr)
{
head = newNode;
newNode->next = head;
}
else
{
Node *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
newNode->next = head;
temp->next = newNode;
head = newNode;

27
Mehnaaz Ansari
07701182023

}
}

// Deletion of a node with a specific value


void deleteNode(int data)
{
if (head == nullptr)
{
cout << "List is empty." << endl;
return;
}

Node *temp = head;


Node *prev = nullptr;

// If head node holds the key to be deleted


if (head->data == data)
{
// If there is only one node
if (head->next == head)
{
delete head;
head = nullptr;
return;
}

// Find the last node


while (temp->next != head)
{
temp = temp->next;
}

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

// If the node to be deleted is not the head


do
{
prev = temp;
temp = temp->next;
} while (temp != head && temp->data != data);

if (temp == head)
{
cout << "Node with value " << data << " not found." << endl;
return;
}

28
Mehnaaz Ansari
07701182023

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

int main()
{
CircularLinkedList cll;

cout << "Insert 10 at the end." << endl;


cll.insertAtEnd(10);

cout << "Insert 20 at the end." << endl;


cll.insertAtEnd(20);

cout << "Insert 5 at the beginning." << endl;


cll.insertAtBeginning(5);

cout << "Circular Linked List (Traversal):" << endl;


cll.traverse(); // Output: 5 -> 10 -> 20 -> HEAD

cout << "Delete node with value 20." << endl;


cll.deleteNode(20);

cout << "Circular Linked List after deletion (Traversal):" << endl;
cll.traverse(); // Output: 5 -> 10 -> HEAD

return 0;
}

OUTPUT:

29
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Language- C++
Compiler- VS code

AIM: Write a C program to implement Stack using Array.


SOURCE CODE:
#include <iostream>
using namespace std;

class Stack
{
private:
int top;
int arr[5]; // Array to hold stack elements with a fixed size of 5

public:
Stack()
{
top = -1; // Initialize top to -1 indicating that the stack is empty
for (int i = 0; i < 5; i++)
{
arr[i] = 0; // Initialize all elements of the array to 0
}
}

bool isEmpty()
{
return top == -1;
}

bool isFull()
{
return top == 4; // Since the array size is 5, the last index is 4
}

void push(int val)


{
if (isFull())
{
cout << "Stack overflow" << endl;
}
else
{
top++; // Increment the top index
arr[top] = val; // Insert the value at the top
cout << val << " pushed into stack." << endl;
}

30
Mehnaaz Ansari
07701182023

int pop()
{
if (isEmpty())
{
cout << "Stack underflow" << endl;
return 0;
}
else
{
int popValue = arr[top]; // Store the value to be popped
arr[top] = 0; // Reset the position in the array
top--; // Decrement the top index
cout << popValue << " popped from stack." << endl;
return popValue; // Return the popped value
}
}

int count()
{
return (top + 1); // Number of elements in the stack
}

int peek(int pos)


{
if (pos < 0 || pos > top)
{
cout << "Invalid position" << endl;
return 0;
}
else
{
return arr[pos]; // Return the element at the given position
}
}

void change(int pos, int val)


{
if (pos < 0 || pos > top)
{
cout << "Invalid position" << endl;
}
else
{
arr[pos] = val; // Change the value at the given position
cout << "Value changed at position " << pos << " to " << val << endl;
}
}

31
Mehnaaz Ansari
07701182023

void display()
{
cout << "All values in the Stack are: " << endl;
for (int i = top; i >= 0; i--)
{
cout << arr[i] << endl; // Print stack elements from top to bottom
}
}
};

int main()
{
Stack s1;

s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.push(50);
s1.push(60); // Stack overflow test

s1.display();

s1.pop();
s1.display();

s1.change(2, 100); // Change value at position 2 (0-based index)


s1.display();

cout << "Element at position 1 is: " << s1.peek(1) << endl; // Peek at position 1

return 0;
}

OUTPUT:

32
Mehnaaz Ansari
07701182023

33
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Language- C++
Compiler- VS code

AIM: Write a C program to implement Stack using Linked List.

SOURCE CODE:
#include <iostream>
using namespace std;

// Node class represents each element in the stack


class Node
{
public:
int data; // Data stored in the node
Node *next; // Pointer to the next node in the stack

Node(int val)
{
data = val; // Initialize data with the passed value
next = nullptr; // Initialize next pointer as null
}
};

// Stack class represents the stack using a linked list


class Stack
{
private:
Node *top; // Pointer to the top of the stack

public:
Stack()
{
top = nullptr; // Initialize top as null, indicating an empty stack
}

// Method to check if the stack is empty


bool isEmpty()
{
return top == nullptr;
}

// Method to push an element onto the stack


void push(int val)
{
Node *newNode = new Node(val); // Create a new node
newNode->next = top; // Point the new node to the current top

34
Mehnaaz Ansari
07701182023

top = newNode; // Update the top to the new node


cout << "Pushed " << val << " onto the stack." << endl;
}

// Method to pop an element from the stack


int pop()
{
if (isEmpty())
{
cout << "Stack underflow" << endl;
return -1;
}
else
{
Node *temp = top; // Temporarily store the current top
int poppedValue = temp->data; // Store the data of the top node
top = top->next; // Move the top pointer to the next node
delete temp; // Delete the old top node
return poppedValue; // Return the popped value
}
}

// Method to peek at the top element without removing it


int peek()
{
if (isEmpty())
{
cout << "Stack is empty" << endl;
return -1;
}
else
{
return top->data; // Return the data of the top node
}
}

// Method to display all elements in the stack


void display()
{
if (isEmpty())
{
cout << "Stack is empty" << endl;
}
else
{
Node *temp = top;
cout << "Stack elements are: ";
while (temp != nullptr)
{
cout << temp->data << " "; // Print the data of each node

35
Mehnaaz Ansari
07701182023

temp = temp->next; // Move to the next node


}
cout << endl;
}
}
};

int main()
{
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.display();

cout << "Popped element: " << s.pop() << endl;


s.display();

cout << "Top element: " << s.peek() << endl;


s.display();

return 0;
}

OUTPUT:

36
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Language- C++
Compiler- VS code

AIM: Write a C program to implement Queue using Array.

SOURCE CODE:

OUTPUT:

37
Mehnaaz Ansari
07701182023

EXPERIMENT-11
Language- C++
Compiler- VS code

AIM:
SOURCE CODE:
OUTPUT:

38
Mehnaaz Ansari
07701182023

EXPERIMENT-12
Language- C++
Compiler- VS code

AIM:
SOURCE CODE:
OUTPUT:

39
Mehnaaz Ansari
07701182023

EXPERIMENT-13
Language- C++
Compiler- VS code

AIM:
SOURCE CODE:
OUTPUT:

40
Mehnaaz Ansari
07701182023

EXPERIMENT-14
Language- C++
Compiler- VS code

AIM:
SOURCE CODE:
OUTPUT:

41
Mehnaaz Ansari
07701182023

EXPERIMENT-15
Language- C++
Compiler- VS code

AIM:
SOURCE CODE:
OUTPUT:

42

You might also like