Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack Any Other Data Structure Array Linked List

Similar presentations


Presentation on theme: "Stack Any Other Data Structure Array Linked List"— Presentation transcript:

1 Stack Any Other Data Structure Array Linked List
Elements are stored in continuous / contiguous memory locations. Elements are stored in non-continuous / discontinuous memory locations. Is there any other option to store elements in memory? No. Any other data structure can be implemented: 1) Either as an Array, (If it stores elements in continuous memory locations) 2) Or as a Linked List. (If it stores elements in discontinuous memory locations)

2 Trains in a railway yard.
Stack Examples Stack of Plates Stack of Books Box of Tennis Balls (Closed at 1 end) Stack of Clothes. Trains in a railway yard. Goods in a cargo. Type: LIFO / FILO Stack of Rings / Discs Stack of Chairs

3 Stack Stack: Collection of, Also called, Stack can be implemented,
Homogeneous data elements where, Insertion and deletion take place, At one end only. Also called, LIFO structure. Last In First Out. FILO structure. First In Last Out. Stack can be implemented, Either as an Array, Stores the elements in continuous memory locations and, Has all advantages/disadvantages of array. Or as a Linked List. Stores the elements in discontinuous memory locations and, Has all advantages/disadvantages of linked list.

4 Implemented using an Array
Stack Implemented using an Array Insert an Element PUSH operation (Stack Overflow) 35 15 25 55 45 75 POP operation (Stack Underflow) Delete an Element 45 5 45 Delete an Element 55 4 55 3 25 Delete an Element 25 2 15 Delete an Element 15 1 75 35 Delete an Element 35 Delete an Element NULL Array A

5 Implemented using a Linked List
Stack Implemented using a Linked List PUSH 35 25 45 45 Stack_Head 25 NULL NULL 35 NULL 1st solution 2nd solution POP 45 PUSH: InsertFront PUSH: InsertEnd POP: DeleteFront POP 25 POP: DeleteEnd POP 35 POP NULL (Stack Underflow)

6 Stack Stack: Insertion operation in a Stack is called:
PUSH When PUSH is not possible, situation is called: Stack Overflow Deletion operation in a Stack is called: POP When POP is not possible, situation is called: Stack Underflow

7 Stack using Array PUSH an element 25 15 35 65 55 Array A POP 55 5 55
4 65 POP 35 3 35 POP 15 2 15 Is this logic efficient? No 1 25 Inefficient for a bigger array.

8 Stack using Array PUSH an element Item 25 15 35 65 55 75 Array A
Steps: U Top 5 55 If(Top >= 5) print “Stack overflow.” Top 4 65 Else Top 3 35 Top = Top + 1 Top 2 15 A[Top] = 25 Item EndIf Top 1 25 Top =

9 Stack using Array POP POP POP POP POP POP 55 65 35 15 25 NULL Steps:
Array A Item = Null L Top 5 55 If(Top < 1) print “Stack underflow.” Top 4 65 Else Top 3 35 Item = A[Top] // 55 65 35 15 25 Top 2 15 A[Top] = Null Top 1 25 Top = Top - 1 EndIf Top = Return(Item)

10 Stack using Array PUSH an element POP an element Steps: Steps:
Item = Null If(Top >= U) If(Top < L) print “Stack overflow.” print “Stack underflow.” Else Else Item = A[Top] Top = Top + 1 A[Top] = Null A[Top] = Item Top = Top - 1 EndIf EndIf Return(Item)

11 PUSH Tracing of PUSH and POP on Stack using Array. 3 If(Top >= U) 2 1) POP() print “Stack overflow.” 1 Else Top = Top + 1 Top = -1 A[Top] = Item Stack underflow. EndIf Item = NULL Array A POP Item = Null 3 2) PUSH(20) 3 If(Top < L) 2 1 print “Stack underflow.” 2 Top 20 Else 1 Item = A[Top] Top 20 3) PUSH(10) A[Top] = Null 4) PUSH(50) Top = -1 Top = Top - 1 5) PUSH(70) EndIf L = 0, U = 3 6) PUSH(80) Return(Item) 7) POP()

12 Stack using Array Algorithm: Input: Output: Data Structure: Push_Array
Top: Pointer to the Top of Stack. Item: Data to be pushed onto the Stack. Output: Stack with newly pushed item at Top if successful, message otherwise. Data Structure: Stack implemented using Array A[L...U] with TOP as the pointer.

13 Algorithm: Push_Array
Steps: If(Top >= U) print “Stack overflow.” Else Top = Top + 1 A[Top] = Item EndIf Stop

14 Stack using Array Algorithm: Input: Output: Data Structure: Pop_Array
Top: Pointer to the Top of Stack. Output: Item: Data removed (popped) from Top of Stack if successful, NULL and message otherwise. Data Structure: Stack implemented using Array A[L...U] with Top as the pointer.

15 Algorithm: Pop_Array Steps: Item = Null If(Top < L)
print “Stack underflow.” Else Item = A[Top] A[Top] = Null Top = Top - 1 EndIf Return(Item) Stop

16 Stack using Array Just look and tell the value of element on Top of Stack. Do not POP the element. PEEP PEEP POP PEEP 65 65 65 35 Steps: Array A Item = Null 5 If(Top < L) print “Stack underflow.” Top 4 65 Else Top 3 35 Item = A[Top] // 65 35 2 15 A[Top] = Null 1 25 Top = Top - 1 EndIf Return(Item)

17 Stack using Array Algorithm: Input: Output: Data Structure: Peep_Array
Top: Pointer to the Top of Stack. Output: Item: Data returned (not deleted) from Top of Stack if successful, message otherwise. Data Structure: Stack implemented using Array A[L...U] with Top as the pointer.

18 Algorithm: Peep_Array
Steps: Item = Null If(Top < L) print “Stack underflow.” Else Item = A[Top] EndIf Return(Item) Stop

19 Stack using Linked List
PUSH an element Top 55 65 35 Item new 35 3000 4000 Top Stack_Head Top 65 2000 NULL NULL 3000 4000 55 NULL 3000 Top 1000 2000 NULL Steps: Steps: (cntd) new->Data = 35 new = GetNode(NODE) new->Link = Top If(new == NULL) Stack_Head->Link = new print “Memory Insufficient. PUSH not possible.” Top = new EndIf Else Stop

20 Stack using Linked List
POP an element 35 Top Stack_Head Top ptr NULL 3000 4000 35 3000 65 2000 55 NULL 1000 4000 3000 2000 Steps: Item = Top->Data ptr = Top->Link Stack_Head->Link = ptr ReturnNode(Top) Top = ptr Return(Item) Stop

21 Stack using Linked List
POP POP Steps: 55 NULL Item = NULL If(Top == NULL) print “Stack Underflow.” Stack_Head Top Else ptr NULL NULL 2000 55 NULL NULL Item = Top->Data 1000 Top 2000 ptr = Top->Link NULL Stack_Head->Link = ptr ReturnNode(Top) Top = ptr EndIf Return(Item) Stop

22 Stack using Linked List
PUSH an element POP an element Trace the following steps on an empty Stack implemented using a Linked List: Steps: Steps: new = GetNode(NODE) Item = NULL If(new == NULL) If(Top == NULL) print “Memory Insufficient. PUSH not possible.” print “Stack Underflow.” Else Else PUSH(20) POP() PUSH(10) PUSH(30) Item = Top->Data new->Data = Item ptr = Top->Link new->Link = Top Stack_Head->Link = ptr Stack_Head->Link = new ReturnNode(Top) Top = new Top = ptr EndIf EndIf Convert into a program. Stop Return(Item) Stop

23 Stack using Linked List
Algorithm: Push_LL Input: Stack_Head: Pointer to the starting of linked list. Top: Pointer to the Top of Stack. Item: Data to be pushed onto the Stack. Output: Stack with newly pushed item at Top if successful, message otherwise. Data Structure: Stack implemented using Single Linked List with, Stack_Head as pointer to the starting of linked list and, Top as the pointer to the Top of Stack.

24 Algorithm: Push_LL print “Memory Insufficient. Push not possible.”
Steps: new = GetNode(NODE) If(new == NULL) print “Memory Insufficient. Push not possible.” Else new->Data = Item new->Link = Top Stack_Head->Link = new Top = new EndIf Stop

25 Stack using Linked List
Algorithm: Pop_LL Input: Stack_Head: Pointer to the starting of linked list. Top: Pointer to the Top of Stack. Output: Item: Element removed (popped) from the Top of Stack if successful, message otherwise. Data Structure: Stack implemented using Single Linked List with, Stack_Head as pointer to the starting of linked list and, Top as the pointer to the Top of Stack.

26 Algorithm: Pop_LL Steps: Item = NULL If(Top == NULL)
print “Stack Underflow.” Else Item = Top->Data ptr = Top->Link Stack_Head->Link = ptr ReturnNode(Top) Top = ptr EndIf Return(Item) Stop

27 Applications of Stack Uses of Stack: Main uses of Stack are:
Evaluation of Arithmetic Expression Example: ( A + B ) * C or ( ) * 3 Execute recursive function calls / Recursion.

28 Evaluation of Arithmetic Expression using a Stack
Applications of Stack Evaluation of Arithmetic Expression using a Stack How can it be solved by a computer? E = A + B 2 + 3 [A = 2, B = 3] Scanning the input once from Left to Right and doing appropriate processing. E = A + B - C [A = 1, B = 2, C = 3] Scanning the input once from Left to Right and doing appropriate processing. E = A + B * C 1 + 2 * 3 [A = 1, B = 2, C = 3] Requires scanning of the input multiple times from Left to Right. E = A – ( ( B + C ) * D ) 1 – ( ( ) * 4 ) [A = 1, B = 2, C = 3, D = 4] Requires scanning of the input several times from Left to Right.

29 Evaluation of Arithmetic Expression using a Stack
Applications of Stack Evaluation of Arithmetic Expression using a Stack Right Parenthesis / Bracket Symbols Operators E = A – ( ( B + C ) * D ) Evaluation depends on, Precedence / Priority: Left Parenthesis / Bracket 1) Operators in Brackets () Operands 2) ^ (Power / Exponentiation) 3) *, / 4) +, -

30 Applications of Stack Evaluation of Arithmetic Expression using a Stack: Process consist of 2 steps: 1) Convert the given arithmetic expression into a special notation/representation using a Stack. Algorithm would be required for this step. 2) Evaluate (Find the value of) that special notation (obtained from step-1) again using a Stack.

31 Evaluation of Arithmetic Expression using a Stack
Step-1: Convert expression into special notation using a Stack. Input Expression: A + B Enclose / Put the input in Parentheses (if not already enclosed) So input Expression becomes: Infix Expression ( A + B ) Step Symbol Stack Output Expression T T 1 ( ( T 2 A A ( T 3 + ( + A Postfix Expression T B 4 B ( A + A B + 5 ) -

32 Infix to Postfix Infix Expression: A * B + C
Stack should be empty at the end of the conversion. Postfix expression never contains parentheses. Infix to Postfix Infix Expression: A * B + C Infix Expression enclosed in parentheses: ( A * B + C ) Step Symbol Stack Output Expression T T 1 ( ( 2 A T A ( T 3 * ( A * T 4 B ( A B * T 5 + ( + A B * T 6 C ( + A B * C 7 ) A B * C +

33 Infix to Postfix Step Symbol Stack Output Expression Infix Expression 1 ( ( ((A + B) * (C – D)) 2 ( ( ( Already enclosed in parentheses. 3 A ( ( A 4 + ( ( + A 5 B ( ( + A B 6 ) ( A B + 7 * ( * A B + 8 ( ( * ( A B + 9 C ( * ( A B + C 10 - ( * ( - A B + C 11 D ( * ( - A B + C D 12 ) ( * A B + C D - 13 ) A B + C D - *

34 Infix to Postfix Infix Expression A + B * C ^ D Infix Expression enclosed in parentheses (A + B * C ^ D) Step Symbol Stack Output Expression 1 ( ( 2 A ( A 3 + ( + A 4 B ( + A B 5 * ( + * A B 6 C ( + * A B C 7 ^ ( + * ^ A B C 8 D ( + * ^ A B C D 9 ) A B C D ^ * +

35 Infix to Postfix Infix Expression A + B / C - D Infix Expression enclosed in parentheses (A + B / C - D) Step Symbol Stack Output Expression 1 ( ( 2 A ( A 3 + ( + A 4 B ( + A B 5 / ( + / A B 6 C ( + / A B C 7 - ( - A B C / + 8 D ( - A B C / + D 9 ) A B C / + D -

36 Infix to Postfix Convert the following arithmetic expressions from Infix to Postfix. ( A + B ) ^ C – ( D * E ) / F ( A + ( ( B ^ C ) – D ) ) * ( E – ( A / C ) ) A + ( B * C ) / D ( A + B ) * C A * B ^ C + D

37 Evaluation of Arithmetic Expression using a Stack
Step-2: Evaluate (Find the value of) special notation (Postfix) using Stack. Step-1 Infix Expression: A + B Postfix Expression: A B + Values: A = 3, B = 5 Postfix Expression: A B + 3 5 + 3 5 + Operands Operator 3 5 + Push(3) Push(5) y = Pop() // y = 5 1) 2) 3) x = Pop() // x = 3 5 result = x operator y // result = = 8 3 3 8 Push(result)

38 Evaluation of Arithmetic Expression using a Stack
Step-2: Evaluate (Find the value of) special notation (Postfix) using Stack. 3 5 + 3 5 + Push(3) Push(5) y = Pop() // y = 5 1) 2) 3) x = Pop() // x = 3 5 result = x operator y // result = = 8 3 3 8 Push(result) Step Symbol Stack Operation 1 3 3 Push(3) 2 5 3, 5 Push(5) y = Pop() // 5, x = Pop() // 3, 3 + 8 result = x operator y // 3+5, Push(result)

39 Evaluate Postfix Step-1 Infix Expression: A * B + C
Postfix Expression: A B * C + Values: A=3, B=5, C=5 Postfix Expression: A B * C + 3 5 * 5 + 3 5 * 5 + Step Symbol Stack Operation 1 3 3 Push(3) 2 5 3, 5 Push(5) y = Pop() // 5, x = Pop() // 3, 3 * 15 result = x operator y // 3*5, Push(result) 4 5 15, 5 Push(5) y = Pop() // 5, x = Pop() // 15, 5 + 20 result = x operator y // 15+5, Push(result)

40 Evaluate Postfix Step-1 Infix: ( (A+B) * (C-D) ) Postfix: A B + C D -*
* Step Symbol Stack Operation 1 1 1 Push(1) 2 2 1, 2 Push(2) y = Pop() // 2, x = Pop() // 1, 3 + 3 result = x operator y // 1+2, Push(result) 4 5 3, 5 Push(5) 5 3 3, 5, 3 Push(3) y = Pop() // 3, x = Pop() // 5, 6 - 3, 2 result = x operator y // 5-3, Push(result) y = Pop() // 2, x = Pop() // 3, 7 * 6 result = x operator y // 3*2, Push(result)

41 Evaluation of Arithmetic Expression using a Stack
Exercise: Evaluate the following Arithmetic Expression using a Stack: ( A * B ) - ( C / D ) [ A = 1, B = 3, C = 8, D = 2 ] Steps: 1) Infix to Postfix A B * C D / - 2) Evaluate Postfix -1

42 Algorithm: InfixToPostfix
Enclose the entire expression in parentheses (brackets) if not already enclosed. Add ‘(‘ at the beginning and ‘)’ at the end of the expression. Scan 1 symbol at a time from left to right and do the following things depending on the type of symbol scanned: If symbol is ‘(‘: Push it onto the stack. If symbol is ‘Operand’: Add/Append it to the Output Expression/Output String/Postfix Expression. If symbol is ‘Operator’: Pop all the operators one by one from TOP of stack which have, The same or higher precedence than the symbol and go on, Adding them to the Output String. Then push the incoming operator onto the stack. If symbol is ‘)’: Pop all the operators one by one from TOP of stack and go on adding them to the Output String until, ‘(‘ is encountered / obtained. Remove that ‘(‘ from the Stack and do not do anything with ‘)’.

43 Algorithm: InfixToPostfix
Steps: symbol = E.ReadSymbol() While(symbol != NULL) case: symbol = ‘(‘ symbol = operand symbol = operator symbol = ‘)’ case: otherwise print “Invalid input.” EndWhile Push(symbol) Output(symbol) item = Pop() While( item is operator ) AND (PREC(item) >= PREC(symbol)) Output(item) item = Pop() EndWhile Push(item) Push(symbol) item = Pop() While( item != ‘(’ ) Output(item) item = Pop() EndWhile

44 Algorithm: EvaluatePostfix
Steps: symbol = E.ReadSymbol() While(symbol != NULL) case: symbol = operand Push(symbol) case: symbol = operator y = Pop() x = Pop() result = x symbol y Push(result) case: otherwise print “Invalid input.” EndWhile value = Pop() Return(value) Stop


Download ppt "Stack Any Other Data Structure Array Linked List"

Similar presentations


Ads by Google