bhupixb

Stack

Jan 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. // I can't tell you what it really is,
  2. // I can only tell you what it feels like.
  3. #include "bits/stdc++.h"
  4.  
  5. using namespace std;
  6. class node{
  7. public:
  8. int data;
  9. node *next;
  10. node() {
  11. next = NULL;
  12. }
  13. };
  14. node *top;
  15. void add(int x) {
  16. if(top -> next == NULL) {
  17. top -> data = x;
  18. top -> next = top;
  19. } else {
  20. node *temp = new node;
  21. temp -> data = x;
  22. temp -> next = top;
  23. top = temp;
  24. }
  25. }
  26. void del() {
  27. if(top -> next == NULL) {
  28. cout << "Stack is Empty\n";
  29. } else {
  30. if(top == top -> next) {
  31. top -> next = NULL;
  32. } else {
  33. node *temp = top;
  34. top = top -> next;
  35. delete temp;
  36. }
  37. }
  38. }
  39. int on() {
  40. if(top -> next == NULL)
  41. return -1;
  42. else
  43. return top -> data;
  44. }
  45. int main() {
  46. int ch, num;
  47. top = new node;
  48. while(1) {
  49. cin >> ch;
  50. if(ch == 1) {
  51. cout << "enter num ";
  52. cin >> num;
  53. add(num);
  54. }
  55. else if(ch == 2){
  56. del();
  57. }
  58. else if(ch == 3)
  59. cout << on() << '\n';
  60. else
  61. break;
  62. }
  63. }
Add Comment
Please, Sign In to add comment