Example 1: C++ implementation of the stack and its operations
kw.cpp
#include<iostream>usingnamespacestd;intstack[100];inttop=-1;intmain(){intn,e;cout<<"———————————————————————————————————————————";cout<<"\nImplementation of a stack\n";cout<<"———————————————————————————————————————————";cout<<"\nNumbers of elements in a Stack ";cin>>n;for(inti=0;i<n;i++){cin>>e;stack[++top]=e;}if(top<0){cout<<"\nStack is empty";}cout<<"\nStack view\n";for(inti=top;i>=0;i--){cout<<"| "<<stack[i]<<" |"<<endl;}cout<<"———————————————————————————————————————————\n";return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Implementation of a stack
———————————————————————————————————————————
Numbers of elements in a Stack 5
7
6
5
2
0
Stack view
| 0 |
| 2 |
| 5 |
| 6 |
| 7 |
———————————————————————————————————————————
kodingwindow@kw:~$
Example 2: C++ implementation of the stack and its operations
kw.cpp
#include<iostream>usingnamespacestd;classStack{intstack[100];inttop;public:Stack(){top=-1;}voidpush(inte){stack[++top]=e;}voidpop(){if(top<0){cout<<"\nStack underflow";return;}cout<<"\nTop element popped "<<stack[top--]<<endl;}voiddisplay(){cout<<"\nStack view\n";for(inti=top;i>=0;i--){cout<<"| "<<stack[i]<<" |"<<endl;}}};intmain(){intch,n,e;Stacks;while(ch!=3){cout<<"———————————————————————————————————————————";cout<<"\nImplementation of a stack operations\n";cout<<"———————————————————————————————————————————";cout<<"\n1.Push\n2.Pop\n3.Exit\n";cout<<"———————————————————————————————————————————";cout<<"\nEnter your choice ";cin>>ch;switch(ch){case1:cout<<"\nNumbers of elements in a Stack ";cin>>n;for(inti=0;i<n;i++){cin>>e;s.push(e);}s.display();break;case2:s.pop();s.display();break;case3:exit(0);default:cout<<"\nWrong choice entered... Please try again!\n";}}return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Implementation of a stack operations
———————————————————————————————————————————
1.Push
2.Pop
3.Exit
———————————————————————————————————————————
Enter your choice 1
Numbers of elements in a Stack 3
56
89
14
Stack view
| 14 |
| 89 |
| 56 |
———————————————————————————————————————————
Implementation of a stack operations
———————————————————————————————————————————
1.Push
2.Pop
3.Exit
———————————————————————————————————————————
Enter your choice 2
Top element popped 14
Stack view
| 89 |
| 56 |
———————————————————————————————————————————
Implementation of a stack operations
———————————————————————————————————————————
1.Push
2.Pop
3.Exit
———————————————————————————————————————————
Enter your choice 3
kodingwindow@kw:~$
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?