Embed
Email

6

Document Sample

Shared by: xiaopangnv
Categories
Tags
Stats
views:
5
posted:
12/10/2011
language:
pages:
14
1 CS2005









Week 6 Lectures



STACK & QUEUE

ABSTRACT DATA TYPES

2 CS2005









Aims

 Describe the Abstract Data Types Stack and

Queue

 Illustrate an implementation of the Stack ADT

 Describe the Java Collections Framework

3 CS2005









Learning Outcomes

 specify the Stack and QUEUE ADTs

 implement the Stack and Queue ADTs in

Java;

 write a Java program using the

java.util.Stack class

 recall the main points of Watt & Brown

chapters 5 & 6.

4 CS2005



Specification of a Stack ADT

Associated ADTs: Boolean, Element

Operations:

• create:  Stack

• push: Element  Stack  Stack

• peek: Stack  Element

• pop: Stack  Element  Stack

• isEmpty: Stack  Boolean

5 CS2005



Specification of a Stack ADT

Requirements:

1. isEmpty(create) = true

2. For every x,s isEmpty(push(x,s)) = false

3. For every x,s peek(push(x,s)) = x

4. For every x,s pop(push(x,s)) = (x,s)



Exceptions:

– peek(create) is illegal

– pop(create) is illegal

6 CS2005









Implementation of a Stack ADT

public class Stack {

private class SLLnode {

Object element; SLLnode succ;

private SLLnode(Object element,

SLLnode succ) {

this.element = element;

this.succ = succ;

}

}

private SLLnode top;

7 CS2005









Implementation (cont)

public Stack() {

top = null;

}

public boolean isEmpty() {

return (top == null);

}

public void push(Object elem) {

top = new SLLnode(elem,top);

}

8 CS2005









Implementation (cont)

public Object peek() {

if (top == null)

throw new NoSuchElementException();

return top.element;

}

public Object pop() {

if (top == null)

throw new NoSuchElementException();

Object elem = top.element;

top = top.succ;

return elem;

}

9 CS2005



Specification of a Queue ADT

Associated ADTs: Boolean, Element

Operations:

• create:  Queue

• append: Element  Queue  Queue

• front: Queue  Element

• remove: Queue  Queue

• isEmpty: Queue  Boolean

10 CS2005



Specification of a Queue ADT

Requirements:

1. isEmpty(create) = true

2. For every x,q isEmpty(append(x,q)) = false

3. front(append(x,create)) = x

4. For every x,q!=create front(append(x,q)) = front(q)

5. remove(append(x,create)) = create

6. For every x,q!=create remove(append(x,q)) =

append(x,remove(q))

Exceptions:

– front(create) is illegal

– remove(create) is illegal

11 CS2005





Linked List Implementation

public class Queue {

private class SLLnode {

Object element;

SLLnode succ;

private SLLnode(Object element, SLLnode succ) {

this.element = element;

this.succ = succ;

}

}

private SLLnode front, rear;



// The methods go here ...

}

12 CS2005









Array Implementation









Animated demo

13 CS2005







Java Collections Framework

 Classes that specify/implement ADTs. They

can be

See diagram

 Interfaces

of framework

 (Partial) specifications of ADTs

 Abstract Classes

 Some methods not implemented

 Classes

 Implement all methods for named interfaces

 Implement all unimplemented methods for any

abstract superclasses

 Optionally overide any other methods from

superclasses

14 CS2005









Further Study

• Reading

– Chapters 5 & 6 of Watt & Brown

• Exercises

– Tutorial Sheet 6



Related docs
Other docs by xiaopangnv
180617
Views: 0  |  Downloads: 0
apostar-por-crear-una-empresa
Views: 0  |  Downloads: 0
Contemplative Pedagogy Principles and Design
Views: 1  |  Downloads: 0
PreApplications
Views: 1  |  Downloads: 0
Basic or Pure Science vs. Applied Science
Views: 0  |  Downloads: 0
Algorithmic Problems Related To The Internet
Views: 0  |  Downloads: 0
E07-PC-23-03a_EFET Wish list
Views: 0  |  Downloads: 0
ATT
Views: 2  |  Downloads: 0
1793A_Example
Views: 1  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!