Linked Stack Data Structure
A simple linked stack data structure implementation in C++ with a demo command-line interface.
#include<iostream.h> #include<stdio.h> #include<conio.h> #include<process.h> struct node { int rollno; int age; struct node *next; }; class stack { struct node *top; public: stack() { top=NULL; } void push(); // to insert an element void pop(); // to delete an element void show(); // to show the stack }; // PUSH Operation void stack::push() { int value; struct node *ptr=new node; cout<<"Enter a rollno: "; cin>>ptr->rollno; cout<<"Enter age: "; cin>>ptr->age; ptr->next=NULL; if(top!=NULL) ptr->next=top; top=ptr; cout<<"New item is inserted to the stack!!!"<<endl; show(); } // POP Operation void stack::pop() { struct node *temp; if(top==NULL) { cout<<"nThe stack is empty!!!"<<endl; } temp=top; top=top->next; cout<<"nPOP Operation........nPoped value is "<<temp->rollno<<" "<<temp->age<<endl; delete temp; show(); } // Show stack void stack::show() { struct node *ptr1=top; cout<<"nThe stack is"<<endl; while(ptr1!=NULL) { cout<<ptr1->rollno<<" "<<ptr1->age<<" ->"<<endl; ptr1=ptr1->next; } cout<<"!!!"; } // Main function void main() { clrscr(); stack s; int choice; l1: cout<<"Main Menu"<<endl; cout<<"_______________________________________________________"<<endl; cout<<"1:PUSH"<<endl; cout<<"2:POP"<<endl; cout<<"3:DISPLAY STACK"<<endl; cout<<"4:EXIT"<<endl; cout<<"_______________________________________________________"<<endl; cin>>choice; switch(choice) { case 1: s.push(); goto l1; case 2: s.pop(); goto l1; case 3: s.show(); goto l1; case 4: exit(0); } }

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Download this code in plain text format here