Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000."— Presentation transcript:

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

2 To glue data together... ValueNext Node = List = a chain of nodes abcd

3 Terminology abcd “head” “tail” “links”

4 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?)

5 A Simple Java Implementation Class Node { Object value; Node next; // methods …. } Class List { Node head; // methods …. }

6 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?

7 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)

8 A Variation: Doubly Linked Lists abcd ValueNext Node = Previous

9 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?

10 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)

11 Stacks Definition: A stack is a list which is accessed only by the two following methods: 1. Push - insert an element at the head 2. Pop - remove the most recently “pushed” element

12 Stacks - the intuition Think of pancakes on a plate Empty a Push(a) Push(b) a b Push(c) a b c Pop( ) a b a c b

13 Why do we care? A stack has less functionality than a list (because we only pop and push) but it’s enough 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.

14 Definition: A queue is a list which is accessed only by the two following methods, which preserve a First-In First-Out or “FIFO” order: 1. Insert - insert an element at one end 2. Dequeue - remove the “oldest” element (= the one at the other end of the list) Queues (aka FIFO)

15 Queues The intuition: this is what you do when you’re waiting in line at the grocery store Customers in Cashiers service customers Queue a bcd ( = INSERT) ( = DEQUEUE)

16 Why do we care? Whenever you’ve got a lot of tasks to do, putting them in a queue and servicing them in FIFO order guarantees that no task goes forever unfinished…. True for HTTP requests on a webserver as for the client in line at the bank.

17 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??

18 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)

19 A Class Hierarchy of List-Like Objects SimpleList ListStackQueue This lives on our website under “Examples”

20 Any questions?


Download ppt "308-203A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000."

Similar presentations


Ads by Google