lecture18-july24 - Lecture 18 Announcements Assignment 3...

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

View Full Document Right Arrow Icon
1 Lecture 18 Announcements • Assignment 3 – due Sunday • Read rest of Ch. 4, then Ch. 5-6 • Topics of the day – sequential collections – List ADT – Stacks – Stack applications – LinkedLists – Queues The Collection Hierarchy AbstractQueue PriorityQueue + Concrete Classes AbstractQueue AbstractSet EnumSet AbstractCollection AbstractList Implements Collection Abstract Sequential List ArrayList LinkedList HashSet TreeSet PriorityQueue Linked HashSet Vector Stack +List +Queue +Set Stacks Introduction to Stacks • Consider a card game with a discard pile – Discards always placed on the top of the pile – Players may retrieve a card only from the top • We seek a way to represent and manipulate this in a computer program • This is a stack What other examples can you think of that are modeled by a stack? Introduction to Stacks • A stack is a last-in-first-out (LIFO) data structure • Adding an item – Referred to as pushing it onto the stack • Removing an item – Referred to as popping it from the top of the stack
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Stack ADT Sketch • Definition: – An ordered collection of data items – Can be accessed at only one end (the top) • Operations: – construct a stack (empty) – check if it is empty – Push: add an element onto the top – Pop: remove the top element – Peek: retrieve the top element without removal Pushing/Popping a Stack Because a pop() removes the last item that was pushed onto the stack, it uncovers the previous item pushed, or an empty stack push(A) push(B) push(C) pop ( ) pop( ) push(D) Stack Class Operations Stack extends Vector but restricts the inherited methods class Stack <T> • Constructor Stack( ); //Creates an empty stack Example stack declarations Stack <Integer> intstk = new Stack <Integer> ( ); Stack <Character> digits = new Stack <Character> ( ) ; Stack <Boolean> answers = new Stack <Boolean> ( ) ; Stack <Card> deck = new Stack <Card> ( ); Stack <Student> pile = new Stack <Student> ( ); Stack Operations T push (T item); • Insert and also return the argument item onto the top of the stack. • Postcondition: The stack has a new item at the top. T pop ( ); • Remove and return the item from the top of the stack. • Precondition:The stack is not empty; if so then you get an EmptyStackException thrown • Postcondition: Either the stack is empty or the stack has a new topmost item from a previous push. Stack Operations T peek ( ); • Returns the element at the top of the stack without removing it. • Precondition: The stack is not empty. boolean empty( ); • Check whether the stack is empty. Return true if it is empty and false otherwise. int search (Object element ); • Searches for the element in the stack • Returns offset from the top if found or -1 otherwise.
Background image of page 2
3 Example: Base Conversion Consider a program to do base conversion of a number (e.g. from base ten to two) using the successive modulo division algorithm •What if we had a Stack to help accomplish this? 26
Background image of page 3

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

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

This note was uploaded on 09/15/2009 for the course EE 322C taught by Professor Nettles during the Summer '08 term at University of Texas at Austin.

Page1 / 9

lecture18-july24 - Lecture 18 Announcements Assignment 3...

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

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