

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This c++ code demonstrates the implementation of a stack data structure using an array. It includes basic stack operations such as push, pop, isempty, and isfull. The code provides a menu-driven interface for users to interact with the stack, allowing them to push elements onto the stack, pop elements from the stack, and display the current contents of the stack. This example is useful for understanding the fundamental principles of stack data structures and their practical implementation in c++.
Typology: Lecture notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


#include
void STACK::displayItems() { cout << "STACK is: "; for (int i = top; i >= 0; i--) cout << num[i] << " "; cout << endl; } int main() { STACK stk; int choice, n, temp; do { cout << "\n0 - Exit." << endl; cout << "1 - Push Item." << endl; cout << "2 - Pop Item." << endl; cout << "3 - Display Items (Print STACK)." << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 0: break; case 1: cout << "Enter item to insert: "; cin >> n; temp = stk.push(n); if (temp == -1) cout << "STACK is FULL." << endl; else cout << temp << " inserted." << endl; break; case 2: temp = stk.pop(); if (temp == -1) cout << "STACK IS EMPTY." << endl; else cout << temp << " is removed (popped)." << endl; break; case 3: stk.displayItems(); break; default: cout << "An Invalid choice." << endl; } } while (choice != 0); return 0; }