308-203a introduction to computing ii lecture 6: lists, stacks, and queues fall session 2000
TRANSCRIPT
308-203AIntroduction 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
Terminology
a b c d
“head” “tail”“links”
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 Node {
Object value;Node next;
// methods….
}
Class List {
Node head;
// 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?
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)
A Variation: Doubly Linked Lists
a b c d
Value NextNode =
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?
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)
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
Stacks - the intuitionThink of pancakes on a plate
Empty
a
Push(a) Push(b)
ab
Push(c)
abc
Pop( ) Pop( )
ab
a
c b
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.
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)
QueuesThe intuition: this is what you do whenyou’re waiting in line at the grocery store
Customers inCashiers servicecustomers
Queue
abcd
( = INSERT) ( = DEQUEUE)
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.
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?
Q2. If it’s a doubly-linked list??
One is O(n), the other O(1)
Both O(1)
A Class Hierarchy ofList-Like Objects
SimpleList
List Stack Queue
This lives on our website under “Examples”
Any questions?