ch06 - Ch 6 - Stacks Motivation and Definition: Stacks were...

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

View Full Document Right Arrow Icon
Ch 6 - Stacks Motivation and Definition: • Stacks were invented as an abstract data type (ADT) for "last in first out" storage. • Think of a pile of dishes in your cupboard. o When we clean dishes, we put them away one at a time on top of a pile. o When we want to use a dish, we take the top dish. o Thus we use dishes in a last in first out way. • Many programming problems can be solved by using a stack to store data.
Background image of page 1

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

View Full DocumentRight Arrow Icon
The Stack ADT usually has the following operations: • create - Makes a new empty stack. • destroy - Deletes all data on the stack. • push - Stores data on top of all other data in stack. • pop - Retrieves the data item on top of the stack. • top - Retrieves the data on top, but does not remove it. (Same as pop/push) • full - Checks if stack has room for more data. • empty - Checks if stack has any data available. Stack Operations
Background image of page 2
Stack Interface Assume we want to store characters on a stack: class Stack{ public : Stack (); ~ Stack (); void push ( char item); char pop (); char top (); bool isFull (); bool isEmpty (); private : TBA }; Note : You can easily change storage to another data type by changing the char to another type.
Background image of page 3

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

View Full DocumentRight Arrow Icon
• Assume you are given an unknown number of characters from the user. • How can you check to see if they have entered something of the from anbn where n >=0 . • One solution is to push 'a's as you read them, and pop 'a's when you read a 'b'. Checking for anbn
Background image of page 4
Checking for anbn cont. Example: user enters "aabb". 1. read 'a', push 2. read 'a', push 3. read 'b', pop 4. read 'b', pop *Stack is empty so input was valid.
Background image of page 5

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

View Full DocumentRight Arrow Icon
anbn Implementation bool check_anbn ( char [] str){ Stack s; char ch; // process input for ( int i=0; i < strlen(str); i++){ if (str[i] == 'a' ) s.push( 'a' ); else if (str[i] == 'b' ) ch = s.pop(); } // check if stack is empty if (s.empty()) return true ; else return false ; } • This function has a number of errors. Can you find them?
Background image of page 6
Improved anbn Code bool check_anbn ( char [] str ){ Stack s; char ch; // process a's int i=0; while 'a' ))&&(!s.full())){ s.push( 'a' ); i++; } // process b's while
Background image of page 7

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

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

Page1 / 25

ch06 - Ch 6 - Stacks Motivation and Definition: Stacks were...

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

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