Docsity

STACK CODE IN DEV DSA, Lab Reports of Data Structures and Algorithms

Partial preview of the text

Download STACK CODE IN DEV DSA and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

#include #include <string.h> using namespace std; class Stack { struct node { int data; node* next; }; private: node* top; public: Stack() { top = NULL; } void push(int c) { clearScreen(); node* temp; temp = new node; temp -> data = c; temp -> next = top;

top = temp; cout << "Last Operation: Data PUSHED in Stack"<< endl<< endl; } void pop() { clearScreen(); node temp; temp=top; if(top==NULL) { cout<<"Stack UnderFlow"<<endl<<endl; }else { cout << "Last Operation: Data POP from Stack: " << top -> data<< endl<< endl; top = top->next; } delete temp; } void display() { cout << "top -> "; node curr = top; while (curr != NULL) { cout << curr -> data << " -> ";

void clearScreen() { system("CLS"); cout << "******** STACK ********" << endl << endl; } }; int main() { Stack st; system("CLS"); cout << "******** STACK ********" << endl << endl; int choice = -1; while (choice != 0) { st.display(); cout << endl << endl; cout << "Press 1 : Push data to STACK" << endl; cout << "Press 2 : Pop data from STACK" << endl; cout << "Press 3 : Display data of STACK" << endl; cout << "Press 0 : Exit" << endl; choice = -1; while (choice < 0 || choice > 3) { cout << "\nEnter your choice here: "; cin >> choice; cout << endl; }

int data; switch (choice) { case 1: cout << "Enter Data: "; cin >> data; st.push(data); break; case 2: st.pop(); break; case 3: st.displayInDetail(); break; default: break; } } }