1. last lecture stacks and queues implemented with a linked list doubly linked lists 2 today

43
1

Upload: linette-cross

Post on 19-Jan-2016

247 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today

1

Page 2: 1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

43

insertBefore(2,“nice”)head

ptr

tail

to be or not to be

null

null

tmp

nice