16
Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java, Third Edition Ch03–1

Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Embed Size (px)

Citation preview

Page 1: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Linked lists

• Singly linked lists• Doubly linked lists• Circular lists• Self-organizing lists• LinkedList and ArrayList

Data Structures and Algorithms in Java, Third Edition Ch03–1

Page 2: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Singly linked list nodepublic class SLLNode<T> { public T info; public SLLNode<T> next; public SLLNode() { next = null; } public SLLNode(T el) { info = el; next = null; } public SLLNode(T el, SLLNode<T> p) { info = el; next = p; }} el

abbreviated as:el

Data Structures and Algorithms in Java, Third Edition Ch03–2

Page 3: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Singly linked list

SLLNode<Integer> p;

p = new SLLNode<Integer>(2);

p.next =

new SLLNode<Integer>(8);

p.next.next =

new SLLNode<Integer>(3);

2

\

p

8

\

2

p

82p

3

\

\

\

Data Structures and Algorithms in Java, Third Edition Ch03–3

Page 4: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Singly linked list

public class SLL<T> { protected SLLNode<T> head, tail; // methods: . . . . . . . . . . .}

headtail

// methods:

2 8 3

\

Data Structures and Algorithms in Java, Third Edition Ch03–4

Page 5: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Singly linked list: insertion at the head

2 8 3

\tail5

headtail

nullnull

special case: 5

\

head

Data Structures and Algorithms in Java, Third Edition Ch03–5

Page 6: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Singly linked list: insertion at the tail

2 8 3headtail

5

\

headtail

nullnull

special case: 5

\

\

Data Structures and Algorithms in Java, Third Edition Ch03–6

Page 7: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Singly linked list: deletion from the head

8 3

\tail

headtail

nullnull

special case:

\

head 2

3

Data Structures and Algorithms in Java, Third Edition Ch03–7

Page 8: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Singly linked list: deletion from the tail

2 8

\tail

headtail

nullnull

special case:

\

head 3

\

3

Data Structures and Algorithms in Java, Third Edition Ch03–8

Page 9: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Doubly linked list

public class DLL<T> { protected DLLNode<T> head, tail; // methods: . . . . . . . . . . .}

head

tail

// methods:

2

\

8 3

\

public class DLLNode<T> { public T info; public DLLNode<T> next, prev; // constructors: . . . . . . . . . . .}

Data Structures and Algorithms in Java, Third Edition Ch03–9

Page 10: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Doubly linked list: insertion at the head

2 8 3

\tail5

\

headtail

nullnull

special case:5

\

\

head

\

Data Structures and Algorithms in Java, Third Edition Ch03–10

Page 11: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Doubly linked list: deletion from the tail

2

\

8

\tail

headtail

nullnull

special case:

\

\

head 3

\

3

Data Structures and Algorithms in Java, Third Edition Ch03–11

Page 12: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Self-organizing lists

Move‑to‑front method: after the desired element is located, put it at the beginning of the list.

A B C D C CA B D

Transpose method: after the desired element is located, swap it with its predecessor.

A B C D C A CB D

Data Structures and Algorithms in Java, Third Edition Ch03–12

Page 13: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

A

4

B

2

C

2

D

1

C

Ordering method: order the list using a criterion natural for information under scrutiny.

A B C D C A B C D

Count method: order the list by the number of times elements are being accessed.

A

4

C

3

B

2

D

1

Self-organizing lists (cont’d)

C

2

Data Structures and Algorithms in Java, Third Edition Ch03–13

Page 14: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Self-organizing lists:accessing elements for the first time

move-to-front, transpose, and count methods:

A B X Y P A B X Y P

ordering method:

A B X Y P A B P X Y

Data Structures and Algorithms in Java, Third Edition Ch03–14

Page 15: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Self-organizing lists: example

element

DACCBAA

plain

DDADACDACDACBDACBDACB

move-to-front

DDADACCDACDABACDBACDB

trans-pose

DDADACDCADCABDACBADCB

count

D1

D1A1

D1A1C1

C2D1A1

C2D1A1B1

C2A2D1B1

A3C2D1B1

ordering

DADACDACDABCDABCDABCD

A B CABC represents the list

Page 16: Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,

Priority queue:linked list implementation

number of operations

enqueue dequeue

unordered list

ordered list

const n

≤ n const

Data Structures and Algorithms in Java, Third Edition Ch03–16