Linked Lists By Rugaia Omer Ahmed Lecture outlines
Linked Lists By Rugaia Omer Ahmed
Lecture outlines Arrays limitation. Lined list definition. Linked list types. Singly linked list SLL Doubly DLL Circular Lists linked list CLL Reference Data Structure and Algorithms in Java, Adam Drozdek. Cengage Learning 2008 (3 rd Edition)
Linked list Array limitations: (1) changing the size of the array requires creating a new array and then copying all data from the array with the old size to the array with the new size (2) the data in the array are next to each n other sequentially in memory, which means that inserting an item inside the array requires shifting some other data in this array. n This limitation can be overcome by using linked structures. n 3
A linked list n n A linked structure is a collection of nodes storing data and links to other nodes. A linked list consists of: n A sequence of nodes my. List a b c d Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link The list may (or may not) have a header 4
More terminology n n n A node’s successor is the next node in the sequence n The last node has no successor A node’s predecessor is the previous node in the sequence n The first node has no predecessor A list’s length is the number of elements in it n A list may be empty (contain no elements) 5
Creating references n n The keyword new creates a new object, but also returns a reference to that object For example, Person p = new Person("John") n new Person("John") creates the object and returns a reference to it n We can assign this reference to p, or use it in other ways 6
Creating links in Java my. List: 44 97 23 19 class Node { int value; Node next; Node (int v, Node n) { // constructor value = v; next = n; } } Node temp = new Node(19, null); = new Node(23, temp); = new Node(97, temp); my. List = new Node(50, temp); 7
Singly-linked lists n singly-linked list (SLL): my. List a b c d A singly linked list is a data structure composed of nodes, each node holding some information and a reference to another node in the list. a node has a link only to its successor in this sequence. n 8
Inserting a node into a SLL n n There are many ways you might want to insert a new node into a list: n As the new first element n As the new last element n Before a given node (specified by a reference) n After a given node n Before a given value n After a given value All are possible, but differ in difficulty 9
Using a header node A header node is just an initial node that exists at the front of every list, even when the list is empty The purpose is to keep the list from being null, and to point at the first element n n numerals head one two void insert. At. Front(Object value) { Node front = new Node(value, this); this. next = front; n } 10
Inserting after (animation) node 2. 5 numerals one two three Find the node you want to insert after First, copy the link from the node that's already in the list Then, change the link in the node that's already in the list 11
Deleting a node from a SLL n n n In order to delete a node from a SLL, you have to change the link in its predecessor This is slightly tricky, because you can’t follow a pointer backwards Deleting the first node in a list is a special case, because the node’s predecessor is the list header 12
Deleting an element from a SLL • To delete the first element, change the link in the header numerals one two three • To delete some other element, change the link in its predecessor numerals (predecessor) one two three • Deleted nodes will eventually be garbage collected 13
Doubly-linked lists n Doubly-linked list (DLL): my. DLL a n n b c 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) 14
DLLs compared to SLLs n n n 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 n n 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) 15
Deleting a node from a DLL Node deletion from a DLL involves changing two links In this example, we will delete node b n n my. DLL a n n n b c We don’t have to do anything about the links in node b Garbage collection will take care of deleted nodes Deletion of the first node or the last node is a special case 16
Circular Lists n n n The list is finite and each node has a successor. An example of such a situation is when several processes are using the same resource for the same amount of time, and we have to assure that each process has a fair share of the resource In an implementation of a circular singly linked list, we can use only one permanent reference, tail, to the list even though operations on the list require access to the tail and its successor, the head. To that end, a linear singly linked list as discussed in Section 3. 1 uses two permanent references, head and tail
- Slides: 17