![Page 1: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/1.jpg)
Lecture 4
Linked Lists
King Fahd University of Petroleum & MineralsCollege of Computer Science & Engineering
Information & Computer Science Department
![Page 2: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/2.jpg)
Singly Linked List
2
![Page 3: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/3.jpg)
Singly Linked List
Representation
Space Analysis
Creation and Insertion
Traversal
Search
Deletion
3
![Page 4: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/4.jpg)
Representation
We are using a representation in which a linked list has both head and tail references .
list head
tail
public class MyLinkedList{ protected Element head; protected Element tail; public final class Element{ Object data;
Element next;Element(Object obj, Element element){ data = obj; next = element;}
public Object getData(){return data;}public Element getNext(){return next;}
}}
4
![Page 5: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/5.jpg)
Representation: Space Analysis
Now, we can take a look at the space requirements:
S(n) = sizeof(MyLinkedList) + n sizeof(MyLinkedList.Element) = 2 sizeof(MyLinkedList.Element ref) + n [sizeof(Object ref)
+ sizeof(MyLinkedList.Element ref)] = (n + 2) sizeof(MyLinkedList.Element ref) + n sizeof(Object ref)
Space RequireExplanation
sizeof(MyLinkedList)
The list reference has two fields:
head (type: Element) and tail (type: Element)
= 2 sizeof(MyLinkedList.Element ref)
n
sizeof(MyLinkedList.Element)
The list has n elements of type Element. Each element has two fields-- data (type Object) and next (type Element).
5
![Page 6: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/6.jpg)
List Creation and Insertion
An empty list is created as follows:
Once created, elements can be inserted into the list using either the append or prepend methods
Also if we have reference to a node (an element), we can use insertAfter or InsertBefore of the Element class.
head
tail
MyLinkedList list = new MyLinkedList;()
for (int k = 0; k < 10; k++) list.append(new Integer(k));
6
![Page 7: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/7.jpg)
public void append(Object obj){ Element element = new Element(obj, null); if(head == null) head = element; else tail.next = element; tail = element;}
Insertion at the end (Append)
Complexity is O(1)
7
![Page 8: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/8.jpg)
public void prepend(Object obj) { Element element = new Element(obj, head); if(head == null) tail = element; head = element;}
Insertion at the beginning (Prepend)
Complexity is O(1)
8
![Page 9: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/9.jpg)
Insertion before and after an element
public void insertBefore(Object obj) { Element element = new Element(obj, this); if(this == head) { head = element; return; } Element previous = head; while (previous.next != this) { previous = previous.next; } previous.next = element;}
Complexity is
public void insertAfter(Object obj) { next = new Element(obj, next); if(this == tail) tail = next;}
Complexity is O(1)
O(n)
9
![Page 10: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/10.jpg)
Traversal
To move a reference e from one node to the next:
Example: Count the number of nodes in a linked list.public int countNodes(){ int count = 0; Element e = head; while(e != null){ count++;
e = e.next; } return count;}
e = e.next;
Complexity is O(n)
10
![Page 11: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/11.jpg)
Searching
To search for an element, we traverse from head until we locate the object.Example: Count the number of nodes with data
field equal to a given object.
public int countNodes(Object obj){ int count = 0; Element e = head; while(e != null){ if(e.data.equals(obj)) count++;
e = e.next; } return count;}
Complexity is.…
11
O(n)
![Page 12: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/12.jpg)
public void extract(Object obj) { Element element = head; Element previous = null; while(element != null && ! element.data.equals(obj)) { previous = element; element = element.next; }
if(element == null) throw new IllegalArgumentException("item not found"); if(element == head) head = element.next; else previous.next = element.next; if(element == tail) tail = previous;}
Deletion
To delete an element, we use either the extract method of MyLinkedList or that of the Element inner class.
Complexity is…
12
O(n)
![Page 13: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/13.jpg)
Deletion - Difference between the MyLinkedList and the Element extracts
To delete an element, we use either the extract method of MyLinkedList or that of the Element inner class.
try{ list.extract(obj1);} catch(IllegalArgumentException e){ System.out.println("Element not found");}
MyLinkedList.Element e = list.find(obj1);if(e != null) e.extract();else System.out.println("Element not found");
13
![Page 14: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/14.jpg)
Deletion – Deleting First and Last Element
public void extractFirst() { if(head == null) throw new IllegalArgumentException("item not found"); head = head.next; if(head == null) tail = null;}
public void extractLast() { if(tail == null) throw new IllegalArgumentException("item not found"); if (head == tail) head = tail = null; else { Element previous = head; while (previous.next != tail) previous = previous.next; previous.next = null; tail = previous; }}
Complexity is…
Complexity is…
14
O(1)
O(n)
![Page 15: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/15.jpg)
Exercises
For the MyLinkedList class, Implement each of the following methods: String toString() Element find(Object obj) void insertAt(int n) //counting the nodes from 1.
State the complexity of each method.
Which methods are affected if we do not use the tail reference in MyLinkedList class.
15
![Page 16: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/16.jpg)
Doubly Linked List
16
![Page 17: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/17.jpg)
Doubly Linked Lists
Representation
Space Analysis
Creation and Insertion
Traversal
Deletion
17
![Page 18: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/18.jpg)
Representation
public class DoublyLinkedList{ protected Element head, tail; //. . . public class Element { Object data; Element next, previous; Element(Object obj, Element next, Element previous){ data = obj; this.next = next; this.previous = previous; } public Object getData(){return data;} public Element getNext(){return next;} public Element getPrevious(){return previous;} // . . . }}
list head
tail
18
![Page 19: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/19.jpg)
Doubly Linked Lists : Space Analysis
The space requirements of our representation of the doubly linked lists is as follows: S(n) = sizeof(DoublyLinkedList) + n
sizeof(DoublyLinkedList.Element) = 2 sizeof(DoublyLinkedList.Element ref) + n [sizeof(Object
ref) + 2 sizeof(DoublyLinkedList.Element ref)]
= (2n + 2) sizeof(DoublyLinkedList.Element ref) + n sizeof(Object ref)
Required spaceExplanation
sizeof(DoublyLinkedList)The list reference has two fields:
head (type: Element) and tail (type: Element)
= 2 sizeof(DoublyLinkedList.Element ref)
n sizeof(DoublyLinkedList.
Element)
The list has n elements of type Element. Each element has three fields-- previous (type Element), data (type Object), and next (type Element)
19
![Page 20: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/20.jpg)
List Creation and Insertion
An empty doubly linked list is created as follows:DoublyLinkedList list = new DoublyLinkedList();
Like singly link list, once created, elements can be inserted into the list using either the append or prepend methods
for (int k = 0; k < 10; k++) list.append(new Int(k));
Also if we have reference to a node (an element), we can use insertAfter or InsertBefore of the Element class..
b(
head
tail
20
![Page 21: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/21.jpg)
Insertion at the end (append)
public void append(Object obj){ Element element = new Element(obj, null, tail); if(head == null) head = tail = element; else { tail.next = element; tail = element; }}
Complexity is…
21
O(1)
![Page 22: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/22.jpg)
Insertion at the beginning (prepend)
public void prepend(Object obj){ Element element = new Element(obj, head, null); if(head == null) head = tail = element; else { head.previous = element; head = element; } }
Complexity is…
22
O(1)
![Page 23: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/23.jpg)
Insertion before an element
Inserting before the current node (this) that is neither the first nor the last node:
Complexity is…
Element element = new Element(obj, this, this.previous);this.previous.next = element;this.previous = element;
23
O(1)
![Page 24: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/24.jpg)
Traversal
For DoublyLinked list, traversal can be done in either direction. Forward, starting from head, or backward starting from tail.
Example: Count the number of nodes in a linked list.
Element e = head;while (e != null) { //do something e = e.next;}
Element e = tail;while (e != null) { //do something e = e.previous;}
public int countNodes(){ int count = 0; Element e = head; while(e != null){ count++; e = e.next; } return count;}
Complexity is…
24
O(n)
![Page 25: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/25.jpg)
public int sumLastNnodes(int n){ if(n <= 0)
throw new IllegalArgumentException("Wrong: " + n); if(head == null)
throw new ListEmptyException();
int count = 0, sum = 0; Element e = tail; while(e != null && count < n){
sum += ((Integer)e.data).intValue(); count++; e = e.previous;
} if(count < n)
throw new IllegalArgumentException(“No. of nodes < "+n); return sum; }
Traversal
Example: The following computes the sum of the last n nodes:
Complexity is…
25
O(n)
![Page 26: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/26.jpg)
Deletion
To delete an element, we use either the extract method of DoublyLinkedList or that of the Element inner class.
public void extract(Object obj){ Element element = head; while((element != null) && (!element.data.equals(obj)))
element = element.next; if(element == null) throw new IllegalArgumentException("item not found"); if(element == head) { head = element.next; if(element.next != null) element.next.previous = null; }else{ element.previous.next = element.next; if(element.next != null) element.next.previous = element.previous; } if(element == tail) tail = element.previous;}
Complexity is…
26
O(n)
![Page 27: Lecture 4 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d385503460f94a11cf5/html5/thumbnails/27.jpg)
Exercises
For the DoublyLinkedList class, Implement each of the following methods and state its complexity. String toString() Element find(Object obj) void ExtractLast() void ExtractFirst() void ExtractLastN(int n)
For the DoublyLinkedList.Element inner class, implement each of the following methods and state its complexity. void insertBefore() void insertAfter() void extract()
What are the methods of DoublyLinkedList and its Element inner class are more efficient than those of MyLinkedList class?
27