0% found this document useful (0 votes)
6 views9 pages

Tanguin2B Stacks MachProb DSA

This document outlines a machine problem for a Data Structures and Algorithms course, focusing on implementing a stack using arrays. It describes the stack's operations: push, pop, and display, along with user interaction through a menu-driven interface. The implementation emphasizes memory management and boundary checking while providing a practical tool for managing data in a Last In, First Out manner.

Uploaded by

lancetanguin
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)
6 views9 pages

Tanguin2B Stacks MachProb DSA

This document outlines a machine problem for a Data Structures and Algorithms course, focusing on implementing a stack using arrays. It describes the stack's operations: push, pop, and display, along with user interaction through a menu-driven interface. The implementation emphasizes memory management and boundary checking while providing a practical tool for managing data in a Last In, First Out manner.

Uploaded by

lancetanguin
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/ 9

Bicol University

Polangui

Data Structures and


Algorithm
Machine Problem:
(Stacks)

By: Lancelot R. Tanguin


Course & Block: BSCpE 2B
Professor: Melissa Jeankie Satiada-Rellon

Statement Problem

In many real-world scenarios, we encounter situations where data needs to


be managed in a Last In, First Out (LIFO) manner. For example, when
undoing operations in a text editor, reversing a string, or managing a
browser's back button functionality. To handle such situations
programmatically, the stack data structure is highly effective. This project
focuses on implementing a stack using arrays to allow users to perform three
primary operations: push, pop, and display. The program starts by allowing
the user to define the maximum size of the stack, ensuring that the memory
allocation is efficient and meets the requirements of the application. The first
operation, push, enables the user to add an element to the top of the stack.
This operation is essential when new data needs to be added dynamically.
However, if the stack is already full, the program detects this and prevents
overflow, notifying the user that the stack is full.

The second operation, pop, allows the user to remove the topmost element from the stack. This
operation is critical for scenarios where the most recently added data is no longer needed. If the
stack is empty when this operation is attempted, the program prevents underflow and alerts the
user that no elements are available for removal.

Lastly, the display operation provides a clear view of the stack's contents, starting from the
topmost element to the bottom. This feature ensures transparency, allowing users to verify the
current state of the stack at any time.

To make the program user-friendly, it employs a menu-driven interface. Users


can perform multiple operations sequentially until they choose to exit. The
program ensures robust memory management by dynamically allocating and
deallocating memory for the stack array. This project demonstrates the
practicality of stacks in managing data efficiently and introduces key
programming concepts such as boundary checking, memory management,
and interactive design. The implementation offers a versatile tool for
handling LIFO-based problems while providing a solid foundation for learning
advanced data structures.
Flowchart
Code

#include <iostream>

using namespace std;

class Stack {

private:

int top; // Index of the top element

int size; // Maximum size of the stack

int* stack; // Array to hold stack elements

public:

// Constructor

Stack(int maxSize) {

size = maxSize;

stack = new int[size];

top = -1;

// Push operation

void push(int value) {

if (top == size - 1) {

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

} else {
stack[++top] = value;

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

// Pop operation

void pop() {

if (top == -1) {

cout << "Stack Underflow! The stack is empty.\n";

} else {

cout << "Popped " << stack[top--] << " from the stack.\n";

// Display stack

void display() {

if (top == -1) {

cout << "The stack is empty.\n";

} else {

cout << "Stack elements (top to bottom): ";

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

cout << stack[i] << " ";

cout << "\n";

}
// Destructor

~Stack() {

delete[] stack;

cout << "Stack memory freed.\n";

};

int main() {

int maxSize, choice, value;

cout << "Enter the maximum size of the stack: ";

cin >> maxSize;

Stack stack(maxSize);

do {

cout << "\nMenu:\n";

cout << "1. Push\n";

cout << "2. Pop\n";

cout << "3. Display Stack\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter value to push: ";

cin >> value;


stack.push(value);

break;

case 2:

stack.pop();

break;

case 3:

stack.display();

break;

case 4:

cout << "Exiting program.\n";

break;

default:

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

} while (choice != 4);

return 0;

You might also like