05-Stacks - 1 Stacks 1 Abstract Data Types(ADTs circle5 An...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Stacks 1 Abstract Data Types (ADTs) circle5 An abstract data type (ADT) is an abstraction of a data structure circle5 An ADT specifies: circle5 Data stored circle5 Operations on the data circle5 Error conditions associated with operations 2 2 • The Stack ADT stores arbitrary objects. • Insertions and deletions follow the last-in • first-out (LIFO) scheme. The last item placed on the stack will be the first item removed (similar to a stack of dishes or a pile of papers/sheets) Stack of Dishes The Stack ADT 3 ADT Stack Operations circle5 Create an empty stack circle5 Destroy a stack circle5 Determine whether a stack is empty circle5 Add a new item -- push circle5 Remove the item that was added most recently -- pop circle5 Retrieve the item that was added most recently push pop Stack top 4 3 ADT Stack Operations (cont.) circle5 createStack() circle5 creates a an empty stack circle5 destroyStack() circle5 destroys a stack circle5 isEmpty():boolean circle5 determines whether a stack is empty or not circle5 push(in newItem:StackItemType) throw StackException circle5 Adds newItem to the top of a stack circle5 pop() throw StackException circle5 pop(out stackTop:StackItemType) throw StackException circle5 Removes the top of a stack (i.e., removes the item that was added most recently) circle5 getTop(out stackTop:StackItemType) throw StackException circle5 Retrieves the top of stack into stackTop 5 A Simple Stack Application -- Undo sequence in a text editor Problem: abcd ← ← fg ← barb4right abf // Reads an input line. Enter a char or a backspace ( ← ) readAndCorrect(out aStack:Stack) aStack.createStack() read newChar while (newChar is not end-of-line symbol) { if (newChar is not backspace character) aStack.push(newChar) else if (!aStack.isEmpty()) aStack.pop() read newChar } 6 4 A Simple Stack Application -- Display Backward circle5 Display the input line in reversed order by writing the contents of stack, named aStack. displayBackward(in aStack:Stack) while (!aStack.isEmpty())) { aStack.pop(newChar) write newChar } 7 Checking for Balanced Braces circle5 A stack can be used to verify whether a program contains balanced braces circle5 An example of balanced braces abc{defg{ijk}{l{mn}}op}qr circle5 An example of unbalanced braces abc{def}}{ghij{kl}m circle5 Requirements for balanced braces circle5 Each time we encounter a “}”, it matches an already encountered “{” circle5 When we reach the end of the string, we have matched each “{” 8 5 11/20/08 Checking for Balanced Braces 9 Checking for Balanced Braces -- Algorithm aStack.createStack(); balancedSoFar = true; i=0; while (balancedSoFar and i < length of aString) { ch = character at position i in aString; i++; if (ch is ‘{‘) // push an open brace aStack.push(‘{‘); else if (ch is ‘}’) // close brace if (!aStack.isEmpty()) aStack.pop(); // pop a matching open brace else // no matching open brace balancedSoFar = false; // ignore all characters other than braces } if (balancedSoFar and aStack.isEmpty()) aString has balanced braces else aString does not have balanced braces 10 6...
View Full Document

This note was uploaded on 11/12/2010 for the course CENG 223 taught by Professor Yok during the Spring '10 term at Middle East Technical University.

Page1 / 25

05-Stacks - 1 Stacks 1 Abstract Data Types(ADTs circle5 An...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online