|
| 1 | +Stack |
| 2 | +=== |
| 3 | +A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This means the last element inserted inside the stack is removed first. |
| 4 | + |
| 5 | +Stack Representation |
| 6 | +---- |
| 7 | +    <img src="https://www.tutorialspoint.com/data_structures_algorithms/images/stack_representation.jpg" alt="Stack Representation" width="30%"/> |
| 8 | +    <img src="https://cdn.programiz.com/sites/tutorial2program/files/stack.png" alt="Stack Representation" width="45%"/> |
| 9 | + |
| 10 | +Basic Operations of Stack |
| 11 | +--- |
| 12 | +There are some basic operations that allow us to perform different actions on a stack. |
| 13 | + |
| 14 | +- Push: Add an element to the top of a stack |
| 15 | +- Pop: Remove an element from the top of a stack |
| 16 | +- IsEmpty: Check if the stack is empty |
| 17 | +- IsFull: Check if the stack is full |
| 18 | +- Peek: Get the value of the top element without removing it |
| 19 | + |
| 20 | +Working of Stack |
| 21 | +--- |
| 22 | +The operations work as follows: |
| 23 | + |
| 24 | +- A pointer called TOP is used to keep track of the top element in the stack. |
| 25 | +- When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1. |
| 26 | +- On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP. |
| 27 | +- On popping an element, we return the element pointed to by TOP and reduce its value. |
| 28 | +- Before pushing, we check if the stack is already full |
| 29 | +- Before popping, we check if the stack is already empty |
| 30 | + |
| 31 | +    <img src="https://cdn.programiz.com/sites/tutorial2program/files/stack-operations.png" alt="Working of stack" width="600"/> |
| 32 | + |
| 33 | + |
| 34 | +Algorithm And Code Example |
| 35 | +--- |
| 36 | +> ### peek() |
| 37 | +Algorithm of peek() function : |
| 38 | +#### Algorithm |
| 39 | +``` |
| 40 | +begin procedure peek |
| 41 | +return stack[top] |
| 42 | +end procedure |
| 43 | +``` |
| 44 | +Implementation of peek() function in C programming language : |
| 45 | + |
| 46 | +#### Example |
| 47 | +``` |
| 48 | +int peek() { |
| 49 | + return stack[top]; |
| 50 | +} |
| 51 | +``` |
| 52 | +> ### isfull() |
| 53 | +Algorithm of isfull() function : |
| 54 | + |
| 55 | +#### Algorithm |
| 56 | +``` |
| 57 | +begin procedure isfull |
| 58 | +
|
| 59 | + if top equals to MAXSIZE |
| 60 | + return true |
| 61 | + else |
| 62 | + return false |
| 63 | + endif |
| 64 | + |
| 65 | +end procedure |
| 66 | +
|
| 67 | +``` |
| 68 | +Implementation of isfull() function in C programming language : |
| 69 | + |
| 70 | +#### Example |
| 71 | +``` |
| 72 | +bool isfull() { |
| 73 | + if(top == MAXSIZE) |
| 74 | + return true; |
| 75 | + else |
| 76 | + return false; |
| 77 | +} |
| 78 | +``` |
| 79 | +> ### isempty() |
| 80 | +Algorithm of isempty() function : |
| 81 | +#### Algorithm |
| 82 | +``` |
| 83 | +begin procedure isempty |
| 84 | +
|
| 85 | + if top less than 1 |
| 86 | + return true |
| 87 | + else |
| 88 | + return false |
| 89 | + endif |
| 90 | + |
| 91 | +end procedure |
| 92 | +``` |
| 93 | +Implementation of isempty() function in C programming language is slightly different. We initialize top at -1, as the index in array starts from 0. So we check if the top is below zero or -1 to determine if the stack is empty. Here's the code : |
| 94 | + |
| 95 | +#### Example |
| 96 | +``` |
| 97 | +bool isempty() { |
| 98 | + if(top == -1) |
| 99 | + return true; |
| 100 | + else |
| 101 | + return false; |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +> ### push() |
| 106 | +The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps : |
| 107 | +- Step 1 − Checks if the stack is full. |
| 108 | +- Step 2 − If the stack is full, produces an error and exit. |
| 109 | +- Step 3 − If the stack is not full, increments top to point next empty space. |
| 110 | +- Step 4 − Adds data element to the stack location, where top is pointing. |
| 111 | +- Step 5 − Returns success. |
| 112 | + |
| 113 | +#### Algorithm |
| 114 | +A simple algorithm for Push operation can be derived as follows : |
| 115 | +``` |
| 116 | +begin procedure push: stack, data |
| 117 | +
|
| 118 | + if stack is full |
| 119 | + return null |
| 120 | + endif |
| 121 | + |
| 122 | + top ← top + 1 |
| 123 | + stack[top] ← data |
| 124 | +
|
| 125 | +end procedure |
| 126 | +``` |
| 127 | +Implementation of this algorithm in C : |
| 128 | +#### Example |
| 129 | +``` |
| 130 | +void push(int data) { |
| 131 | + if(!isFull()) { |
| 132 | + top = top + 1; |
| 133 | + stack[top] = data; |
| 134 | + } else { |
| 135 | + printf("Could not insert data, Stack is full.\n"); |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | +> ###pop() |
| 140 | +A Pop operation may involve the following steps − |
| 141 | +- Step 1 − Checks if the stack is empty. |
| 142 | +- Step 2 − If the stack is empty, produces an error and exit. |
| 143 | +- Step 3 − If the stack is not empty, accesses the data element at which top is pointing. |
| 144 | +- Step 4 − Decreases the value of top by 1. |
| 145 | +- Step 5 − Returns success. |
| 146 | + |
| 147 | +A simple algorithm for Pop operation can be derived as follows : |
| 148 | +#### Algoritm |
| 149 | +``` |
| 150 | +begin procedure pop: stack |
| 151 | +
|
| 152 | + if stack is empty |
| 153 | + return null |
| 154 | + endif |
| 155 | + |
| 156 | + data ← stack[top] |
| 157 | + top ← top - 1 |
| 158 | + return data |
| 159 | +
|
| 160 | +end procedure |
| 161 | +``` |
| 162 | +Implementation of this algorithm in C : |
| 163 | + |
| 164 | +#### Example |
| 165 | +``` |
| 166 | +int pop(int data) { |
| 167 | +
|
| 168 | + if(!isempty()) { |
| 169 | + data = stack[top]; |
| 170 | + top = top - 1; |
| 171 | + return data; |
| 172 | + } else { |
| 173 | + printf("Could not retrieve data, Stack is empty.\n"); |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | +--- |
| 178 | +For a complete stack program in C programming language, please check the above code files. |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments