0% found this document useful (0 votes)
16 views14 pages

Experiment 8

Uploaded by

siaish388
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)
16 views14 pages

Experiment 8

Uploaded by

siaish388
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/ 14

EXPERIMENT 8

Aim - Write a Cpp program to implement Stack using Array

Ans –

#include <iostream>

using namespace std;

#define MAX 100 // Define the maximum size of the stack

class Stack {

private:

int arr[MAX];

int top;

public:

Stack() : top(-1) {} // Constructor to initialize the stack

bool isFull() {

return top == MAX - 1;

bool isEmpty() {

return top == -1;

void push(int value) {

if (isFull()) {

cout << "Stack overflow! Cannot push " << value << ".\n";

return;

arr[++top] = value;

cout << "Pushed " << value << " onto the stack.\n";
}

int pop() {

if (isEmpty()) {

cout << "Stack underflow! Cannot pop.\n";

return -1;

return arr[top--];

void display() {

if (isEmpty()) {

cout << "Stack is empty.\n";

return;

cout << "Stack elements:\n";

for (int i = top; i >= 0; i--) {

cout << arr[i] << "\n";

};

int main() {

Stack stack;

int choice, value;

while (true) {

cout << "\nStack Operations:\n";

cout << "1. Push\n";

cout << "2. Pop\n";

cout << "3. Display\n";


cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

if (choice == 1) {

cout << "Enter value to push: ";

cin >> value;

stack.push(value);

} else if (choice == 2) {

value = stack.pop();

if (value != -1)

cout << "Popped value: " << value << "\n";

} else if (choice == 3) {

stack.display();

} else if (choice == 4) {

cout << "Exiting...\n";

break;

} else {

cout << "Invalid choice! Please try again.\n";

return 0;

Experiment 9 - Write a Cpp program to implement Stack using Linked List.

Ans -

#include <iostream>

using namespace std;

// Define the node structure


struct Node {

int data;

Node* next;

};

// Define the Stack class

class Stack {

private:

Node* top; // Pointer to the top of the stack

public:

// Constructor to initialize an empty stack

Stack() {

top = nullptr;

// Push operation: Add an element to the stack

void push(int value) {

Node* newNode = new Node(); // Create a new node

newNode->data = value; // Set its data

newNode->next = top; // Link it to the current top

top = newNode; // Update the top pointer

cout << "Pushed " << value << " onto the stack.\n";

// Pop operation: Remove and return the top element

int pop() {

if (isEmpty()) {

cout << "Stack underflow! Cannot pop.\n";

return -1; // Indicate failure

}
Node* temp = top; // Temporary pointer to the top node

int poppedValue = top->data; // Store the data of the top node

top = top->next; // Update the top pointer

delete temp; // Free memory of the popped node

return poppedValue; // Return the popped value

// Check if the stack is empty

bool isEmpty() {

return top == nullptr;

// Display all the elements in the stack

void display() {

if (isEmpty()) {

cout << "Stack is empty.\n";

return;

Node* temp = top;

cout << "Stack elements:\n";

while (temp != nullptr) {

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

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

// Destructor to free memory allocated for the stack

~Stack() {

while (!isEmpty()) {

pop(); // Continuously pop elements to release memory

}
}

};

int main() {

Stack stack; // Create a stack object

int choice, value;

while (true) {

cout << "\nStack Operations:\n";

cout << "1. Push\n";

cout << "2. Pop\n";

cout << "3. Display\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

if (choice == 1) {

cout << "Enter value to push: ";

cin >> value;

stack.push(value);

} else if (choice == 2) {

value = stack.pop();

if (value != -1)

cout << "Popped value: " << value << "\n";

} else if (choice == 3) {

stack.display();

} else if (choice == 4) {

cout << "Exiting...\n";

break;

} else {

cout << "Invalid choice! Please try again.\n";


}

return 0;

EXPERIMENT 10
AIM - Write a CPP program to implement Queue using Array.

ANS –

#include <iostream>

using namespace std;

#define MAX 100 // Define the maximum size of the queue

class Queue {

private:

int arr[MAX];

int front;

int rear;

public:

// Constructor to initialize the queue

Queue() {

front = -1;

rear = -1;

// Check if the queue is full

bool isFull() {

return rear == MAX - 1;


}

// Check if the queue is empty

bool isEmpty() {

return front == -1 || front > rear;

// Enqueue operation: Add an element to the queue

void enqueue(int value) {

if (isFull()) {

cout << "Queue overflow! Cannot enqueue " << value << ".\n";

return;

if (front == -1) // Initialize front on the first enqueue

front = 0;

arr[++rear] = value;

cout << "Enqueued " << value << " to the queue.\n";

// Dequeue operation: Remove and return the front element

int dequeue() {

if (isEmpty()) {

cout << "Queue underflow! Cannot dequeue.\n";

return -1; // Indicate failure

int dequeuedValue = arr[front++];

if (front > rear) // Reset queue when all elements are dequeued

front = rear = -1;

return dequeuedValue;

}
// Display all elements in the queue

void display() {

if (isEmpty()) {

cout << "Queue is empty.\n";

return;

cout << "Queue elements:\n";

for (int i = front; i <= rear; i++) {

cout << arr[i] << " ";

cout << "\n";

};

int main() {

Queue queue;

int choice, value;

while (true) {

cout << "\nQueue Operations:\n";

cout << "1. Enqueue\n";

cout << "2. Dequeue\n";

cout << "3. Display\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

if (choice == 1) {

cout << "Enter value to enqueue: ";

cin >> value;

queue.enqueue(value);
} else if (choice == 2) {

value = queue.dequeue();

if (value != -1)

cout << "Dequeued value: " << value << "\n";

} else if (choice == 3) {

queue.display();

} else if (choice == 4) {

cout << "Exiting...\n";

break;

} else {

cout << "Invalid choice! Please try again.\n";

return 0;

Experiment – 11
Aim – Write a C program to implement Queue using linked list

#include <iostream>

using namespace std;

// Node structure for the linked list

struct Node {

int data;

Node* next;

};

// Queue class using linked list

class Queue {

private:

Node* front; // Points to the front of the queue


Node* rear; // Points to the rear of the queue

public:

// Constructor to initialize an empty queue

Queue() {

front = nullptr;

rear = nullptr;

// Enqueue operation: Add an element to the queue

void enqueue(int value) {

Node* newNode = new Node(); // Create a new node

newNode->data = value; // Set its data

newNode->next = nullptr; // Set the next pointer to nullptr

if (rear == nullptr) { // If the queue is empty

front = rear = newNode;

cout << "Enqueued " << value << " to the queue.\n";

return;

rear->next = newNode; // Link the new node at the end of the queue

rear = newNode; // Update the rear pointer

cout << "Enqueued " << value << " to the queue.\n";

// Dequeue operation: Remove and return the front element

int dequeue() {

if (front == nullptr) { // If the queue is empty

cout << "Queue underflow! Cannot dequeue.\n";

return -1; // Indicate failure


}

Node* temp = front; // Temporary pointer to the front node

int dequeuedValue = front->data; // Get the data of the front node

front = front->next; // Update the front pointer

if (front == nullptr) // If the queue becomes empty

rear = nullptr;

delete temp; // Free the memory of the dequeued node

return dequeuedValue; // Return the dequeued value

// Display all elements in the queue

void display() {

if (front == nullptr) {

cout << "Queue is empty.\n";

return;

Node* temp = front;

cout << "Queue elements: ";

while (temp != nullptr) {

cout << temp->data << " ";

temp = temp->next;

cout << "\n";

// Destructor to free memory allocated for the queue

~Queue() {
while (front != nullptr) {

Node* temp = front;

front = front->next;

delete temp;

};

int main() {

Queue queue;

int choice, value;

while (true) {

cout << "\nQueue Operations:\n";

cout << "1. Enqueue\n";

cout << "2. Dequeue\n";

cout << "3. Display\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

if (choice == 1) {

cout << "Enter value to enqueue: ";

cin >> value;

queue.enqueue(value);

} else if (choice == 2) {

value = queue.dequeue();

if (value != -1)

cout << "Dequeued value: " << value << "\n";

} else if (choice == 3) {

queue.display();
} else if (choice == 4) {

cout << "Exiting...\n";

break;

} else {

cout << "Invalid choice! Please try again.\n";

return 0;

You might also like