Docsity

Stacks uses in easy words, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Stacks uses in easy words and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

LECTURE 07 – STACKS

Stack

  • A stack is a linear data structure which can be accessed only at one of its ends (Top) for

storing and retrieving data

  • Items are added to and removed from the top of the stack (the most recently added

items are at the top of the stack)

  • The last item to be added is the first to be removed (LIFO: Last In, First Out)

Stack

  • New items are put on top of the stack
    • In which case the top of the stack moves upward to correspond to the new highest element
  • Items which are at the top of the stack are removed
    • In which case the top of the stack moves downward to correspond to the new highest element
  • Which end is top???
    • We must decide which end of the stack is designated as its top

Stack - Terminologies

  • Data items are “pushed” and “popped” (stored and retrieved) from the top of the stack.
  • Stacks normally have a maximum size. It is an error to push items onto a full stack, or

pop items off an empty stack.

  • Applications:
    • Parsing of algebraic expressions
    • Banking Transaction View – You view the last transaction first

Insertion and Deletion in Stack

  • C is the current top element of the stack. If any new items are added to the stack, they

are placed on top of C.

  • If any items are to be deleted, C is deleted first.

Insertion and Deletion in Stack

Array Stack

  • How do we know that which element to pop when user asks a pop operation. As not all

the spaces of array are filled.

  • We need another variable (usually int) to keep track of last pushed index.
  • So for each push we add one to top and for each pop we deduct one from top

Array Stack

Stack - Terminologies int top = - 1; int stack[10]; void main() { int ch; int val; do { cout << endl << "1: push"; cout << endl << "2: pop"; cout << endl << "3: exit"; cout << endl << "enter choice"; cin >> ch; switch (ch) { case 1: cin>>val; push(val); listStack(); break ; case 2: cout << "data poped= " << pop(); listStack(); break ; case 3: exit(0); } } while (1); }

Push() void push( int data) { if (top+1 == length) { cout << "stack overflow\n Cannot enter new data"; return ; } top++; stack[top] = data; }

listStack() void listStack() { cout << endl << "The stack is" << endl ; for ( int i=top; i>=0; i--) cout << stack[i] << endl; }

Applications of Stack

  • Stack is one of the most important data structures in computer science
    • Compilers use stacks to evaluate expressions
    • Stacks are great for reversing things, matching up related pairs of things, etc.
    • Stack programming problems:
      • Examine an algebraic expression to see if its braces { } and other operators match
      • Convert infix expressions to postfix or prefix
      • Reverse letters in a string, reverse words in a line, or reverse a list of numbers

Expressions

  • (A+B) * C Valid
  • A + B) Invalid
  • (A+B] Invalid

Rules

  • Each left parentheses is the opening scope
  • Each right parenthesis is a closing scope
  • Parentheses count = 0 at the end means that no scopes have been left open. This

means left and right parentheses exactly match

  • The parentheses count at any time should never become negative. If negative, then it

means expression is invalid