250 likes | 615 Views
Linked Lists. Topics to be discussed…. Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists. myList. a. b. c. d. Linked list.
E N D
Topics to be discussed… Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists
myList a b c d Linked list • 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. • Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link back
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
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) myList a b c d Singly-linked lists
Insertion Description • Insertion at the top of the list • Insertion at the end of the list • Insertion in the middle of the list
Deletion Description • Deleting from the top of the list • Deleting from the end of the list • Deleting from the middle of the list
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
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 Doubly-linked lists
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) DLLs compared to SLLs back
Circular Lists 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. back