12
Linked Lists

Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Embed Size (px)

Citation preview

Page 1: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Linked Lists

Page 2: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Topics to be discussed…•Linked list•More terminology•Singly-linked lists•Doubly-linked lists•DLLs compared to SLLs•Circular Lists

Page 3: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Linked list

a b c d

Each node contains a valueand a link (pointer or reference) to some other node

The last node contains a null link

myList

back

A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any

position in the sequence.

Page 4: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

More terminology A node’s successor is the next node in the sequence

◦ The last node has no successor A node’s predecessor is the previous node in the sequence

◦ The first node has no predecessor A list’s length is the number of elements in it

◦ A list may be empty (contain no elements)

back

Page 5: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Singly-linked lists Here is a singly-linked list (SLL):

Each node contains a value and a link to its successor (the last node has no successor)

The header points to the first node in the list (or contains the null link if the list is empty)

a b c d

myList

Page 6: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Insertion Description

Insertion at the top of the list Insertion at the end of the list Insertion in the middle of the list

Page 7: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Deletion Description

Deleting from the top of the list Deleting from the end of the list Deleting from the middle of the list

Page 8: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Other operations on linked lists

Most “algorithms” on linked lists—such as insertion, deletion, and searching—are pretty obvious; you just need to be careful

Sorting a linked list is just messy, since you can’t directly access the nth element—you have to count your way through a lot of other elements

back

Page 9: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

back

Doubly-linked lists Here is a doubly-linked list (DLL):

Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any)

The header points to the first node in the list and to the last node in the list (or contains null links if the list is empty)

myDLL

a b c

Page 10: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

DLLs compared to SLLs Advantages:

◦ Can be traversed in either direction (may be essential for some programs)

◦ Some operations, such as deletion and inserting before a node, become easier

Disadvantages:◦ Requires more space

◦ List manipulations are slower (because more links must be changed)

◦ Greater chance of having bugs (because more links must be manipulated)

back

Page 11: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Circular Lists

back

A circular linked list is a linked list in which the head element's previous pointer points to the tail element and the tail element's next pointer points to the head element. In the special case of a circular list with only one element, the element's previous and next pointers point to itself, and it is both the head and tail of the list.

Page 12: Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists

Thank You