linked lists, stacks, and queues. oh my!...1 linked lists, stacks, and queues. oh my! announcements...

20
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

Upload: others

Post on 25-Dec-2019

29 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 2: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 3: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

9/23/13  

3  

List •  ArrayList

List •  ArrayList

Page 4: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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!

Page 5: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 6: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 7: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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!

Page 8: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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  

Page 9: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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  

Page 10: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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  

Page 11: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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  

Page 12: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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  

Page 13: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 14: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 15: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 16: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 17: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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

Page 18: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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  

Page 19: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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  

Page 20: Linked Lists, Stacks, and Queues. Oh My!...1 Linked Lists, Stacks, and Queues. Oh My! Announcements • Jotto – Due tomorrow • Apt Set 3 – Due Thursday • Exam 1 – next Wednesday

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