What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end called Top Visual Representation of Stack: Field Value Size of the Stack 6 Maximum Value of Stack Top 5 Minimum Value of Stack Top 0 Value of Top when Stack is Empty -1 Value of Top when Stack is Full 5 Basic Operations Performed on Stack : 1. Create 2. Push 3. Pop 4. Empty Creating Stack: 1. Stack can be created by declaring the structure with two members. 2. One Member can store the actual data in the form of array. 3. Another Member can store the position of the topmost element. typedef struct stack int data[max]; int top; stack;
Push on Stack: We have declared data array in the above declaration. Whenever we add any element in the data array then it will be called as Pushing Data on the Stack. Suppose top is a pointer to the top element in a stack. After every push operation, the value of top is incremented by one. Pop on Stack : Whenever we try to remove element from the stack then the operation is called as POP Operation on Stack. Some basic terms : Concept Stack Push Stack Overflow Definition The procedure of inserting a new element to the top of the stack is known as Push Operation Any attempt to insert a new element in already full stack is results into Stack Overflow.
Concept Stack Pop Stack Underflow Definition The procedure of removing element from the top of the stack is called Pop Operation. Any attempt to delete an element from already empty stack results into Stack Underflow. When Stack is Empty When Stack is said to empty then it does not contain any element inside it. Whenever the Stack is Empty the position of topmost element is -1. When Stack is Not Empty Whenever we add very first element then topmost position will be incremented by 1. After adding First Element top = 0. After Deletion of 1 Element Top Will be Decremented by 1 Position of Top and Its Value: Position of Top Status of Stack -1 Stack is Empty 0 First Element is Just Added into Stack N-1 Stack is said to Full N Stack is said to be Overflow Values of Stack and Top: Operation Explanation top = -1-1 indicated Empty Stack top = top + 1 After push operation value of top is incremented by integer 1 top = top 1 After pop operation value of top is decremented by 1 Check Whether Stack is Empty or Not? We are using Empty Function for Checking whether stack is empty or not 1. Function returns True if Stack is Empty. 2. Function returns False if Stack is Non-Empty. 3. Function Takes Pointer to Stack int empty (stack *s)
1. Return Type : Integer. [Empty Stack Return 1, Non Empty Stack Return 0 ] 2. Parameter : Address of Variable of Type Stack. 3. How to Call this Function From Main? typedef struct stack int data[max]; int top; stack; Empty Function : int empty(stack *s) if(s->top == -1) //Stack is Empty return(1); else return(0); stack; Check Whether Stack is Full or Not? We are using Empty Function for Checking whether stack is full or not 1. Function returns True if Stack is Full 2. Function returns False if Stack is Not Full. 3. Function Takes Pointer to Stack int full (stack *s) 1. Return Type : Integer. [If full Stack Return 1, not full Stack Return 0 ] 2. Parameter : Address of Variable of Type Stack. How to Call this Function From Main? typedef struct stack int data[max]; int top; stack; void main() stack s; // Declare Stack Variable ------- ------- i = full(&s); // Pass By Reference ------ ------ 1. Pass Stack Variable to full Function using pass by reference. 2. As full Function returns integer, we have facility to store returned value into some integer variable so we have written [ i = full(&s) ]
Complete Code int full(stack *s) if(s->top == MAX-1) //Stack is Full return(1); else return(0); Push Operation: 1. Push Refers as Adding Elements onto Stack. 2. Push Operation carried out in following 2 steps o o First Increment Variable top so that it now refers to next memory location. Secondly Add Element Onto Stack by accessing array. 3. Main Function Should ensure that stack is not full before making call to push() in order to prevent Stack Overflow Push Function void push(stack *s,int num) s->top = s->top + 1; s->data[s->top] = num; Pop Operation Arguments and Return Type : 1. Argument : Variable of Type Stack. 2. Return Type : Integer [ Removed Element ] Steps in Pop Operation : 1. Store Topmost Element in another Variable. 2. Decrement Top by 1 3. Return Topmost Element. Pre-requisites : Stack Type Definition.: Click Here Pop Function : int pop(stack *s) int x; x = s->data[s->top]; s->top = s->top - 1; return(x);
Application of Stack : 1. Parsing 2. Recursive Function 3. Calling Function 4. Expression Evaluation 5. Expression Conversion I. Infix to Postfix II. III. IV. Infix to Prefix Postfix to Infix Prefix to Infix 6. Towers of hanoi Expression Representation Techniques : 1. Infix Expression 2. Prefix Expression 3. Postfix Expression Evaluation of Postfix Expression : [ Click Here ] Expression Example Note Infix a + b Operator Between Operands Prefix + a b Operator before Operands Postfix a b + Operator after Operands Generally postfix expressions are free from Operator Precedence thats why they are preferred in Computer system.computer System Uses Postfix form to represent expression. Following is the example that shows evaluation of the Postfix expression using stack as data structure. Algorithm for Evaluation of Postfix Expression Initialize(Stack S) x = ReadToken(); // Read Token while(x) if ( x is Operand ) Push ( x ) Onto Stack S. if ( x is Operator )
Operand2 = Pop(Stack S); Operand2 = Pop(Stack S); Evaluate (Operand1,Operand2,Operator x); x = ReadNextToken(); // Read Token