Lecture15Stacks

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

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

View Full Document Right Arrow Icon
Abstract Data Types
Background image of page 1

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

View Full DocumentRight Arrow Icon
ADTs An abstract data type defines the fundamental operation on the data but does not specify an implementation.
Background image of page 2
Stacks A stack is a collection of items with “last in first out” retrieval. This is sometimes called LIFO.
Background image of page 3

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

View Full DocumentRight 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
Background 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 ???
Background image of page 5

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

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

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

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

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

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

This note was uploaded on 10/13/2008 for the course CSC 216 taught by Professor Miller during the Spring '08 term at N.C. State.

Page1 / 25

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

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

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