06c_stackqueue

06c_stackqueue - Data Structures - CSCI 102 CS102 C+ Stacks...

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

View Full Document Right Arrow Icon
1 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 Bill Cheng http://merlot.usc.edu/cs102-s11
Background image of page 1

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

View Full DocumentRight Arrow Icon
Stacks/Queues (Ch 18) Topics to cover 2 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Abstract Data Types Vectors (Ch 22) Linked Lists (Ch 17) Templates (Ch 14)
Background image of page 2
Templated lists are good for storing generic sequences of items, but they can be specialized to form other useful structures 3 Data Structures - CSCI 102 Copyright © William C. Cheng Stacks/Queues Stack: only ever insert/remove from the front of the list What if we had a List, but we restricted how insertion and removal were done? Queue: only ever insert at the back and remove from the front of the list
Background image of page 3

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

View Full DocumentRight Arrow Icon
Examples A spring-loaded plate dispenser at a buffet Stack: a list of items where insertion and removal only occurs at one end of the list 4 Data Structures - CSCI 102 Copyright © William C. Cheng Stacks A stack of boxes where you have to move the top one to get to ones farther down A PEZ dispenser Items at the top of the stack are the newest Stacks are LIFO (Last In, First Out) Items at the bottom of the stack are the oldest
Background image of page 4
A list of items What member data does a Stack need? 5 Data Structures - CSCI 102 Copyright © William C. Cheng The Stack ADT push(item) - Add an item to the top of the Stack What member functions does a Stack have? pop() - Remove the top item from the Stack A length A maximum size (optional) top() - Get a reference to the top item on the Stack (don’t remove it though!) size() - Get the number of items in the Stack empty() - Check if the Stack is empty
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Data Structures - CSCI 102 Copyright © William C. Cheng The Stack ADT Push Pop
Background image of page 6
template < typename T> class Stack { public : Stack(); ~Stack(); int size() const ; void push( const T& value); void pop(); T& top(); bool empty() const ; }; What does the interface for a Stack look like? 7 Data Structures - CSCI 102 Copyright © William C. Cheng Stack Declaration
Background image of page 7

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

View Full DocumentRight Arrow Icon
< typename T> class Stack { private : Node<T>* head; int length; public : Stack(); ~Stack(); int size() const ; void push( const T& value); void pop(); T& top(); bool empty() const ; }; How would you build a Linked List-based Stack? 8
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

06c_stackqueue - Data Structures - CSCI 102 CS102 C+ Stacks...

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

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