1. last lecture stacks and queues implemented with a linked list doubly linked lists 2 today
TRANSCRIPT
![Page 1: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/1.jpg)
1
![Page 2: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/2.jpg)
2
Last Lecture• Stacks and Queues implemented with a Linked List
• Doubly Linked Lists
Today
![Page 3: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/3.jpg)
3
Java collections librarypublic interface java.util.List<E>
boolean add(int index, E element) Inserts the specified element at the specified
position in this list (optional operation).
E remove(int index) Removes the element at the specified position in
this list
…
Implementations:• LinkedList doubly-linked list implementation• ArrayList resizable-array implementation
![Page 4: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/4.jpg)
4
Example clientpublic static void main(String[] args){ StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s);} }
![Page 5: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/5.jpg)
5
Example client
Input: “judge me by my size do > you > < < < < < ? >”
public static void main(String[] args){ StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s);} }
![Page 6: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/6.jpg)
6
Example client
Can we implement StackQueue efficiently using a linked list?
public static void main(String[] args){ StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s);} }
![Page 7: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/7.jpg)
7
Example client
Can we implement StackQueue efficiently using a linked list?• enqueue and dequeue can run in Θ(1)• but pop will need to run in Θ(n)
we can do better
public static void main(String[] args){ StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s);} }
![Page 8: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/8.jpg)
8
Doubly Linked Lists(Java implementation in Assignment 2)
https://www.scss.tcd.ie/Vasileios.Koutavas/teaching/cs2010/michaelmas1516/assignment-2/
![Page 9: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/9.jpg)
9
Doubly Linked Listshead
tail
to be or not to
null
class DLLNode { String item; DLLNode next; DLLNode prev;}
![Page 10: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/10.jpg)
10
Doubly Linked Listshead
tail
to be or not to
null
class DLLNode { String item; DLLNode next; DLLNode prev;}
null to to not be
null null
null null
Empty DLL
The only node of a DLL with 1 node
The first node of a DLL with >1 node
The last node of a DLL with >1 node
A middle node of a DLL with >2 items
States of nodes:
![Page 11: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/11.jpg)
11
Doubly Linked Listsclass DLLofString
DoublyLinkedList()
void insertFirst(String s) inserts s at the head of the listString getFirst() returns string at the head of the listboolean deleteFirst() removes string at the head of the listvoid insertLast(String s) inserts s at the end of the listString getLast(String s) returns string at the end of the listboolean deleteLast() removes string at the end of the list
void insertBefore(int pos, String s) inserts s before position posString get(int pos) returns string at position posboolean deleteAt(int pos) deletes string at position pos
• Interface somewhat different than that of java.util.List.• How to make interface generic?
• class DoublyLinkedList<T>
![Page 12: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/12.jpg)
12
Doubly Linked Listsclass DLLofString
DoublyLinkedList()
void insertFirst(String s) inserts s at the head of the listString getFirst() returns string at the head of the listboolean deleteFirst() removes string at the head of the listvoid insertLast(String s) inserts s at the end of the listString getLast(String s) returns string at the end of the listboolean deleteLast() removes string at the end of the list
void insertBefore(int pos, String s) inserts s before position posString get(int pos) returns string at position posboolean deleteAt(int pos) deletes string at position pos
• Interface somewhat different than that of java.util.List.• How to make interface generic?
• class DoublyLinkedList<T>
![Page 13: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/13.jpg)
13
Three main cases to deal withhead
tail
to be or not to
null
null
head tail
to
null
null
head nulltail null
>1 node
1 node
0 nodes
![Page 14: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/14.jpg)
14
/** * Inserts an element at the end of the doubly linked list * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertLast( T data )
![Page 15: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/15.jpg)
15
void insertLast(“be”)
DLL of size 0
?
DLL of size 1
?
DLL of size > 1
?
case
s fo
r si
ze o
f th
e D
LL
Richard Feynman:“Consider simple examples
but not too simple!”(paraphrase)
![Page 16: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/16.jpg)
16
insertLast(“be”)head
tail
to be or not to
null
>1 node
![Page 17: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/17.jpg)
17
insertLast(“be”)head
tail
to be or not to be
null null
null
>1 node
![Page 18: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/18.jpg)
18
insertLast(“be”)head
tail
to be or not to be
null
>1 node
![Page 19: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/19.jpg)
19
insertLast(“be”)head
tail
to be or not to be
null
>1 node
![Page 20: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/20.jpg)
20
insertLast(“be”)head = tail
to
null
null
1 nodes
![Page 21: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/21.jpg)
21
insertLast(“be”)head = tail
to be
null null
null null
1 node
![Page 22: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/22.jpg)
22
insertLast(“be”)head = tail
to be
null
null
1 node
![Page 23: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/23.jpg)
23
insertLast(“be”)head
tail
to be
null
null
1 node
![Page 24: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/24.jpg)
24
insertLast(“be”)head
tail
to be
null
null
1 node
* Same as the case with >1 node
![Page 25: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/25.jpg)
25
insertLast(“be”)head null tail null
0 nodes
![Page 26: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/26.jpg)
26
insertLast(“be”)head null tail null
be
null
null
0 nodes
![Page 27: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/27.jpg)
27
insertLast(“be”)head = tail
be
null
null
0 nodes
![Page 28: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/28.jpg)
28
/** * Inserts an element at the beginning of the doubly linked list * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertFirst( T data )
how can we implement this?
![Page 29: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/29.jpg)
29
void insertFirst(“be”)
DLL of size 0
?
DLL of size 1
?
DLL of size > 1
?
case
s fo
r si
ze o
f th
e D
LLwrite down an examplefor each case
![Page 30: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/30.jpg)
30
/** * Inserts an element in the doubly linked list * @param pos : The integer location at which the new data should be * inserted in the list. We assume that the first position in the list * is 0 (zero). If pos is less than 0 then add to the head of the list. * If pos is greater or equal to the size of the list then add the * element at the end of the list. * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertBefore( int pos, T data )
![Page 31: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/31.jpg)
31
void insertBefore(pos,“be”)
pos == 0(insert at the head of the
DLL)
0 < pos < DLL.size -1(insert in the middle of
the DLL)
pos == DLL.size -1
pos < 0(insert at the
head of the DLL)
pos ≥ DLL.size(insert at the end
of the DLL)
DLL of
size 0
DLL of
size 1
DLL of
size > 1
![Page 32: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/32.jpg)
32
void insertBefore(pos,“be”)
pos == 0(insert at the head of the
DLL)
0 < pos < DLL.size -1(insert in the middle of
the DLL)
pos == DLL.size -1
pos < 0(insert at the
head of the DLL)
pos ≥ DLL.size(insert at the end
of the DLL)
DLL of
size 0
insertFirst(“be”)
DLL of
size 1
insertFirst(“be”)
DLL of
size > 1
insertFirst(“be”)
![Page 33: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/33.jpg)
33
void insertBefore(pos,“be”)
pos == 0(insert at the head of the
DLL)
0 < pos < DLL.size -1(insert in the middle of
the DLL)
pos == DLL.size -1
pos < 0(insert at the
head of the DLL)
pos ≥ DLL.size(insert at the end
of the DLL)
DLL of
size 0
insertFirst(“be”) not possible0 < pos < -1
DLL of
size 1
insertFirst(“be”) not possible0 < pos < 0
DLL of
size > 1
insertFirst(“be”) ?
![Page 34: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/34.jpg)
34
void insertBefore(pos,“be”)
pos == 0(insert at the head of the
DLL)
0 < pos < DLL.size -1(insert in the middle of
the DLL)
pos == DLL.size -1
pos < 0(insert at the
head of the DLL)
pos ≥ DLL.size(insert at the end
of the DLL)
DLL of
size 0
insertFirst(“be”) not possible0 < pos < -1
insertFirst(“be”)
(here pos == -1)
DLL of
size 1
insertFirst(“be”) not possible0 < pos < 0
insertFirst(“be”)
(here pos == 0)
DLL of
size > 1
insertFirst(“be”) ? ?
![Page 35: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/35.jpg)
35
void insertBefore(pos,“be”)
pos == 0(insert at the head of the
DLL)
0 < pos < DLL.size -1(insert in the middle of
the DLL)
pos == DLL.size -1
pos < 0(insert at the
head of the DLL)
pos ≥ DLL.size(insert at the end
of the DLL)
DLL of
size 0
insertFirst(“be”) not possible0 < pos < -1
insertFirst(“be”)
(here pos == -1)
insertFirst(“be”) InsertLast(“be”)
DLL of
size 1
insertFirst(“be”) not possible0 < pos < 0
insertFirst(“be”)
(here pos == 0)
insertFirst(“be”) InsertLast(“be”)
DLL of
size > 1
insertFirst(“be”) ? ? insertFirst(“be”) InsertLast(“be”)
![Page 36: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/36.jpg)
36
void insertBefore(pos,“be”)
pos == 0(insert at the head of the
DLL)
0 < pos < DLL.size -1(insert in the middle of
the DLL)
pos == DLL.size -1
pos < 0(insert at the
head of the DLL)
pos ≥ DLL.size(insert at the end
of the DLL)
DLL of
size 0
insertFirst(“be”) not possible0 < pos < -1
insertFirst(“be”)
(here pos == -1)
insertFirst(“be”) InsertLast(“be”)
DLL of
size 1
insertFirst(“be”) not possible0 < pos < 0
insertFirst(“be”)
(here pos == 0)
insertFirst(“be”) InsertLast(“be”)
DLL of
size > 1
insertFirst(“be”) ? same as case to the left
insertFirst(“be”) InsertLast(“be”)
![Page 37: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/37.jpg)
37
insertBefore(2,“nice”)head
tail
to be or not to be
null
null
![Page 38: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/38.jpg)
38
insertBefore(2,“nice”)head
ptr
tail
to be or not to be
null
null
![Page 39: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/39.jpg)
39
insertBefore(2,“nice”)head
ptr
tail
to be or not to be
null
null
tmp
nice
null
null
![Page 40: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/40.jpg)
40
insertBefore(2,“nice”)head
ptr
tail
to be or not to be
null
null
tmp
nice
null
null
![Page 41: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/41.jpg)
41
insertBefore(2,“nice”)head
ptr
tail
to be or not to be
null
null
tmp
nice
null
![Page 42: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/42.jpg)
42
insertBefore(2,“nice”)head
ptr
tail
to be or not to be
null
null
tmp
nice
null
![Page 43: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today](https://reader035.vdocuments.us/reader035/viewer/2022062301/5697c0141a28abf838ccd5fc/html5/thumbnails/43.jpg)
43
insertBefore(2,“nice”)head
ptr
tail
to be or not to be
null
null
tmp
nice