Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements...
Transcript of Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements...
9/23/13
1
Linked Lists, Stacks, and Queues. Oh My!
Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday
• Exam 1 – next Wednesday • Review – next Monday
9/23/13
2
Assignment Submission • Include a README • Check your submissions • We will not grade it without a README • You can resubmit • but you will lose points
• APTs • all in 1 folder, no README
• Grade errors – report using form
Today • Linked Lists • Queues • Stacks
• There is a great description of Linked Lists in your textbook
9/23/13
3
List • ArrayList
List • ArrayList
9/23/13
4
List • ArrayList
• List – ordered collection of values
• List Interface • add(E elem) • add(int index, E elem) • contains(Obeject o) • an many more!
List • ArrayList
• List – ordered collection of values
• List Interface • add(E elem) • add(int index, E elem) • contains(Obeject o) • an many more!
9/23/13
5
List • ArrayList
• Array – a data structure in which elements may be located by index
List • ArrayList implements List • inherits all methods from List • ArrayList implements List’s methods using an array
• ArrayList<String> array = new ArrayList<String>(); • OR • List<String> array = new ArrayList<String>();
9/23/13
6
List • What are ArrayLists good for?
• Constant time O(1) • size • isEmpty • get • set • add*
• Linear time O(N) • everything else
* add is ~O(1)
C o m p s c i
• list.add(“C”); • list.add(“o”); • list.add(“m”); • list.add(“p”); • list.add(“s”); • list.add(“c”); • list.add(“i”);
List
9/23/13
7
List
C o m p s c i
• How would you • add to the end, beginning, middle? • remove an element?
Linked List • LinkedList • List – ordered collection of values
• List Interface • add(E elem) • add(int index, E elem) • contains(Obeject o) • an many more!
9/23/13
8
Linked List • LinkedList • different implementation of List from ArrayList
h e l o
myHead myTail
Linked List • LinkedList • Nodes • data • pointer to the next node
• Pointer to beginning and (sometimes) end
h e l o
myHead myTail
9/23/13
9
Linked List • insert “l” after “e”
C o m p h e l o
myHead myTail
Linked List • insert “l” after “e” • find the location
C o m p h e l o
myHead myTail
9/23/13
10
Linked List • insert “l” after “e” • find the location
C o m p h e l o
myHead myTail
Linked List • insert “l” after “e” • find the location • create a new node
C o m p h e l o
myHead myTail l
9/23/13
11
Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’
C o m p h e l o
myHead myTail l
myNext
Linked List • insert “l” after “e” • find the location • create a new node • update newNode to point to ‘l’ • update myNext to point to newNode
C o m p h e l o
myHead myTail l myNext
9/23/13
12
Linked List • Code time
1 public void addValues(List<String> list, String file) throws FileNotFoundException{
2 Scanner s = new Scanner(new FileInputStream(file)); 3 while(s.hasNext()){ 4 list.add(s.next()); 5 } 6 } 7 8 public void removeValues(List<String> list){ 9 for (int i = 0; i < list.size(); i++){ 10 list.remove(0); 11 } 12 }
Linked List • Singly linked list
• doubly linked list
C o m p h e l o
C o m p h e l o
9/23/13
13
Linked List • LinkedList vs. ArrayList
get add at end add in middle
LinkedList O(N) O(1) Search time + O(1)
ArrayList O(1) O(1) but O(N) worst case
Shift elements
Queue
9/23/13
14
Queue • First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
comp
Queue
comp sci
• First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
9/23/13
15
Queue
comp sci is
• First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
Queue
comp sci is great!
• First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
9/23/13
16
Queue
sci is great!
• First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
Queue
is great!
• First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
9/23/13
17
Queue
great!
• First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
Queue
“Comp sci is great!”
• First In First Out (FIFO)
1 Queue<String> q = new LinkedList<String>(); 2 q.add("comp ");
3 q.add("sci "); 4 q.add("is ");
5 q.add("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.remove());
9/23/13
18
Stack • Last In First Out (LIFO)
1 Stack<String> q = new Stack<String>(); 2 q.push("comp ");
3 q.push("sci "); 4 q.push("is ");
5 q.push("great!");
6 while(!q.isEmpty()) 7 System.out.print(q.pop());
comp sci is great!
Stack • Last In First Out (LIFO)
1 Stack<String> q = new Stack<String>();
2 q.push("comp ");
3 q.push("sci "); 4 q.push("is ");
5 q.push("great!"); 6 while(!q.isEmpty()) 7 System.out.print(q.pop());
comp sci is
9/23/13
19
Stack • Last In First Out (LIFO)
1 Stack<String> q = new Stack<String>();
2 q.push("comp ");
3 q.push("sci "); 4 q.push("is ");
5 q.push("great!"); 6 while(!q.isEmpty()) 7 System.out.print(q.pop());
comp sci
Stack • Last In First Out (LIFO)
1 Stack<String> q = new Stack<String>();
2 q.push("comp ");
3 q.push("sci "); 4 q.push("is ");
5 q.push("great!"); 6 while(!q.isEmpty()) 7 System.out.print(q.pop());
comp
9/23/13
20
Stack • Last In First Out (LIFO)
1 Stack<String> q = new Stack<String>();
2 q.push("comp ");
3 q.push("sci "); 4 q.push("is ");
5 q.push("great!"); 6 while(!q.isEmpty()) 7 System.out.print(q.pop());
“great! is comp sci”
Today • Linked Lists • Queues • Stacks