You are on page 1of 18

CSD201 – Data Structures and Algorithms (In

C++)

Chapter3: Linked lists

http://www.fpt.edu.vn/ 1
Chapter topics
• Singly linked lists
• Doubly linked lists
• Circular lists
• Self-organizing lists
• LinkedList and ArrayList

http://www.fpt.edu.vn/ 2
Singly linked list node
public 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:

http://www.fpt.edu.vn/ 3
Singly linked list

SLLNode<Integer> p; p
p = new SLLNode<Integer>(2); 2
\

p.next = p
new SLLNode<Integer>(8); 2 8
\ \

p.next.next =
p
new SLLNode<Integer>(3); 2 8 3
\ \

http://www.fpt.edu.vn/ 4
Singly linked list (continued)

public class SLL<T> {


protected SLLNode<T> head, tail;
// methods:
...........
}

head 2 8 3
tail \

// methods:

http://www.fpt.edu.vn/ 5
Singly linked list: insertion at the head

head 5 2 8 3
tail \

special case: 5
\

head null
tail null

http://www.fpt.edu.vn/ 6
Singly linked list: insertion at the tail

head 2 8 3 5
tail \ \

special case: 5
\

head null
tail null

http://www.fpt.edu.vn/ 7
Singly linked list: deletion from the head

head 2 8 3
tail \

special case:

head 3 null
tail \ null

http://www.fpt.edu.vn/ 8
Singly linked list: deletion from the tail

head 2 8 3
tail \ \

special case:

head 3 null
tail \ null

http://www.fpt.edu.vn/ 9
Doubly linked list
public class DLLNode<T> {
public T info;
public DLLNode<T> next, prev;
// constructors:
...........
}
public class DLL<T> {
protected DLLNode<T> head, tail;
// methods:
. . . . . . . . . . .
}
head 2 8 3
tail \
\
// methods:

http://www.fpt.edu.vn/ 10
Doubly linked list: insertion at the head

head 5 2 8 3
tail \
\ \

5
special case: \
\
head null
tail null

http://www.fpt.edu.vn/ 11
Doubly linked list: deletion from the tail

head 2 8 3
tail \ \
\

special case:

head 3 null
tail \ null
\

http://www.fpt.edu.vn/ 12
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 A B C D

• Transpose method: after the desired element is located, swap it with


its predecessor.

A B C D C A B C D

http://www.fpt.edu.vn/ 13
Self-organizing lists (continued)
• Count method: order the list by the number of times elements are
being accessed.
A B C D C A B C D
4 2 2 1 4 2 23 1

• Ordering method: order the list using a criterion natural for


information under scrutiny.

A B C D C A B C D

http://www.fpt.edu.vn/ 14
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

http://www.fpt.edu.vn/ 15
Self-organizing lists: example

element plain move-to- trans- count ordering


front pose

D D D D D1 D
A DA DA DA D1A1 AD
C DAC DAC DAC D1A1C1 ACD
C DAC CDA DCA ACD
C2D1A1
B DACB CDAB DCAB ABCD
C2D1A1B1
A DACB ACDB DACB ABCD
C2A2D1B1
A DACB ACDB ADCB ABCD
A3C2D1B1

A B C
ABC represents the
list

http://www.fpt.edu.vn/ 16
Priority queue: linked list implementation

number of operations enqueue dequeue


unordered list const n

ordered list ≤n const

http://www.fpt.edu.vn/ 17
Q&A

Thank you for your listening!

http://www.fpt.edu.vn/ 18

You might also like