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