1 / 24

# Singly Linked Lists - PowerPoint PPT Presentation

Singly Linked Lists. Representation Space Analysis Creation and Insertion Traversal Search Deletion. list. head. tail. Representation. We are using a representation in which a linked list has both head and tail references. public class MyLinkedList{ protected Element head;

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

• Representation

• Space Analysis

• Creation and Insertion

• Traversal

• Search

• Deletion

tail

Representation

• We are using a representation in which a linked list has both head and tail references.

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;}

}

}

• Now, we can take a look at the space requirements:

= (n + 2) sizeof(MyLinkedList.Element ref) + n sizeof(Object ref)

• 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.

tail

• for (int k = 0; k < 10; k++)

• list.append(new Integer(k));

public void append(Object obj){

Element element = new Element(obj, null);

else

tail.next = element;

tail = element;

}

Complexity is

O(1)

public void prepend(Object obj) {

Element element = new Element(obj, head);

tail = element;

}

Complexity is

O(1)

public void insertBefore(Object obj) {

Element element = new Element(obj, this);

return;

}

while (previous.next != this) {

previous= previous.next;

}

previous.next = element;

}

Complexity is

O(n)

• public void insertAfter(Object obj) {

• next = new Element(obj, next);

• if(this == tail)

• tail = next;

• }

Complexity is

O(1)

To move a reference e from one node to the next:

Example: Count the number of nodes in a linked list.

e = e.next;

• public int countNodes(){

• int count = 0;

• while(e != null){

• count++;

• e = e.next;

• }

• return count;

• }

Complexity is

O(n)

• 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;

while(e != null){

if(e.data.equals(obj))

count++;

e = e.next;

}

return count;

}

Complexity is ….

• To delete an element, we use either the extract method of MyLinkedList or that of the Element inner class.

public void extract(Object obj) {

Element previous = null;

while(element != null && ! element.data.equals(obj)) {

previous = element;

element = element.next;

}

if(element == null)

else

previous.next = element.next;

if(element == tail)

tail = previous;

}

Complexity is …

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){

• }

• if(e != null)

• e.extract();

• else

Deletion – Deleting First and Last Element Element extracts

public void extractFirst() {

tail = null;

}

Complexity is …

public void extractLast() {

if(tail == null)

else {

while (previous.next != tail)

previous = previous.next;

previous.next = null;

tail = previous;

}

}

Complexity is …

Exercises Element extracts

• 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.

• Representation

• Space Analysis

• Creation and Insertion

• Traversal

• Deletion

list

tail

Representation

//. . .

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;}

// . . .

}

}

Doubly Linked Lists : Space Analysis Element extracts

• The space requirements of our representation of the doubly linked lists is as follows:

= 2 sizeof(DoublyLinkedList.Element ref) + n [sizeof(Object ref)

= (2n + 2) sizeof(DoublyLinkedList.Element ref) + n sizeof(Object ref)

b)

tail

List Creation and Insertion

• An empty doubly linked list is created as follows:

• 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..

Insertion at the end (append) Element extracts

public void append(Object obj){

Element element = new Element(obj, null, tail);

else {

tail.next = element;

tail = element;

}

}

Complexity is …

Insertion at the beginning (prepend) Element extracts

public void prepend(Object obj){

Element element = new Element(obj, head, null);

else {

}

}

Complexity is …

Insertion before an element Element extracts

• Inserting before the current node (this) that is neither the first nor the last node:

Element element = new Element(obj, this, this.previous);

this.previous.next = element;

this.previous = element;

Complexity is …

Traversal Element extracts

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.

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;

• while(e != null){

• count++;

• e = e.next;

• }

• return count;

• }

Complexity is …

Traversal Element extracts

Example: The following computes the sum of the last n nodes:

• public int sumLastNnodes(int n){

• if(n <= 0)

• throw new IllegalArgumentException("Wrong: " + n);

• 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;

• }

Complexity is …

Deletion Element extracts

• To delete an element, we use either the extract method of DoublyLinkedList or that of the Element inner class.

• public void extract(Object obj){

• while((element != null) && (!element.data.equals(obj)))

• element = element.next;

• if(element == null)

• 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 …

Exercises Element extracts

• 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?