Summary

This document is an educational resource on stacks, data structures, detailing operations, implementations, and applications, such as reversing strings, and infix-to-postfix conversion.

Full Transcript

Stacks CS223: Data Structures Stack Stack: A list with the restriction that insertions are done at one end and deletions are done at the same end. Last-In, First-Out (“LIFO”): Items are removed from a stack in the reverse order from the way th...

Stacks CS223: Data Structures Stack Stack: A list with the restriction that insertions are done at one end and deletions are done at the same end. Last-In, First-Out (“LIFO”): Items are removed from a stack in the reverse order from the way they were inserted Addition and deletion of elements occur only at one end, called the top of the stack. Basic stack operations: add (push): Add an element to the top of the stack. remove (pop): Remove an element from the top of the stack. top (peek): retrieve the top element of the stack Stacks (cont’d.) Operations on a Stack Operation Description push/add Adds an element to the top of the stack pop/remove Removes an element from the top of the stack peek Examines the element at the top of the stack isEmpty Determines whether the stack is empty size Determines the number of elements in the stack Stack Implementation 1. Using Array 2. Using Linked List Pushing and Popping 0 1 2 3 4 5 6 7 8 9 stack: 17 23 97 44 top = 3 When a stack is empty, set top = -1 To add (push) an element, increment top and store the element in stack[top] To remove (pop) an element, get the element from stack[top] and decrement top Stack Implementation using Array //MAXSIZE is array size const int MAXSIZE = 10; int stack [MAXSIZE]; //Initialize top to -1 int top = -1; Array Implementation of a Stack A Stack with 4 elements 0 1 2 3 4 5 6 7 stack … 3 top After pushing an element 0 1 2 3 4 5 6 7 stack … 4 top After popping one element 0 1 2 3 4 5 6 7 stack … 3 top After popping another element 0 1 2 3 4 5 6 7 stack … 2 top Stack Implementation using Array: Push Operation void push(int data) { if(top != MAXSIZE -1)) { top = top + 1; stack[top] = data; } } Stack Implementation using Array: Pop Operation int pop() { int data = stack[top]; top = top – 1; return data; } Stack Implementation 1.Using Array 2. Using Linked List Stack Implementation using a Linked List struct Node { int data; From a linked list implementation struct Node *next; }; New pointer for Stack Node *top=NULL; implementation Stack Implementation using Linked-list Since all the actions happen at the top of a stack, a singly- linked list is a fine way to implement it The header of the list points to the top of the stack top: 44 97 23 17 Pushing is the action of inserting an element at the beginning of the list Popping is the action of removing an element from the beginning of the list Stack Implementation using Linked List: Push Operation void push(int data) { Node* newNode = new Node; newNode->data = data; newNode->next = NULL; if(top == NULL) { top = newNode; } else { newNode ->next = top; top = newNode; } } Stack Implementation using Linked List: Pop Operation int pop() { Node * temp = top; int x = top->data;// or int x = temp -> data; top = top->next; delete temp; return x; } Motivation - Stacks Applications Line editing Reverse a string Bracket matching Postfix calculation Function call stack Browsers: to keep track of pages visited in a browser tab Reversing Strings: A simple application of stack is reversing strings. To reverse a string, the characters of the string are pushed into a stack one by one while reading the string left to right. Once all the characters of a string are pushed into a stack, they are popped one by one. Since the character last pushed in comes out first, subsequent pop operation results in the reversal of the string. Reversing Strings: Example String is "a b c d e f" PUSH to STACK Reversing Strings: Example (Cont.) Reversed String: "f e d c b a“ POP from STACK Infix to Postfix Conversion What we push into stack? Read all the symbols one by one from left to right in the given infix expression. If the reading symbol is operand, then directly print it to the result (output). If the reading symbol is a left parenthesis '(', then Push it into the Stack. If the reading symbol is a right parenthesis ')', then Pop all the contents of stack until the respective left parenthesis is popped and print each popped symbol (except the left parenthesis) to the result. If the reading symbol is an operator (e.g., + , - , * , /), then Push it into the Stack. However, first Pop the operator in the top of the stack if it has a higher or equal precedence than the currently read operator and print the popped operator to the result. Infix to Postfix Conversion Example 1 (A+ (B*C-(D/E^F)*G)*H) Resultant Postfix Expression: ABC*DEF^/G*-H*+ Infix to Postfix Conversion Example 2 (A+B)*(C-D) Infix to Postfix Conversion Example 2 (A+B)*(C-D) Infix to Postfix Conversion Example 2 (A+B)*(C-D) Infix to Postfix Conversion Example 2 (A+B)*(C-D) Infix to Postfix Conversion Example 2 (A+B)*(C-D) Result Postfix Expression: A B + C D - * Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 We see the first number 10, output it 10 Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 We see the first operator +, push it into the stack 10 + Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 We see the number 2, output it 10 2 + Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 We see the operator *, since the top operator in the stack, +, has lower priority then *, push(*) * 10 2 + Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 We see the number 8, output it * 10 2 8 + Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 We see the operator -, because its priority is lower then *, we pop. Also, because + is on the left of it, we pop +, too. Then we push(-) 10 2 8 * + - Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 We see the number 3, output it 10 2 8 * + 3 - Infix to Postfix Conversion Example 3 Ex: 10 + 2 * 8 - 3 Because the expression is ended, we pop all the operators in the stack 10 2 8 * + 3 - Evaluating Postfix Expressions 1. Create a stack to store operands (or values). 2. Scan the given expression and do following for every scanned element. If the element is a number, push it into the stack If the element is an operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack 3. When the expression is ended, the number in the stack is the final answer Evaluate a Postfix Expression Example 1 Evaluation of 7 4 -3 * 1 5 + / * top At the end of evaluation, the result top 5 top is the only item on the stack -3 top 1 6 top 4 -12 -12 -12 -2 top 7 7 7 7 7 -14 4 * -3 1+5 -12 / 6 7 * -2 Evaluate a Postfix Expression Example 2 Evaluate a Postfix Expression Example 2 Evaluate a Postfix Expression Example 2 Evaluate a Postfix Expression Example 2 Evaluate a Postfix Expression Example 2

Use Quizgecko on...
Browser
Browser