# linked lists singly linked lists doubly linked lists circular lists self-organizing lists linkedlist...

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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