220 likes | 392 Views
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.
E N D
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.
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.
Stacks • Two procedures: • Push(item) • { • if(top == n) • Write: “Overflow”, Exit. • top = top + 1 • S[top] = item • }
Stacks • Pop(item) • { • if(top == 0) • Write: “Underflow”, Exit. • item = S[top] • top = top - 1 • }
Applications of Stack • Polish Notation: Arithmetic expressions can be represented in the following three notations: • Infix • Prefix (Polish notation) • Postfix (Reverse polish notation)
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
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
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 + / -
Applications of Stack • Some more examples: • (a - b) * (d / e) • (a + b ^ d) / (e - f) + g • a * (b + d) / e – f * ( g + h / k)
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.
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 ^ -)
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.
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; • }
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.
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; • }
Tower of Hanoi • This is also implemented using recursion. • Do it yourself. This is included in the syllabus.
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.
Queue • Implementation: • Front = 0 Rear = 0 • Insert(10) • Front = 1 Rear =1 • Insert(8) • Front = 1 Rear =2 • Insert(12) • Front = 1 Rear = 3
Queue • Insert(2) • Insert(3) • Insert(6) • Front =1 Rear = 6
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 • }
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