Stack can be implemented in two ways :
Static implementation uses arrays to create stack. Static implementation though a very simple technique but is not a flexible way of creation, as the size of stack has to be declared during program design, after that the size cannot be varied.
Dynamic implementation is also called linked list representation and uses pointers to implement the stack type of data structure.
A stack supports two basic operations:
Push Operation : The push operation is used to insert an element into the stack. The new element is added at the topmost position of the stack. However, before inserting the value, we must first check if TOP=MAX–1, because if that is the case, then the stack is full and no more insertions can be done. If an attempt is made to insert a value in a stack that is already full, an OVERFLOW message is printed.
Pop Operation : The pop operation is used to delete the topmost element from the stack. However, before deleting the value, we must first check if TOP=NULL because if that is the case, then it means the stack is empty and no more deletions can be done. If an attempt is made to delete a value from a stack that is already empty, an UNDERFLOW message is printed.
Example: Write a program to perform Push and Pop operations on a stack.
#include<stdio.h> #include<stdlib.h> #include<conio.h> #define MAX 3 // Altering this value changes size of stack created int st[MAX], top=-1; void push(int st[], int val); int pop(int st[]); int peek(int st[]); void display(int st[]); int main(int argc, char *argv[]) { int val, option; do { printf(" *****MAIN MENU*****"); printf(" 1. PUSH"); printf(" 2. POP"); printf(" 3. DISPLAY"); printf(" 4. EXIT"); printf(" Enter your option: "); scanf("%d", &option); switch(option) { case 1: printf(" Enter the number to be pushed on stack: "); scanf("%d", &val); push(st, val); break; case 2: val = pop(st); if(val != -1) printf(" The value deleted from stack is: %d", val); break; case 3: display(st); break; } } while(option != 4); return 0; } void push(int st[], int val) { if(top == MAX-1) { printf(" STACK OVERFLOW"); } else { top++; st[top] = val; } } int pop(int st[]) { int val; if(top == -1) { printf(" STACK UNDERFLOW"); return -1; } else { val = st[top]; top--; return val; } } void display(int st[]) { int i; if(top == -1) printf("STACK IS EMPTY"); else { for(i=top;i>=0;i--) printf("%d",st[i]); printf(""); // Added for formatting purposes } }
*****MAIN MENU***** 1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 1 Enter the number to be pushed on stack : 500= 0
In this section we will discuss typical problems where stacks can be easily applied for a simple and efficient solution. The topics that will be discussed in this section include the following:
While implementing a stack using an array, we had seen that the size of the array must be known in advance. If the stack is allocated less space, then frequent OVERFLOW conditions will be encountered. To deal with this problem, the code will have to be modified to reallocate more space for the array. In case we allocate a large amount of space for the stack, it may result in sheer wastage of memory. Thus, there lies a trade-off between the frequency of overflows and the space allocated. So, a better solution to deal with this problem is to have multiple stacks or to have more than one stack in the same array of sufficient size.