8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
1/38
Data Structures
Linked Lists
certain class of lists may be represented usingarrays
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 cannotdynamically alter their size once they have
Linked Lists * Property of STI Page 1 of 38
representing such kind of linear lists is thelinked lis t
Two types of linked listsSingly linked listDoubly linked list
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
2/38
Data Structures
basic format of a node
data field or simply data is used to contain the
value of the elementpointer field , link , or simply, reference, containsthe address of the next node in the list of acomputer memory
Singly-linked list
Linked Lists * Property of STI Page 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 theSUCCESSOR
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
3/38
Data Structures
Head – first node in the list contains the “Martin”There is no limit to the size of a singly-linked list
Singly-linked list
Singly-linked list with 4 nodes
Linked Lists * Property of STI Page 3 of 38
of:
Creating a new node.Setting the data field of the new node to thevalue to be inserted into the list.Assigning the address of the new node tothe pointer field of the current last node inthe list.Setting the pointer field of the new node to
NULL.
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
4/38
Data Structures
Common operationsInsert - usually inserted at the beginning ofthe listSearch - moves along the list searching for
the specified value then prompts you theaddress of the nodeDelete – search for the value and delete thedata then connects the arrow from the
revious link strai ht across to the followin
Singly-linked list
Linked Lists * Property of STI Page 4 of 38
link
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
5/38
Data Structures
Algorithm for basic operations1. Inserting an item at the beginning of the list
make a new linkset 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 STI Page 5 of 38
{Link newLink = new Link(id,
dd);newLink.next = first;first = newLink;
}
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
6/38
Data Structures
2. Deleting an item at the beginning of the listsave reference to linkdelete it, set first to old nextreturn deleted link
public deleteFirst()
{
Link temp = first;
Singly-linked list
Linked Lists * Property of STI Page 6 of 38
first = first.next;
return temp;
}
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
7/38
Data Structures
display the entire list, start at first and follow thechain of references from link to link
start at the beginning of last until end of listprint data
move to next link
public displayList()
{
Singly-linked list
Linked Lists * Property of STI Page 7 of 38
System.out.print( ″ List (firstlast): ″ ); //optional
Link current = first;while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.println( ″″ ); //optional}
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
8/38
Data Structures
Singly-linked list
Finding Specified LinksAlgorithm to find a specific link in a listassuming a non-empty list
start at the first while no matchif end of list, didn’t find it
not end of list, go to the next linkfound it
public Link find(int key)
Linked Lists * Property of STI Page 8 of 38
Link current = first;while(current.iData != key) //
iData refers to integer data itemdeclared in the class{
if(current.next == null)return null;
else
current = current.next;}return current;
}
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
9/38
Data Structures
Singly-linked listFinding Specified Links
algorithm of deleting a link assuming a non-emptylist
search for linkif not found, go to next linkfound itif first link, change firstotherwise, bypass it
public Link delete(int key){
Link current = first;
Linked Lists * Property of STI Page 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;}
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
10/38
Data Structures
Singly-linked list
other operations you can perform in a singly-linkedlist
Finding the length of the list, Reading the listfrom left-to-rightRetrieving the ith node in the list, where i ≤ n
Storing a new value into the ith position,where i ≤ nInserting a new node at position i, where i ≤ nDeleting the ith element, where i ≤ n
Linked Lists * Property of STI Page 10 of 38
Sorting the nodes in the listMerging two or more listsSplitting a list into several sublists
The following notation will be used for the purposeof this discussion:
Pointer_Value → Node.Data -References the Data field of the node whoseaddress is Pointer_ValuePointer_Value → Node.Pointer -
References the Pointer field of the nodewhose address is Pointer_Value
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
11/38
Data Structures
Finding the length of the list, Reading thelist from left-to-right
the only way to determine how many nodesare contained in the list is to traverse it
the list must be read
Retrieving the ith node in the list, where i≤≤≤≤ nthe list must be traversed until it reaches the
Singly-linked list
Linked Lists * Property of STI Page 11 of 38
Storing a new value into the ith position,where i≤≤≤≤ nmust be traversedstoring a new value simply requiresoverwriting the data field of the nodesimple modification of retrieving a node
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
12/38
Data Structures
Singly-linked list
Inserting a Node in a Singly-Linked List general procedure for inserting a newnode 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 thevalue to be inserted.
3. Insert the node.
Linked Lists * Property of STI Page 12 of 38
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 , where1
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
13/38
Data Structures
Inserting a Node at the Start of a Singly- Linked List ( i=1 )
step 3 of the general procedure includes thefollowing operations
1. Set the pointer field of the new node tothe value of HEAD when the HEADpoints to the current first node in the list
2. Set HEAD to the address of the newnode
Singly-linked list
Linked Lists * Property of STI Page 13 of 38
Singly-linked list after inserting “Ken”
Singly-linked list after reassigning HEAD
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
14/38
Data Structures
Inserting a Node at the End of a Singly- Linked List (i>Length of List)
step 3 of the general procedure includes thefollowing operations
1. Set the pointer field of the current lastnode to the address of the new nodewhen the pointer field of the current lastnode contains NULL.
2. Set the ointer field of the new node to
Singly-linked list
Linked Lists * Property of STI Page 14 of 38
NULL
Singly-linked list after inserting “Ken” at the end
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
15/38
Data Structures
Inserting a Node at Position i (1
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
16/38
Data Structures
Inserting a Node at Position i (1
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
17/38
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 thevalue of NULL.4. Set the pointer field of the node referenced
by TAIL to the address of the new node.
Linked Lists * Property of STI Page 17 of 38
. .
Singly-linked list with HEAD and TAIL pointers
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
18/38
Data Structures
Singly-linked list
List after setting the pointer field of “Brando”
Linked Lists * Property of STI Page 18 of 38
public insertEnd (Tail, New_Value){Create new nodeSet Address of New Node →→→→
Node.Data to New_ValueSet Address of New Node →→→→
Node.Pointer to NULL
Set Tail →→→→ Node.Pointer to Addressof New NodeSet Tail to Address of New Node
}
List after setting TAIL
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
19/38
Data Structures
Singly-linked list
Deleting a Node from a Singly-Linked List By PositionBy Value
make the following assumptions
The singly-linked list has a lengthof n nodes.The node to be deleted is atposition i, where 1 ≤ i≤ n
Linked Lists * Property of STI Page 19 of 38
summar ze as o owsLocate the node i.
Delete the node.This step may be furthersubdivided into two (2) separatecases:
1. If the node i is at the HEAD ofthe list (i=1).
2. If node i is not the HEAD ofthe list (1 < i≤ n).
Release the node from memory.
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
20/38
Data Structures
Singly-linked list
Deleting the Node at the Head of the List (i =1)
1. Set the variable HEAD to the addresscontained in the pointer field of the node tobe deleted.
Linked Lists * Property of STI Page 20 of 38
Singly-linked list after deleting “Anna”
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
21/38
Data Structures
Singly-linked list
Deleting a Node NOT at the Head of the List (1
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
22/38
Data Structures
Singly-linked list
Releasing the Node from Memory
Linked Lists * Property of STI Page 22 of 38
Memory Allocation of 3 nodes
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
23/38
Data Structures
Doubly-Linked List
data field and the right pointer field function inthe same manner as in singly-linked listsleft pointer field is used to contain the addressof the preceding node in the list or what isknown as the PREDECESSOR
Format of a Doubly-linked list
Linked Lists * Property of STI Page 23 of 38
Doubly-linked list with 3 nodes
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
24/38
Data Structures
Doubly-Linked List
Traversal OperationdisplayBackward() algorithm
start at enduntil starts of list
display datamove to previous link
link current = last;
Linked Lists * Property of STI Page 24 of 38
while(current != null)
current = current.previous;
{current.displayLink();
current = current.previous;
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
25/38
Data Structures
Doubly-Linked ListInsertion Methods
insertLast() method algorithmmake a new linkif empty listfirst newLink
old last newLinkold last newLinknewLink last
Linked Lists * Property of STI Page 25 of 38
pu c vo nsertLast ou e{
Link newLink = newLink(dd);if( isEmpty() )
first = newLink;else{
last.next = newLink;
newLink.previous = last;}last = newLink;
}
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
26/38
Data Structures
Doubly-Linked List
insertAfter() method algorithmstart at beginninguntil match is found, move to next linknot found, return false
make a new linkif last linknewLink nullnewLink last
Linked Lists * Property of STI Page 26 of 38
else not last linknewLink old next
newLink old nextold current newLinkold current newLinkwhen found – insertion done
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
27/38
Data Structures
Doubly-Linked List
public Boolean insertAfter(double key,double dd)
{Link current = firstlwhile(current.dData != key){
current = current.next;if(current == null)
Return false;}
Linked Lists * Property of STI Page 27 of 38
n new n = new n ;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;
}
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
28/38
Data Structures
Doubly-Linked List
deleteKey() method algorithmfirst item?first old nextnot first
old previous old nextlast item?old previous lastnot last
Linked Lists * Property of STI Page 28 of 38
old previous old next
if(current==first)first = current.next;
elsecurrent.previous.next =
current.next;if(current==last)
last = current.previous;else
current.next.previous =current.previous;
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
29/38
Data Structures
Doubly-Linked List
Inserting a Node into a Doubly-Linked Listgeneral 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 inthe list based on its value.Insert the node.
Linked Lists * Property of STI Page 29 of 38
Doubly-linked list with 3 nodes
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
30/38
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 STI Page 30 of 38
Doubly-linked list after setting the new node’s rightpointer field
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
31/38
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 no
Linked Lists * Property of STI Page 31 of 38
Doubly-linked list after reassigning HEAD
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
32/38
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 STI Page 32 of 38
Doubly-linked list after setting the new node’s left pointer fi
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
33/38
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 nod
Linked Lists * Property of STI Page 33 of 38
Doubly-linked list after reassigning TAIL
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
34/38
Data Structures
Doubly-Linked List
Inserting a Node Within the List
New node
Linked Lists * Property of STI Page 34 of 38
Doubly-linked list after setting the new node’s left pointer fi
Doubly-linked list after setting the new node’s right pointer fi
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
35/38
Data Structures
Doubly-Linked List
Inserting a Node Within the List (cont..)
Linked Lists * Property of STI Page 35 of 38
Doubly-linked list after setting the right pointer field of node
Doubly-linked list setting the left pointer field of displaced n
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
36/38
Data Structures
Doubly-Linked List
Deleting a Node from a Doubly-Linked Listexactly the same as the steps in deletinga 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 STI Page 36 of 38
Doubly-linked list after reassigning HEAD
Doubly-linked list setting the left pointer field of node “D”
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
37/38
Data Structures
Doubly-Linked List
Deleting a Node from Within the List
Doubl -linked list settin the ri ht ointer field of node “B”
Linked Lists * Property of STI Page 37 of 38
Doubly-linked list setting the left pointer field of node “E”
8/9/2019 MELJUN CORTES DATA STRUCTURES Linked Lists_NoRestriction
38/38
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 Nodereference variables head, p, and q.