{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture15Stacks - Abstract Data Types ADTs An abstract data...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Abstract Data Types
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
ADTs An abstract data type defines the fundamental operation on the data but does not specify an implementation.
Image of page 2
Stacks A stack is a collection of items with “last in first out” retrieval. This is sometimes called LIFO.
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
Operations on a Stack A stack lets you insert and remove elements at only one end, traditionally called the top of the stack. Traditionally, the addition and removal operations are called push and pop
Image of page 4
Java Stack class Stack<String> s = new Stack<String> ( ); s.push(“A”); s.push(“B”); s.push(“C”); while (s.size() > 0) System.out.println (s.pop()); } //displays ???
Image of page 5

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

View Full Document Right Arrow Icon
Implementation A Stack can be implemented as an array of objects A Stack can be implemented as a linked list
Image of page 6
Linked List Implementation public void push (Object obj) { first = new Node (obj, first); }
Image of page 7

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

View Full Document Right Arrow Icon
pop( ) public Object pop ( ) { if (first == null) throw new NoSuchElementException () ; Object value = first.data; first = first.next; return value; }
Image of page 8
isEmpty() public boolean isEmpty ( ) { return first == null; }
Image of page 9

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

View Full Document Right Arrow Icon
toString() public String toString() {
Image of page 10
Image of page 11
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