STACK Data Structure www btechsmartclass com What is
STACK Data Structure www. btechsmartclass. com
What is a STACK ? A stack is a container of elements that are inserted and removed according to the last-in first-out (LIFO) principle. A stack is a ordered list of elements of same data type A stack is a Linear list
What is a STACK ? In a stack all operation like insertion and deletion are performed at only one end called Top 0 1 2 3 4
What is a STACK ? Insertion In a stack all operation like insertion and deletion are performed at only one end called Top Deletion 4 3 2 1 0 Top
Operations on STACK ? Creation Insertion Deletion Displaying
Operations on STACK ? Creation Insertion Deletion Displaying #define SIZE 5 int stack[SIZE]; 4 3 2 1 0 stack
Operations on STACK ? Insertion operation is called as “push” Creation Insertion Deletion Displaying void push(element){ if(Stack is full) { printf(“FULL!!!”); } else { Top++; stack[Top] = element; } } 4 3 2 1 0 stack
Operations on STACK ? Creation Insertion Deletion Displaying Deletion operation is called as “pop” int pop( ){ if(Stack is Empty) { printf(“EMPTY!!!”); return Top; } else { deleted = stack[Top]; Top--; return deleted; } } 4 3 2 1 0 stack
Operations on STACK ? Creation Insertion Deletion Displaying void display( ){ if(Stack is Empty) { printf(“EMPTY!!!”); } else { for(i=Top; i>-1; i--) printf(“%dn”, stack[i]); } } 4 3 2 1 0 stack
- Slides: 9