Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.

Similar presentations


Presentation on theme: "Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks."— Presentation transcript:

1 Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks

2 Ceng-112 Data Structures ITurgut Kalfaoglu 2 Figure 3-2 Linear Lists Operations are; 1.Insertion 2.Deletion 3.Retrieval 4.Traversal (exception for restristed lists).

3 Ceng-112 Data Structures ITurgut Kalfaoglu 3 Figure 4-1 Stacks A stack is a Last in First out (LIFO) data structure in which all insertions and deletions are restricted to one end called top.

4 Ceng-112 Data Structures ITurgut Kalfaoglu 4 Figure 4-2 Stack Basic Operations Overflow?

5 Ceng-112 Data Structures ITurgut Kalfaoglu 5 Figure 4-3 Stack Basic Operations Underflow?

6 Ceng-112 Data Structures ITurgut Kalfaoglu 6 Figure 4-4 Stack Basic Operations

7 Ceng-112 Data Structures ITurgut Kalfaoglu 7 Figure 4-5

8 Ceng-112 Data Structures ITurgut Kalfaoglu 8 Figure 4-6 Stack Data Structure

9 Ceng-112 Data Structures ITurgut Kalfaoglu 9 Figure 4-7 Stack Data Structure

10 Ceng-112 Data Structures ITurgut Kalfaoglu 10 Figure 4-8, Part I Stack Operations

11 Ceng-112 Data Structures ITurgut Kalfaoglu 11 Figure 4-8, Part II Stack Operations

12 Ceng-112 Data Structures ITurgut Kalfaoglu 12 Create Stack algorithm createStack Allocates memory for a stack head node from dynamic memory and returns its address to the caller. Pre Nothing Post Head node allocated or error returned Return pointer to head node or null pointer if no memory 1.if (memory available) 1.allocate (stackPtr) 2.stackPtr  count = 0 3.stackPtr  top = null 2.else 1.stackPtr = null 3.return stackPtr end createStack

13 Ceng-112 Data Structures ITurgut Kalfaoglu 13 Figure 4-9 Push Stack

14 Ceng-112 Data Structures ITurgut Kalfaoglu 14 Push Stack algorithm pushStack(val stack, val data ) Insert (push) one item into the stack. Pre stack is a pointer to the stack head structure. data contains data to be pushed into stack. Post data have been pushed in stack. 1.if (stack full) 1.success = false 2.else 1.allocate (newPtr) 2.newPtr  data= data 3.newPtr  next= stack  top 4.stack  top=newPtr 5.stack  count = stack  count+1 6.Success=true 3.return success end pushStack

15 Ceng-112 Data Structures ITurgut Kalfaoglu 15 Figure 4-10 Pop Stack

16 Ceng-112 Data Structures ITurgut Kalfaoglu 16 Pop Stack algorithm popStack(val stack, val dataOut ) Pops the item on the top of the stack and returns it to the user. Pre stack is a pointer to the stack head structure. dataOut is a reference variable to receive the data. Post data have been returned to the calling algorithm. 1.if (stack empty) 1.success = false 2.else 1.dltPtr= stack  top 2.dataOut = stack  top  data 3.stack  top = stack  top  next 4.stack  count = stack  count – 1 5.recycle(dltPtr) 6.success=true 3.return success end popStack

17 Ceng-112 Data Structures ITurgut Kalfaoglu 17 Destroy Stack algorithm destroyStack(val stack ) This algorithm releases all nodes back to the dynamic memory. Pre stack is a pointer to the stack head structure. Post stack empty and all nodes recycled 1.if (stack not empty) 1.loop 1.temp = stack  top 2.stack  top = stack  top  link 3.recycled(temp) 2.recycled (stack) 3.return null pointer end destroyStack

18 Ceng-112 Data Structures ITurgut Kalfaoglu 18 Stack Applications We can be classified stack applications into four categories: 1.Reversing data. 2.Parsing data. 3.Postponing data usage. 4.Backtracking steps.

19 Ceng-112 Data Structures ITurgut Kalfaoglu 19 Stack Applications Reversing Data 1 2 3 4 55 4 3 2 1 1 2 3 4 5 Push Pop

20 Ceng-112 Data Structures ITurgut Kalfaoglu 20 Figure 4-11 Stack Applications Parsing Data Breaks the data into independent pieces for further processing. source program Compiler machine language

21 Ceng-112 Data Structures ITurgut Kalfaoglu 21 Stack Applications Parsing Data 3.Pop ) 2.Push ( 1.Push ( 3.Pop ) 2.Pop ) 1.Push (

22 Ceng-112 Data Structures ITurgut Kalfaoglu 22 Stack Applications Posponement Arithmetic expression can be represent three different formats: 1.Prefix+ a b 2.Infixa + b 3.Postfixa b + Arithmetic precedence; multiply and divide before add and subtract!

23 Ceng-112 Data Structures ITurgut Kalfaoglu 23 Stack Applications Posponement A + B * C Place the paranthesis ( A + ( B * C)) Replace it in postfix format (A(BC*)+) Remove all paranthesis ABC*+ Implementation by computer is too hard!

24 Ceng-112 Data Structures ITurgut Kalfaoglu 24 Figure 4-12, Part I Stack Applications Posponement * and / operators have higher priority than the operator at the top of the stack.

25 Ceng-112 Data Structures ITurgut Kalfaoglu 25 Figure 4-12, Part II Stack Applications Posponement

26 Ceng-112 Data Structures ITurgut Kalfaoglu 26 Figure 4-13 Stack Applications Evaluation of Postfix Expression

27 Ceng-112 Data Structures ITurgut Kalfaoglu 27 Excercise Change the following infix expression to postfix expression using the algoritmic method (a stack). a+b*c-d Solution: abc*+d-

28 Ceng-112 Data Structures ITurgut Kalfaoglu 28 Infix String : a+b*c-d The first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack. Next character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack. The next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack. Next character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be : End result : * Infix String : a+b*c-d * Postfix String : abc*+d-

29 Ceng-112 Data Structures ITurgut Kalfaoglu 29 Figure 4-14 Stack Applications BackTracking

30 Ceng-112 Data Structures ITurgut Kalfaoglu 30 Figure 4-15

31 Ceng-112 Data Structures ITurgut Kalfaoglu 31 Figure 4-16 Stack Applications BackTracking

32 Ceng-112 Data Structures ITurgut Kalfaoglu 32 Figure 4-17

33 Ceng-112 Data Structures ITurgut Kalfaoglu 33 Figure 4-20 Array Implementation of Stacks stack stackAry count stackMax top end stack

34 Ceng-112 Data Structures ITurgut Kalfaoglu 34 Hw-5 Write a program that accepts parentheses and brackets characters, one per line on standard input. Use a stack to determine whether pairs of characters are matching or not. Therefore complete the body of below function. bool balanced(const char p[ ], size_t n) // Precondition: p[0]...p[n-1] contains n characters, each of which // is '(', ')', '{' or '}'. // Postcondition: The function returns true if the characters form a // sequence of correctly balanced parentheses with each '(' matching // a ')' and each '{' matching a '}'. Note that a sequence such as // ( { ) } is NOT balanced because when we draw lines to match the // parentheses to their partners, the lines cross each other. On the // other hand, ( { } ) amd { ( ) } are both balanced. Load your HW-5 to FTP site until 13 Apr. 07 at 09:00 am.

35 Ceng-112 Data Structures ITurgut Kalfaoglu 35 Figure 4-21 Exercises Projects – 23 page 211, 212

36 Ceng-112 Data Structures ITurgut Kalfaoglu 36 Figure 4-22 Exercises Projects – 24 page 212


Download ppt "Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks."

Similar presentations


Ads by Google