200 likes | 211 Views
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.
E N D
308-203AIntroduction to Computing IILecture 6: Lists, Stacks, and Queues Fall Session 2000
Value a b c d Next To glue data together... Node = List = a chain of nodes
a b c d Terminology “head” “links” “tail”
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?)
A Simple Java Implementation Class List { Node head; // methods …. } Class Node { Object value; Node next; // methods …. }
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?
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)
a b c d A Variation: Doubly Linked Lists Value Next Node = Previous
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?
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)
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
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
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.
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)
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)
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.
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??
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)
A Class Hierarchy ofList-Like Objects SimpleList List Stack Queue This lives on our website under “Examples”