Meljun Cortes Data Structures Linked Lists

38
Data Structures Linked Lists certain class of lists may be represented using arrays Lists that are fixed in length. Lists that do not require a considerable amount of insertion and deletion operations first criterion is the fact that arrays cannot dynamically alter their size once they have been defined Linked Lists * Property of STI Page 1 of 38 been defined representing such kind of linear lists is the linked list Two types of linked lists Singly linked list Doubly linked list

description

Meljun Cortes Data Structures Linked Lists

Transcript of Meljun Cortes Data Structures Linked Lists

Page 1: Meljun Cortes Data Structures Linked Lists

Data Structures

Linked Lists

� certain class of lists may be represented using

arrays

� Lists that are fixed in length.

� Lists that do not require a considerable

amount of insertion and deletion operations

� first criterion is the fact that arrays cannot

dynamically alter their size once they have

been defined

Linked Lists * Property of STIPage 1 of 38

been defined

� representing such kind of linear lists is the

linked list

� Two types of linked lists

� Singly linked list

� Doubly linked list

Page 2: Meljun Cortes Data Structures Linked Lists

Data Structures

� basic format of a node

� data field or simply data is used to contain the

value of the element

� pointer field, link, or simply, reference, contains

the address of the next node in the list of a

computer memory

Singly-linked list

Linked Lists * Property of STIPage 2 of 38

� All references are the same size no matter what

they refer in a given computer/operating system

� The "Next Node" in the list is called the

SUCCESSOR

Page 3: Meljun Cortes Data Structures Linked Lists

Data Structures

� Head – first node in the list contains the “Martin”

� There is no limit to the size of a singly-linked list

� Adding a node to a linked list is simply a matter

Singly-linked list

Singly-linked list with 4 nodes

Linked Lists * Property of STIPage 3 of 38

� Adding a node to a linked list is simply a matter

of:

� Creating a new node.

� Setting the data field of the new node to the

value to be inserted into the list.

� Assigning the address of the new node to

the pointer field of the current last node in

the list.

� Setting the pointer field of the new node to

NULL.

Page 4: Meljun Cortes Data Structures Linked Lists

Data Structures

� Common operations

� Insert - usually inserted at the beginning of

the list

� Search - moves along the list searching for

the specified value then prompts you the

address of the node

� Delete – search for the value and delete the

data then connects the arrow from the

previous link straight across to the following

Singly-linked list

Linked Lists * Property of STIPage 4 of 38

previous link straight across to the following

link

Page 5: Meljun Cortes Data Structures Linked Lists

Data Structures

� Algorithm for basic operations

1. Inserting an item at the beginning of the list

� make a new link

� set a new link to the old first

� set first to new link

public void insertFirst(int id,

dd)

{

Singly-linked list

Linked Lists * Property of STIPage 5 of 38

{

Link newLink = new Link(id,

dd);

newLink.next = first;

first = newLink;

}

Page 6: Meljun Cortes Data Structures Linked Lists

Data Structures

2. Deleting an item at the beginning of the list

� save reference to link

� delete it, set first to old next

� return deleted link

public deleteFirst()

{

Link temp = first;

Singly-linked list

Linked Lists * Property of STIPage 6 of 38

Link temp = first;

first = first.next;

return temp;

}

Page 7: Meljun Cortes Data Structures Linked Lists

Data Structures

� display the entire list, start at first and follow the

chain of references from link to link

� start at the beginning of last until end of list

� print data

� move to next link

public displayList()

{

Singly-linked list

Linked Lists * Property of STIPage 7 of 38

System.out.print(″List (first ����

last): ″); //optional

Link current = first;

while(current != null)

{

current.displayLink();

current = current.next;

}

System.out.println(″″); //optional

}

Page 8: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Finding Specified Links

� Algorithm to find a specific link in a list assuming a non-empty list

� start at the first while no match

� if end of list, didn’t find it

� not end of list, go to the next link

� found it

public Link find(int key)

{

Linked Lists * Property of STIPage 8 of 38

{

Link current = first;

while(current.iData != key) // iData refers to integer data item declared in the class

{

if(current.next == null)

return null;

else

current = current.next;

}

return current;

}

Page 9: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Finding Specified Links

� algorithm of deleting a link assuming a non-empty list

� search for link

� if not found, go to next link

� found it

� if first link, change first

� otherwise, bypass it

public Link delete(int key)

{

Link current = first;

Linked Lists * Property of STIPage 9 of 38

Link previous = first;

while(current.iData != key)

{

if(current.next == null)

return null;

else

{

previous = current;

current = current.next;

}

}

if(current == first)

first = first.next;

else

previous.next = current.next;

return current;

}

Page 10: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� other operations you can perform in a singly-linked list

� Finding the length of the list, Reading the list from left-to-right

� Retrieving the ith node in the list, where i≤n

� Storing a new value into the ith position,

where i≤n

� Inserting a new node at position i, where i≤n

� Deleting the ith element, where i≤n

� Copying a list

Linked Lists * Property of STIPage 10 of 38

� Copying a list

� Sorting the nodes in the list

� Merging two or more lists

� Splitting a list into several sublists

� The following notation will be used for the purpose of this discussion:

� Pointer_Value → Node.Data -

References the Data field of the node whose address is Pointer_Value

� Pointer_Value → Node.Pointer -

References the Pointer field of the node whose address is Pointer_Value

Page 11: Meljun Cortes Data Structures Linked Lists

Data Structures

� Finding the length of the list, Reading the list from left-to-right

� the only way to determine how many nodes

are contained in the list is to traverse it

� the list must be read

� Retrieving the ith node in the list, where i≤≤≤≤n

� the list must be traversed until it reaches the

desired node

Singly-linked list

Linked Lists * Property of STIPage 11 of 38

desired node

� Storing a new value into the ith position, where i≤≤≤≤n

� must be traversed

� storing a new value simply requires

overwriting the data field of the node

� simple modification of retrieving a node

Page 12: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Inserting a Node in a Singly-Linked List

� general procedure for inserting a new node at a certain position in a singly-linked list

1. Create a new node for the element.

2. Set the data field of the new node to the

value to be inserted.

3. Insert the node.

� three locations in which a node may be

Linked Lists * Property of STIPage 12 of 38

� three locations in which a node may be

inserted

1. Insert the node at the start of the list

(i=1)

2. Insert the node at the end of the list

(i>length of the list)

3. Insert the node at position i, where

1<i<length of list

Page 13: Meljun Cortes Data Structures Linked Lists

Data Structures

� Inserting a Node at the Start of a Singly-Linked List (i=1)

� step 3 of the general procedure includes the

following operations

1. Set the pointer field of the new node to

the value of HEAD when the HEAD

points to the current first node in the list

2. Set HEAD to the address of the new

node

Singly-linked list

Linked Lists * Property of STIPage 13 of 38

node

Singly-linked list after inserting “Ken”

Singly-linked list after reassigning HEAD

Page 14: Meljun Cortes Data Structures Linked Lists

Data Structures

� Inserting a Node at the End of a Singly-Linked List (i>Length of List)

� step 3 of the general procedure includes the

following operations

1. Set the pointer field of the current last

node to the address of the new node

when the pointer field of the current last

node contains NULL.

2. Set the pointer field of the new node to

Singly-linked list

Linked Lists * Property of STIPage 14 of 38

2. Set the pointer field of the new node to

NULL

Singly-linked list after inserting “Ken” at the end

Page 15: Meljun Cortes Data Structures Linked Lists

Data Structures

� Inserting a Node at Position i (1<i<Length of List)

� step 3 of the general procedure includes the

following operations:

1. Locate the node at position i-1.

2. Set the pointer field of the new node to

the value of the pointer field of node i-1.

3. Set the pointer field of node i-1 to the

address of the new node.

Singly-linked list

Linked Lists * Property of STIPage 15 of 38

address of the new node.

Singly-linked list after setting the pointer field of “Ken”

Page 16: Meljun Cortes Data Structures Linked Lists

Data Structures

� Inserting a Node at Position i (1<i<Length of List) (cont..)

Singly-linked list

Singly-linked list after inserting “Ken” at position 3

Linked Lists * Property of STIPage 16 of 38

Singly-linked list after setting the pointer field of “Ken”

Singly-linked list after setting the pointer field of “Brando”

Page 17: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Insertion using the “Tail”

1. Create a new node for the element.

2. Set the data field of the new node to the

value to be inserted.

3. Set the pointer field of the new node to the

value of NULL.

4. Set the pointer field of the node referenced

by TAIL to the address of the new node.

5. Set TAIL to the address of the new node.

Linked Lists * Property of STIPage 17 of 38

5. Set TAIL to the address of the new node.

Singly-linked list with HEAD and TAIL pointers

Page 18: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

List after setting the pointer field of “Brando”

List after setting TAIL

Linked Lists * Property of STIPage 18 of 38

public insertEnd (Tail, New_Value)

{

Create new node

Set Address of New Node →→→→

Node.Data to New_Value

Set Address of New Node →→→→

Node.Pointer to NULL

Set Tail→→→→ Node.Pointer to Address of New Node

Set Tail to Address of New Node

}

List after setting TAIL

Page 19: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Deleting a Node from a Singly-Linked List

� By Position

� By Value

� make the following assumptions

� The singly-linked list has a length

of n nodes.

� The node to be deleted is at

position i, where 1≤i≤n

� summarized as follows

Linked Lists * Property of STIPage 19 of 38

� summarized as follows

� Locate the node i.

� Delete the node.

� This step may be further

subdivided into two (2) separate

cases:

1. If the node i is at the HEAD of

the list (i=1).

2. If node i is not the HEAD of

the list (1<i≤n).

� Release the node from memory.

Page 20: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Deleting the Node at the Head of the List (i = 1)

1. Set the variable HEAD to the address

contained in the pointer field of the node to

be deleted.

Linked Lists * Property of STIPage 20 of 38

Singly-linked list after deleting “Anna”

Page 21: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Deleting a Node NOT at the Head of the List(1<i≤≤≤≤n)

� Locate the preceding node (i-1).

� Set the pointer field of the preceding node

(i-1) to the value in the pointer field of the

node to be deleted.

Linked Lists * Property of STIPage 21 of 38

Singly-linked list after deleting “Delia”

Singly-linked list after deleting “Brando”

Page 22: Meljun Cortes Data Structures Linked Lists

Data Structures

Singly-linked list

� Releasing the Node from Memory

Linked Lists * Property of STIPage 22 of 38

Memory Allocation of 3 nodes

Page 23: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� data field and the right pointer field function in

the same manner as in singly-linked lists

� left pointer field is used to contain the address

of the preceding node in the list or what is

known as the PREDECESSOR

Format of a Doubly-linked list

Linked Lists * Property of STIPage 23 of 38

Doubly-linked list with 3 nodes

Page 24: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Traversal Operation

� displayBackward() algorithm

� start at end

� until starts of list

� display data

� move to previous link

link current = last;

Linked Lists * Property of STIPage 24 of 38

while(current != null)

current = current.previous;

{

current.displayLink();

current = current.previous;

Page 25: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Insertion Methods

� insertLast() method algorithm

� make a new link

� if empty list

� first � newLink

� old last � newLink

� old last newLink

� newLink last

public void insertLast(double dd)

Linked Lists * Property of STIPage 25 of 38

public void insertLast(double dd)

{

Link newLink = newLink(dd);

if( isEmpty() )

first = newLink;

else

{

last.next = newLink;

newLink.previous = last;

}

last = newLink;

}

Page 26: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� insertAfter() method algorithm

� start at beginning

� until match is found, move to next link

� not found, return false

� make a new link

� if last link

� newLink � null

� newLink last

Linked Lists * Property of STIPage 26 of 38

� else not last link

� newLink � old next

� newLink old next

� old current newLink

� old current �newLink

� when found – insertion done

Page 27: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

public Boolean insertAfter(double key, double dd)

{

Link current = firstl

while(current.dData != key)

{

current = current.next;

if(current == null)

Return false;

}

Link newLink = new Link(dd);

Linked Lists * Property of STIPage 27 of 38

Link newLink = new Link(dd);

if(current == last)

{

newLink.next = null;

last = newLink;

}

else

{

newLink.next = current.next;

current.next.previous = newLink;

}

newLink.previous = current;

current.next = newLink;

return true;

}

Page 28: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� deleteKey() method algorithm

� first item?

� first � old next

� not first

� old previous � old next

� last item?

� old previous last

� not last

Linked Lists * Property of STIPage 28 of 38

� old previous old next

if(current==first)

first = current.next;

else

current.previous.next =

current.next;

if(current==last)

last = current.previous;

else

current.next.previous =

current.previous;

Page 29: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Inserting a Node into a Doubly-Linked List

� general procedure

� Create a new node for the element.

� Set the data field of the new node to the

value to be inserted.

� Determine the position of the node in

the list based on its value.

� Insert the node.

Linked Lists * Property of STIPage 29 of 38

Doubly-linked list with 3 nodes

Page 30: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Inserting a Node at the Head of the List

New node after setting the left pointer field

Linked Lists * Property of STIPage 30 of 38

Doubly-linked list after setting the new node’s right pointer field

Page 31: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Inserting a Node at the Head of the List (cont..)

Doubly-linked list after setting left pointer field of HEAD node

Linked Lists * Property of STIPage 31 of 38

Doubly-linked list after reassigning HEAD

Page 32: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Insert a Node at the End of a List

New node after setting the right pointer field

Linked Lists * Property of STIPage 32 of 38

Doubly-linked list after setting the new node’s left pointer field

Page 33: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Insert a Node at the End of a List (cont..)

Doubly-linked list after setting right pointer field of TAIL node

Linked Lists * Property of STIPage 33 of 38

Doubly-linked list after reassigning TAIL

Page 34: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Inserting a Node Within the List

New node

Linked Lists * Property of STIPage 34 of 38

Doubly-linked list after setting the new node’s left pointer field

Doubly-linked list after setting the new node’s right pointer field

Page 35: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Inserting a Node Within the List (cont..)

Doubly-linked list after setting the right pointer field of node

Linked Lists * Property of STIPage 35 of 38

Doubly-linked list after setting the right pointer field of node

Doubly-linked list setting the left pointer field of displaced node

Page 36: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Deleting a Node from a Doubly-Linked List

� exactly the same as the steps in deleting a node from a singly-linked list

� Locate the node.

� Delete the node.

� Release the node from memory.

� Deleting the Node at the Head of the List

Linked Lists * Property of STIPage 36 of 38

Doubly-linked list after reassigning HEAD

Doubly-linked list setting the left pointer field of node “D”

Page 37: Meljun Cortes Data Structures Linked Lists

Data Structures

Doubly-Linked List

� Deleting a Node from Within the List

Doubly-linked list setting the right pointer field of node “B”

Linked Lists * Property of STIPage 37 of 38

Doubly-linked list setting the right pointer field of node “B”

Doubly-linked list setting the left pointer field of node “E”

Page 38: Meljun Cortes Data Structures Linked Lists

Data Structures

Exercise

� Create a two element linked list where the

nodes have string values "red" and "green".

The variable head references the node "red".

The process begins by declaring three Node

reference variables head, p, and q.

Linked Lists * Property of STIPage 38 of 38