1 / 22

S t a c k s www. rana-softwares.weebly.com

S t a c k s www. rana-softwares.weebly.com. Stacks. A list of elements in which insertion and deletion can take place only at one end is called a stack. That end is called top of stack. Stack is also called Last In First Out (LIFO). Insertion in a stack is called PUSH.

vida
Download Presentation

S t a c k s www. rana-softwares.weebly.com

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. S t a c k swww.rana-softwares.weebly.com

  2. Stacks • A list of elements in which insertion and deletion can take place only at one end is called a stack. • That end is called top of stack. • Stack is also called Last In First Out (LIFO). • Insertion in a stack is called PUSH. • Deletion in a stack is called POP.

  3. Stacks • Implementation: • It is implemented through array and linked list. • Overflow: when the stack is full and we try to push more element. • Underflow: when the stack is empty and we try to remove an element. • to give an example.

  4. Stacks • Two procedures: • Push(item) • { • if(top == n) • Write: “Overflow”, Exit. • top = top + 1 • S[top] = item • }

  5. Stacks • Pop(item) • { • if(top == 0) • Write: “Underflow”, Exit. • item = S[top] • top = top - 1 • }

  6. Applications of Stack • Polish Notation: Arithmetic expressions can be represented in the following three notations: • Infix • Prefix (Polish notation) • Postfix (Reverse polish notation)

  7. Applications of Stack • Infix Notation: • Operator is placed between the operands. • e.g., a + b, a + b * c, etc. • Prefix notation: • Operator is placed before the operands. • e.g., a + b → + a b • c * d → * c d • 2 + 5 → + 2 5

  8. Applications of Stack • a + b * c • = a + [* b c] • = + a * b c • +++++++++++++++++++++++++ • a + b * c / d – k • = a + [* b c] / d – k • = a + [/ * b c d] – k • = [+ a / * b c d] – k • = - + a / * b c d k

  9. Applications of Stack • Postfix Notation (Reversed polish notation): Operators are placed after the operands. e.g., • a + b → a b + • c * d → c d * • a + b * d → a + [b d *] = a b d * + • +++++++++++++++++++++++++++ • a + b – c / (d + k) • = a + b – c / [d k +] • = a + b – [c d k + /] • = [a b +] - [c d k + /] • = a b + c d k + / -

  10. Applications of Stack • Some more examples: • (a - b) * (d / e) • (a + b ^ d) / (e - f) + g • a * (b + d) / e – f * ( g + h / k)

  11. Stacks • Algorithm to evaluate postfix expression • 1. Place right parenthesis on to the postfix expression. • 2. Scan the expression from left to right until right parenthesis is encountered. • a) if an operand is encountered then push the operand on the stack. • b) if an operator is encountered then pop two elements from the stack. Apply the operation and place the result back on the stack.

  12. Stacks • 3. on exit from loop of step 2, value placed in the stack will be the result. • Example: (2 * 5) + 15 / 3 – 4 ^ 2 • To convert into postfix expression: • =[2 5 *] + [15 3 /] – [4 2 ^] • = [2 5 * 15 3 / +] – [4 2 ^] • = 2 5 * 15 3 / + 4 2 ^ -)

  13. Recursion • A function / procedure that calls itself is known as recursive procedure. This operation of calling a function is called recursion. • To use recursion, following two properties must exist: • To call the function repeatedly. • There must be a base criterion to avoid infinite execution.

  14. Factorial function • n! = n * (n-1)! Repeated call to factorial • 0! = 1 this is the base case. • int fact (int n) • { • int f; • if(n == 0) • return 1; • f = n * fact (n - 1); • return f; • }

  15. Fibonacci Sequence • 0,1,1,2,3,5,8,13,21……… • A series in which every term is the sum of previous two terms is called a fibonacci sequence.

  16. Fibonacci Sequence(Recursive function) • int fib(int n) • { int a, b, s; • if(n == 1 || n == 2) • return (n-1); • a = fib(n-2); • b = fib(n-1); • s = a + b; • return s; • }

  17. Tower of Hanoi • This is also implemented using recursion. • Do it yourself. This is included in the syllabus.

  18. Queue • A list of elements in which insertion can take at one end and the deletion at the other end. • It is also called first in first out (FIFO). • Insertion end is called rear and deletion end is called front.

  19. Queue • Implementation: • Front = 0 Rear = 0 • Insert(10) • Front = 1 Rear =1 • Insert(8) • Front = 1 Rear =2 • Insert(12) • Front = 1 Rear = 3

  20. Queue • Insert(2) • Insert(3) • Insert(6) • Front =1 Rear = 6

  21. Insertion in Queue • Insertion(item) • { • if( (rear = N and front = 1) or (rear + 1 = front)) • Write: ‘Overflow’ and exit • if(rear = 0 and front = 0) • rear = 1, front = 1 • else if (rear = N) • rear = 1 • else • rear = rear + 1 • Q[rear] = item • }

  22. Deletion in Queue • if( front = 0 and rear = 0) • Write: ‘underflow’ and exit • Item = Q[front] • if( front = rear) • front = 0, rear = 0 • else if (front = N) • front = 1 • else • front = front + 1

More Related