{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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.
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?
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern