308-203a introduction to computing ii lecture 6: lists, stacks, and queues fall session 2000

20
308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Upload: arline-green

Post on 03-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

308-203AIntroduction to Computing II

Lecture 6: Lists, Stacks, and Queues

Fall Session 2000

Page 2: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

To glue data together...

Value NextNode =

List = a chain of nodes

a b c d

Page 3: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Terminology

a b c d

“head” “tail”“links”

Page 4: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Possible Operations on Lists

• Locate an element

• Check if empty

• Check the number of items in the list

• Concatenate lists together

• Insert an element (where?)

• Delete an element (how is it referred to?)

Page 5: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

A Simple Java Implementation

Class Node {

Object value;Node next;

// methods….

}

Class List {

Node head;

// methods….

}

Page 6: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Orders of Growth

Q1. What is the complexity of adding an item at the head of a list?

Q2. What is the complexity of adding an item at the tail of a list?

Q3. In general?

Page 7: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Orders of Growth

Q1. What is the complexity of adding an item at the head of a list?

Q2. What is the complexity of adding an item at the tail of a list?

Q3. In general?

A1. O(1)

A2. O(n)

A3. O(n)

Page 8: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

A Variation: Doubly Linked Lists

a b c d

Value NextNode =

Previous

Page 9: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Orders of Growth

Q1. What is the complexity of adding an item at the head of a doubly-linked list?

Q2. What is the complexity of adding an item at the tail of a doubly-linked list?

Q3. In general?

Page 10: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Orders of Growth

Q1. What is the complexity of adding an item at the head of a doubly-linked list?

Q2. What is the complexity of adding an item at the tail of a doubly-linked list?

Q3. In general?

A1. O(1)

A2. O(1)

A3. O(n)

Page 11: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Stacks

Definition: A stack is a list which is accessedonly by the two following methods:

1. Push - insert an element at the head2. Pop - remove the most recently “pushed”

element

Page 12: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Stacks - the intuitionThink of pancakes on a plate

Empty

a

Push(a) Push(b)

ab

Push(c)

abc

Pop( ) Pop( )

ab

a

c b

Page 13: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Why do we care?A stack has less functionality than a list(because we only pop and push) but it’senough to do some important things:

1. Reverse Polish Notation calculators

2. This is really what happens when you call a function or method

3. CS Profs like to ask questions about stacks on exams.

Page 14: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Definition: A queue is a list which is accessedonly by the two following methods, which preservea First-In First-Out or “FIFO” order:

1. Insert - insert an element at one end2. Dequeue - remove the “oldest” element

(= the one at the other end of the list)

Queues(aka FIFO)

Page 15: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

QueuesThe intuition: this is what you do whenyou’re waiting in line at the grocery store

Customers inCashiers servicecustomers

Queue

abcd

( = INSERT) ( = DEQUEUE)

Page 16: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Why do we care?

Whenever you’ve got a lot of tasksto do, putting them in a queue andservicing them in FIFO order guaranteesthat no task goes forever unfinished….

True for HTTP requests on a webserveras for the client in line at the bank.

Page 17: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Orders of growth??

Q1. What is the complexity of the two queue operations if there is an underlying (singly-linked) list?

Q2. If it’s a doubly-linked list??

Page 18: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Orders of growth??

Q1. What is the complexity of the two queue operations if there is an underlying (singly-linked) list?

Q2. If it’s a doubly-linked list??

One is O(n), the other O(1)

Both O(1)

Page 19: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

A Class Hierarchy ofList-Like Objects

SimpleList

List Stack Queue

This lives on our website under “Examples”

Page 20: 308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000

Any questions?