0% found this document useful (0 votes)
3 views4 pages

Dsa 5

The document contains a C++ program that implements a Stack and a Queue with various operations such as push, pop, and traverse. The Stack maintains elements in ascending order during insertion, while the Queue allows for specific element removal and maintains order. A menu-driven interface is provided for users to interact with both data structures.

Uploaded by

wifiwev250
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)
3 views4 pages

Dsa 5

The document contains a C++ program that implements a Stack and a Queue with various operations such as push, pop, and traverse. The Stack maintains elements in ascending order during insertion, while the Queue allows for specific element removal and maintains order. A menu-driven interface is provided for users to interact with both data structures.

Uploaded by

wifiwev250
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
You are on page 1/ 4

dsa5.

cpp Fri Aug 08 10:25:50 2025 1


#include <iostream>
using namespace std;
#define MAX_SIZE 100 // Define the maximum size of the stack and queue
class Stack
{
private:
int arr[MAX_SIZE]; // Array to hold stack elements
int top; // Top pointer to track the current top element
public:
// Constructor to initialize the stack
Stack()
{
top = -1; // Stack is initially empty
}
// Function to check if the stack is empty
bool isEmpty()
{
return top == -1;
}
// Function to check if the stack is full
bool isFull()
{
return top == MAX_SIZE - 1;
}
// Function to push an element onto the stack
void push(int data)
{
if (isFull())
{
cout << "Stack overflow." << endl;
return;
}
int tempArr[MAX_SIZE]; // Temporary array to hold popped elements
int tempTop = -1; // Temporary top pointer for tempArr
// (ascending order)
while (!isEmpty() && peek() < data)
{
tempArr[++tempTop] = peek(); // Store top element in tempArr
pop(); // Pop the top element
}
// Insert the new element
arr[++top] = data;
// Push the popped elements back onto the stack
while (tempTop != -1)
{
arr[++top] = tempArr[tempTop--];
}
}
// Function to pop an element from the stack
void pop()
{
if (isEmpty())
{
cout << "Stack underflow." << endl;
return;
}
top--; // Decrement the top pointer to remove the top element
}
// Function to traverse the stack and print its elements
void traverse()
{
if (isEmpty())
{
cout << "Stack is empty." << endl;
return;
}
for (int i = top; i >= 0; i--)
{
cout << arr[i] << " "; // Print elements from top to bottom
}
dsa5.cpp Fri Aug 08 10:25:50 2025 2
cout << endl;
}
// Function to get the top element of the stack
int peek()
{
if (isEmpty())
{
cout << "Stack is empty." << endl;
return -1; // Return -1 to indicate the stack is empty
}
return arr[top]; // Return the top element
}
};
// Queue class definition
class Queue
{
private:
int arr[MAX_SIZE]; // Array to hold queue elements
int front, rear; // Front and rear pointers for the queue
public:
// Constructor to initialize the queue
Queue()
{
front = -1;
rear = -1; // Queue is initially empty
}
// Function to check if the queue is empty
bool isEmptyQ()
{
return front == -1;
}
// Function to check if the queue is full
bool isFullQ()
{
return rear == MAX_SIZE - 1;
}
// Function to insert an element into the queue in ascending order
void insertQ(int data)
{
if (isFullQ())
{
cout << "Queue overflow." << endl;
return;
}
// If queue is empty
if (isEmptyQ())
{
front = rear = 0;
arr[rear] = data;
return;
}
// Find the correct position for insertion (ascending order)
int i;
for (i = rear; i >= front && arr[i] > data; i--)
{
arr[i + 1] = arr[i]; // Shift elements to make space for new data
}
arr[i + 1] = data; // Insert the new element in the correct position
rear++; // Update rear pointer
}
// Function to remove a specific element from the queue
void popQ(int data)
{
if (isEmptyQ())
{
cout << "Queue is empty." << endl;
return;
}
// Find the element to pop
int i;
dsa5.cpp Fri Aug 08 10:25:50 2025 3
for (i = front; i <= rear; i++)
{
if (arr[i] == data)
{
break; // Element found
}
}
if (i > rear)
{
cout << "Item not found in queue." << endl;
return;
}
// Shift elements to fill the gap
for (int j = i; j < rear; j++)
{
arr[j] = arr[j + 1];
}
rear--; // Update rear pointer
// If the queue is now empty, reset front and rear pointers
if (rear < front)
{
front = rear = -1;
}
}
// Function to traverse the queue and print its elements
void traverseQ()
{
if (isEmptyQ())
{
cout << "Queue is empty." << endl;
return;
}
for (int i = front; i <= rear; i++)
{
cout << arr[i] << " "; // Print elements from front to rear
}
cout << endl;
}
};
// Main function to handle menu-driven operations for Stack and Queue
int main()
{
Stack s; // Create a Stack object
Queue q; // Create a Queue object
int choice, data;
// Main menu loop
while (true)
{
cout << "\nSelect an option:\n";
cout << "1. Stack Operations\n";
cout << "2. Queue Operations\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1: // Stack Operations
while (true)
{
cout << "\nStack Operations:\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "4. Traverse\n";
cout << "5. Back to Main Menu\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
dsa5.cpp Fri Aug 08 10:25:50 2025 4
cout << "Enter data to push: ";
cin >> data;
s.push(data); // Push data onto the stack
break;
case 2:
s.pop(); // Pop data from the stack
break;
case 3:
cout << "Top element: " << s.peek() << endl; // Peek top element
break;
case 4:
cout << "Stack elements: ";
s.traverse(); // Traverse stack elements
break;
case 5:
break; // Return to main menu
default:
cout << "Invalid choice." << endl;
}
if (choice == 5) break; // Break the loop if user chooses to return to ma
in menu
}
break;
case 2: // Queue Operations
while (true)
{
cout << "\nQueue Operations:\n";
cout << "1. Insert\n";
cout << "2. Pop\n";
cout << "3. Traverse\n";
cout << "4. Back to Main Menu\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter data to insert: ";
cin >> data;
q.insertQ(data); // Insert data into the queue
break;
case 2:
cout << "Enter data to pop: ";
cin >> data;
q.popQ(data); // Remove specific data from the queue
break;
case 3:
cout << "Queue elements: ";
q.traverseQ(); // Traverse queue elements
break;
case 4:
break; // Return to main menu
default:
cout << "Invalid choice." << endl;
}
if (choice == 4) break; // Break the loop if user chooses to return to ma
in menu
}
break;
case 3:
cout << "Exiting program." << endl;
return 0; // Exit the program

default:
cout << "Invalid choice." << endl;
}
}
return 0;
}

You might also like