BlankOD

Untitled

Mar 12th, 2022
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. struct Node
  2. {
  3.     int data;
  4.     Node* link;
  5. };
  6.  
  7. Node* top;
  8.  
  9. // Utility function to add an element
  10. // data in the stack insert at the beginning
  11. void push(int data)
  12. {
  13.      
  14.     // Create new node temp and allocate memory in heap
  15.     Node* temp = new Node();
  16.  
  17.     // Check if stack (heap) is full.
  18.     // Then inserting an element would
  19.     // lead to stack overflow
  20.     if (!temp)
  21.     {
  22.         cout << "\nStack Overflow";
  23.         exit(1);
  24.     }
  25.  
  26.     // Initialize data into temp data field
  27.     temp->data = data;
  28.  
  29.     // Put top pointer reference into temp link
  30.     temp->link = top;
  31.  
  32.     // Make temp as top of Stack
  33.     top = temp;
  34. }
  35.  
  36. // Utility function to check if
  37. // the stack is empty or not
  38. int isEmpty()
  39. {
  40.     //If top is NULL it means that
  41.     //there are no elements are in stack
  42.     return top == NULL;
  43. }
  44.  
  45. // Utility function to return top element in a stack
  46. int peek()
  47. {
  48.      
  49.     // If stack is not empty , return the top element
  50.     if (!isEmpty())
  51.         return top->data;
  52.     else
  53.         exit(1);
  54. }
  55.  
  56. // Utility function to pop top
  57. // element from the stack
  58. void pop()
  59. {
  60.     Node* temp;
  61.  
  62.     // Check for stack underflow
  63.     if (top == NULL)
  64.     {
  65.         cout << "\nStack Underflow" << endl;
  66.         exit(1);
  67.     }
  68.     else
  69.     {
  70.          
  71.         // Assign top to temp
  72.         temp = top;
  73.  
  74.         // Assign second node to top
  75.         top = top->link;
  76.  
  77.         //This will automatically destroy
  78.         //the link between first node and second node
  79.  
  80.         // Release memory of top node
  81.         //i.e delete the node
  82.         free(temp);
  83.     }
  84. }
  85.  
  86. // Function to print all the
  87. // elements of the stack
  88. void display()
  89. {
  90.     Node* temp;
  91.  
  92.     // Check for stack underflow
  93.     if (top == NULL)
  94.     {
  95.         cout << "\nStack Underflow";
  96.         exit(1);
  97.     }
  98.     else
  99.     {
  100.         temp = top;
  101.         while (temp != NULL)
  102.         {
  103.  
  104.             // Print node data
  105.             cout << temp->data << "-> ";
  106.  
  107.             // Assign temp link to temp
  108.             temp = temp->link;
  109.         }
  110.     }
  111. }
  112.  
  113. // Driver Code
  114. int main()
  115. {
  116.      
  117.     // Push the elements of stack
  118.     push(11);
  119.     push(22);
  120.     push(33);
  121.     push(44);
  122.  
  123.     // Display stack elements
  124.     display();
  125.  
  126.     // Print top element of stack
  127.     cout << "\nTop element is "
  128.         << peek() << endl;
  129.  
  130.     // Delete top elements of stack
  131.     pop();
  132.     pop();
  133.  
  134.     // Display stack elements
  135.     display();
  136.  
  137.     // Print top element of stack
  138.     cout << "\nTop element is "
  139.         << peek() << endl;
  140.          
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment