UNIT2 1 To be able to implement Stack
UNIT-2 1. To be able to implement Stack ADT using arrays. 2. To be able to implement Stack ADT using linked list. 3. To analyze various application of stacks. 4. To implement infix to postfix conversion. 5. To be able to implement Queue ADT using arrays. 6. To be able to implement Queue ADT using linked list. 7. To understand the concept of Circular Queue. 8. To implement Circular Queue ADT. 9. To understand the concept of Double Ended Queue. 10. To be able to implement Double Ended Queue ADT using arrays. 11. To be able to implement Double Ended Queue ADT using linked list.
STACKS • Linear list. • One end is called top. • Other end is called bottom. • Additions to and removals from the top end only.
STACK ADT A stack is a Last In First Out (LIFO) data structure in which all insertions and deletions are takes place at one end, called the top. Stack maintains a variable called top, which keeps track of the top most element in the stack. Any insertions or deletions should be based upon the value of top. In the stack, the elements are removed in the reverse order of that in which they were added to the stack that is last element inserted is to be deleted first. Basic Stack Operations: push : To insert an item into stack pop : To delete an item from stack traversal : To display an items is. Full : To know whether the stack is full or not is. Empty : To know whether the stack is empty or not 3
STACK ADT Example 1: When the elements are inserted in the order as A, B, C, D then the size of the stack is 4 and the top most element in the stack is D. When deletion operation is performed on stack continuously then the order in which the elements are deleted is D, C, B, A. 4
STACKS Standard operations: � Is. Empty … return true iff stack is empty Is. Full … return true iff stack has no remaining capacity Top … return top element of stack Push … add an element to the top of the stack Pop … delete the top element of the stack � � �
STACKS Stack Create. S(max. Stack. Size) : : = #define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct { int key; /* other fields */ } element; Element stack[MAX_SSTACK_SIZE]; Int top = -1; Boolean Is. Empty(stack) : : = top<0; Boolean Is. Full(Stack) : : = top >= MAX_STACK_SIZE – 1;
STACKS ADT Stack is Object: a finite ordered list with 0 / more elements. Functions: for all stack belong Stack, item belong element, max. Stack. Size belongs positive integer. Stack Create(max. Stack. Size): : = create an empty stack whose maximum size is max. Stack. Size Boolean Is. Full(stack , max. Stack. Size): : = if (number of elements in stack == max. Stazk. Size) return TRUE else return FALSE Stack Push(stack) : : = if (Is. Full(stack)) stack. Full else insert item into top of stack and return Boolean Is. Empty(stack) : : = if (stack == Create. S(max. Stack. Size)) return TRUE else return FALSE Element Pop(stack) : : = if (Is. Empty(stack)) return else remove and return the element at the top of the stack
STACKS Use a 1 D array to represent a stack. Stack elements are stored in stack[0] through stack[top]. � �
STACKS Derive From array. List a b c 0 1 2 d e 3 4 5 6 Push(the. Element) => if full then either error or increase capacity and then add at stack[top+1] Suppose we increase capacity when full O(capacity) time when full; otherwise O(1) Pop() => if not empty, delete from stack[top] O(1) time � � �
STACKS Push a b c d e 0 1 2 3 4 top void push(element item) {/* add an item to the global stack */ if (top >= MAX_STACK_SIZE - 1) Stack. Full(); /* add at stack top */ stack[++top] = item; }
STACKS Pop a b c d e 0 1 2 3 4 top element pop() { if (top == -1) return Stack. Empty(); return stack[top--]; }
STACKS Stack. Full() void Stack. Full() { fprintf(stderr, “Stack is full, cannot add element. ”); exit(EXIT_FAILURE); }
STACKS Stack Using Dynamic Arrays Stack Create. S( ) : : = void typedef struct { int key; /* other fields */ } element; element *stack; MALLOC (stack , sizeof(*stack)); Int capacity= 1; Int top = -1; Boolean Is. Empty(Stack) : : = top<0; Boolean Is. Full(Stack) : : = top >= capacity – 1;
STACKS Stack. Full()/Dynamic Array Use a variable called capacity in place of MAX_STACK_SIZE Initialize this variable to (say) 1 When stack is full, double the capacity using REALLOC This is called array doubling
STACKS Stack. Full() void Stack. Full() { REALLOC(stack, 2*capacity*sizeof(*stack); capacity *= 2; }
STACK ADT IMPLEMENTATION Stack can be implemented in two ways: 1. using arrays 2. using linked list 16
STACK ADT USING ARRAYS STACK IMPLEMENTATION USING ARRAYS Procedure: Initialize the stack by assigning -1 to the top variable to indicate that the array based stack is empty. A top is an integer value, which contains the array index for the top of the stack. Each time data is pushed or popped, top is incremented or Decremented accordingly, to keep track of the current top of the stack. Stacks implemented as arrays are useful if a fixed amount of data is to be used. 17
STACK ADT USING ARRAYS Algorithm for inserting an element into the stack push() 1. if top>=size then print “stack is full” 2. else read item top=top+1 stack[top]=item endif 3. stop The first step of this algorithm checks for an overflow condition. Overflow occurs when we are trying to insert an item into a stack which is full. If the top value reaches to maximum size of the stack then items cannot be inserted into the stack i. e. stack is full. Otherwise top is incremented by one and item is inserted into 18 the stack.
STACK ADT USING ARRAYS Push Functon void push() { int ele; if(top==SIZE-1) { printf("n stack is full(overflow)n"); return; } else { printf("enter the element to push: "); scanf("%d", &ele); top++; stack[top]=ele; }
STACK ADT USING ARRAYS Algorithm for deleting an element from the stack pop() 1. if top = -1 then print “stack is empty” 2. else 1. item = stack[top] 2. top=top-1 3. endif 4. stop The first step of this algorithm checks for underflow condition. If the top value is -1 then stack is known as underflow or empty. Takeout the element from the location where the top is pointing and store it in the variable then decrement top by one. 20
STACK ADT USING ARRAYS POP Functon void pop() { if(top==-1) printf("n stack is empty(underflow)n"); else printf("the pop element is: %d", stack[top]); top--; }
STACK ADT USING ARRAYS Algorithm for displaying an elements of the stack traversal() 1. if top = -1 1. print “stack empty” 2. else 1. for(i = top; i >= 0; i--) 1. print “stack[i]” 3. endif 4. stop void display() { if(top==-1) { printf("n stack is empty(underflow)"); return } printf("n the elements in stack are: n"); for(i=top; i>=0; i--) printf("->%d", stack[i]); } If the top value is -1 then the stack is empty. If the stack is not empty, assign a top value to variable i, display the item which is pointed at stack[i] , decrement the top value by one and repeat this process until top becomes zero. C program example 1 , also show error simulation 22
STACK ADT USING LINKED LIST STACK IMPLEMENTATION USING LINKED LIST Implementing stacks as linked lists provides a feasibility on the number of nodes by dynamically growing stacks, as a linked list is a dynamic data structure. The stack can grow or shrink as the program demands it to. Disadvantage of using an array to implement a stack or queue is the wastage of space. A variable top always points to top element of the stack. top = NULL specifies stack is empty. 23
STACK ADT USING LINKED LIST Example: The following list consists of five cells, each of which holds a data object and a link to another cell. 24
STACK ADT USING LINKED LIST Push function void push() { struct node*temp; temp=(struct node*)malloc(sizeof(struct node)); printf("n enter data of item: "); scanf("%d", &temp->data); temp->link=top; top=temp; } POP function void pop() { struct node*temp; if(top==NULL) printf("n stack is empty n"); else { temp=top; printf("popped item is %d n", temp->data); top=top->link; free(temp); } } 25
STACK ADT USING LINKED LIST Traversal function void display() { struct node*temp; temp=top; if(top==NULL) printf("stack is empty n"); else { printf("stack elements: n"); while(temp!=NULL) { printf("->%d", temp->data); temp=temp->link; } } } C program example 2 , also show error simulation
STACK APPLICATIONS • Stacks are used in conversion of infix to postfix expression. • Stacks are also used in evaluation of postfix expression. • Stacks are used to implement recursive procedures. • Stacks are used in compiler design. 27
STACK APPLICATIONS TYPES OF EXPRESSIONS Arithmetic expressions can be represented in three ways: Infix notation: The operator is in between the two operands. Examples: A+B 2 + 3 Prefix (Polish notation): The operator precedes the operands. Examples: +AB +23 Postfix (Reverse Polish Notation): The operator follows the operands. Examples: AB+ 23+ 28
STACK APPLICATIONS CONVERSIONS Consider the infix expression: 2 + 3 * (5 – 7) / 9 Let us insert implicit parentheses (2 + ((3 * (5 – 7)) / 9)) Transfer the operators to the beginning of parentheses (+ 2 (/ (* 3 (– 5 7)) 9)) Remove the parentheses: + 2 / * 3 – 5 7 9 This is the equivalent prefix expression. Transfer the operators to the end of parentheses (2 ((3 (5 7 –) *) 9 /) +) Remove the parentheses: 2 3 5 7 – * 9 / + This is the equivalent postfix expression. 29
STACK APPLICATIONS Algorithm to convert infix expression to Postfix: 1. Initialize an empty stack. 2. Repeat the following process until the end of the infix expression. Get next input token (constant, variable, arithmetic operator, left parenthesis, right parenthesis) in the infix expression. If the token is � � A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is on the top of the stack. Pop the left parenthesis also, but do not display it. An operator: While the stack is nonempty and token has higher or equal priority than the stack’s top element, pop and display. Push token onto the stack. An operand: Display it. � � � � 3. When the infix expression is reached to the end, pop and display stack items until the stack is empty. (Note: Left parenthesis in the stack has lowest priority) 30
STACK APPLICATIONS 31
STACK ADT 1. Algorithm for postfix conversion: void postfix(char inf[15]) Step 1: repeat the steps a to c until inf[i]!='