1 / 20

Introduction to Computing II Lecture 6: Lists, Stacks, and Queues

Learn about lists, stacks, and queues in this lecture. Understand the operations, implementation, and orders of growth for each data structure. Also, explore the concepts of doubly linked lists and the importance of stacks and queues in various applications.

cordovaj
Download Presentation

Introduction to Computing II Lecture 6: Lists, Stacks, and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 308-203AIntroduction to Computing IILecture 6: Lists, Stacks, and Queues Fall Session 2000

  2. Value a b c d Next To glue data together... Node = List = a chain of nodes

  3. a b c d Terminology “head” “links” “tail”

  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 List { Node head; // methods …. } Class Node { Object value; Node next; // 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? A1. O(1) Q2. What is the complexity of adding an item at the tail of a list? A2. O(n) Q3. In general? A3. O(n)

  8. a b c d A Variation: Doubly Linked Lists Value Next 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? A1. O(1) Q2. What is the complexity of adding an item at the tail of a doubly-linked list? A2. O(1) Q3. In general? 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 c b b b a a a a a Push(a) Empty Push(b) Push(c) Pop( ) Pop( ) 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. Queues(aka FIFO) 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)

  15. Queues The intuition: this is what you do when you’re waiting in line at the grocery store Queue a d c b Cashiers service customers Customers in ( = 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? One is O(n), the other O(1) Q2. If it’s a doubly-linked list?? Both O(1)

  19. A Class Hierarchy ofList-Like Objects SimpleList List Stack Queue This lives on our website under “Examples”

  20. Any questions?

More Related