1 / 42

Stacks

Stacks. Stack. A stack is a data structure that works on the principle of Last In First Out (LIFO) . So last item put on the stack is the first item that can be taken off , like a physical stack of books/plates.

mahon
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks

  2. Stack • A stack is a data structure that works on the principle of Last In First Out (LIFO). • So last item put on the stack is the first item that can be taken off, like a physical stack of books/plates. • In stack new elements are added to and removed from the top of the structure.

  3. Two Implementations of Stack • As Vector (Array) Storing the items contiguously . • As List Storing items noncontiguously.

  4. Operations of Stack • IsEmpty: return true if stack is empty, return false otherwise • IsFull: return true if stack is full, return false otherwise • Top: return the element at the top of stack • Push: add an element to the top of stack • Pop: delete the element at the top of stack • DisplayStack: print all the data in the stack

  5. Array Implementation • To use an array to implement a stack, you need both the array itself and an integer • The integer tells you either: • Which location is currently the top of the stack, or • How many elements are in the stack • for an empty stack, set ToS to -1

  6. Push • (1)   Increment ToS by 1. • (2)   Set Stack[ToS] = X • Pop • (1)   Set return value to Stack[ToS] • (2)   Decrement ToS by 1 • These operations are performed in very fast constant time

  7. How stack works Empty stack push(5) push(7) pop() tos=1 tos=0 tos=0 tos= -1

  8. Create Stack • Create an empty stack by setting Top of the stack to -1 createStack() { TOS=-1; }

  9. Push Stack • void Push(int x); • Push an element onto the stack • If the stack is full, print the error information. • Note top always represents the index of the top element. First increment top, then push on to stack. Void push(int x) { if(isFull()) printf(" Stack overflow"); else { TOS++; Stack[TOS]=x; } }

  10. Pop Stack • int Pop() • Pop means return the element at the top of the stack • If the stack is empty, print the error information. Don’t forgot to decrement TOS. pop() { if (isEmpty()) printf( "Stack Underflow"); else { return(Stack[TOS--]); } }

  11. Stack Top • int Top(int Stack[]) • Return the top element of the stack • Unlike Pop, this function does not remove the top element int Top(int Stack[ ]) { if (isEmpty()) { printf("\n Stack is empty"); return; } return Stack[TOS]; }

  12. Printing all the elements • void DisplayStack() • Print all the elements Void displayStack() { int i; printf("Top -->"); for(i=TOS;i>=0;i--) printf("\t \t%d \n" ,Stack[i]); }

  13. Linked List Implementation • Advantage of the linked list : using only one pointer per item at a time. • Disadvantage of contiguous vector implementation : using excess space equal to the number of vacant array items .

  14. Linked List Implementation of Stack • The stack can be implemented as a linked list in which the top of the stack is represented by the first item in the list. topOfStack

  15. Linked List Implementation of Stack • Each stack item stores • element value • pointer to next element

  16. Node Structure of the Stack typedef struct node{ int item; struct node *next; }Node; //Create Empty stack Node *Stack=NULL;

  17. Push operation void Push(int x) { Node *top; top=(Node *)malloc(sizeof(Node)); top->item=x; top->next=Stack; Stack=top; }

  18. 6 List Stack Example push(6); void Push(int x) { Node *top; top=(Node *)malloc(sizeof(Node)); top->item=x; top->next=Stack; Stack=top; } Stack top

  19. 6 1 List Stack Example push(6); push(1); Stack

  20. 6 1 7 List Stack Example push(6); push(1); push(7); stack

  21. 6 1 7 8 List Stack Example push(6); push(1); push(7); push(8); stack

  22. Pop Stack //Pop from a stack int Pop(Node *Stack) {Node *temp; int i; if (IsEmpty(Stack)) printf("Stack empty"); else { temp=Stack; i=temp->item; Stack=Stack->next; free(temp); return i; } }

  23. 6 1 7 8 List Stack Example push(6); push(1); push(7); push(8); pop(); stack

  24. 6 1 7 List Stack Example push(6); push(1); push(7); push(8); pop(); stack

  25. IsEmpty - to Check whether the stack is empty int IsEmpty(Node *Stack) { if(Stack == NULL) return 1; }

  26. Retrieve the Top element of the stack int Top(Node *S) { if (!IsEmpty(S)) return (S->item); printf("Empty stack"); return 0; }

  27. 6 1 7 8 List Stack Example push(6); push(1); push(7); push(8); Top() pop(); Top(); stack Top element retrieved is 8 Top element retrieved is 7

  28. Display the stack void displayStack(Node *Stack) { Node *temp; temp=Stack; printf("Top--->"); while(temp!=NULL) { printf("\t\t%d\n",temp->item); temp=temp->next; } }

  29. Application of Stack • Recognizing palindromes • Checking balanced expressions • Evaluating algebraic expressions is easier. • Searching networks, traversing trees (keeping a track where we are).

  30. Stack Applications • Converting Decimal to Binary: Consider the following pseudocode • Read (number) • Loop (number > 0) 1) digit = number modulo 2 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan The problem with this code is that it will print the binary number backwards. (ex: 19 becomes 11001000 instead of 00010011. ) To remedy this problem, instead of printing the digit right away, we can push it onto the stack. Then after the number is done being converted, we pop the digit out of the stack and print it.

  31. Simple Applications of the ADT Stack: Checking for Balanced Braces • A stack can be used to verify whether a program contains balanced braces • An example of balanced braces abc{defg{ijk}{l{mn}}op}qr • An example of unbalanced braces abc{def}}{ghij{kl}m abc{def}{ghij{kl}m

  32. Checking for Balanced Braces • Requirements for balanced braces • Each time you encounter a “}”, it matches an already encountered “{” • When you reach the end of the string, you have matched each “{”

  33. Checking for Balanced Braces Figure 7-3 Traces of the algorithm that checks for balanced braces

  34. Infix to Postfix Conversion • Scan the Infix string from left to right. • Initialise an empty stack. • If the scannned character is an operand, add it to the Postfix string. • If the scanned character is an operator and if the stack is emptyPush the character to stack. • If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character. • Repeat this step till all the characters are scanned. • (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not emptyadd topStack to Postfix string and Pop the stack. • Repeat this step as long as stack is not empty. • Return the Postfix string.

  35. Example : String a+b*c-d

  36. Infix to Postfix Example A + B * C - D / E InfixStack(bot->top)Postfix a) A + B * C - D / E b) + B * C - D / E A c) B * C - D / E + A d) * C - D / E + A B e) C - D / E + * A B f) - D / E + * A B C g) D / E - A B C * + h) / E - A B C * + D i) E - / A B C * + D j) - / A B C * + D E k) A B C * + D E / -

  37. Infix to Postfix Example #2 A * B - ( C + D ) + E InfixStack(bot->top)Postfix • A * B - ( C - D ) + E empty empty • * B - ( C + D ) + E empty A • B - ( C + D ) + E * A • - ( C + D ) + E * A B • - ( C + D ) + E empty A B * • ( C + D ) + E - A B * • C + D ) + E - ( A B * • + D ) + E - ( A B * C • D ) + E - ( + A B * C • ) + E - ( + A B * C D • + E - A B * C D + • + E empty A B * C D + - • E + A B * C D + - • + A B * C D + - E • empty A B * C D + - E +

  38. Transform postfix to infix: a)Push each operand in the stack until an operation is encountered. b)Pop two operands and wrap them around their operation. c) repeat until done. Transform Prefix to infix: Same as above, but we start parsing from right to left. Example: + B * C D

  39. Evaluating Postfix Expressions • A postfix (reverse Polish logic) calculator • Requires you to enter postfix expressions • Example: 2 3 4 + * • When an operand is entered, the calculator • Pushes it onto a stack • When an operator is entered, the calculator • Applies it to the top two operands of the stack • Pops the operands from the stack • Pushes the result of the operation on the stack

  40. Evaluating Postfix Expressions Figure 7-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)

  41. Postfix Expression Evaluation for each character C in a given string { if C is an operand push C onto stack; else // C is an operator { pop item from stack, and store in Opr2; pop item from stack, and store in Opr1; result = Opr1 C Opr2, using C as an operator; push result onto stack; } }

  42. Application of Stacks – Contd..

More Related