Вы находитесь на странице: 1из 40

Linked List

CSD-202 Data Structure and Algorithms


Outline
 Limitations of Array
 Linked List
 Operations
 Variations of Linked List
 Array vs Linked List

2 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Limitations of Arrays
 Arrays are stored in contiguous memory blocks. And have advantages and
disadvantages due to it.
 It is very easy to access any data element from array using index
 We need to know size of array before hand.
 We cannot resize array. Because They are static in size
 We can relocate existing array to new array, but still expensive
 Contiguous block cannot be guaranteed insufficient blocks size
 Insertion and deletion is very expensive because it needs shifting of elements
 Solution: Linked list
 A dynamic data structure in which each data element is linked with next element
through some link. Because each element is connected/linked, it will be easy to insert
and delete an element without shifting.
3 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Dynamic Data Structure
 Data structures whose size is not known and they can grow/shrink during use are
created using dynamic memory allocation concept.
 Dynamic Memory Allocation
 A free block is allocated using new operator, size is not known until creation. A reference to
that block is returned which needs to be stored in a compatible variable, which is a reference
variable. See following where L is reference:
List L = new List(5)
 How Data Structure will be made?
 If we need a linear list of 5 data elements then 5 blocks are created and every block is connected to each
other by storing address of next block in previous block. Now this list of block is linked.
 If we need another block, create and link with existing block
 If we need to delete, remove link
 Make the block unreachable, Java’s garbage collector will mark it free automatically
 So size is actually increasing/decreasing whereas its not possible in arrays

4 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Linked List
 Linked list is a linear collection of homogenous data elements where each
element is connected through a link.
 A single element in linked list is normally called Node. Every node has two
parts:
 Data: actual information data next
 Next Link- a reference to next node in memory

 Linked list of integers with 3 nodes:


1009 1001 1011
15 1001 78 1011 8 NULL

5 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Linked List
1009 1001 1011
Head/Start
15 1001 78 1011 8 NULL

 Head or Start node is a reference the first node of list.


 Because all nodes are connected through links, so if we have first node, any other node
can be accessed by traversing the list.
 If head is NULL, it means list is empty

 Tail or last node is a node of list whose next link will be null, as any other node would
have a link for it’s next node.

6 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Node
 Node can be represented using either structure or class
C java
struct Node{ class Node{
Type data; Type data;
Node* next; Node next;
 Node Operations: }
}

 Constructing a new node


Node* node=new Node Node node=new Node()

 Accessing the data value node->data node.getData()


node.data
node.setData()

node.getNext()
 Accessing the next pointer node->next node.next
node.SetNext()

7 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Node examples
 Node can have individual data members or other objects as well.

class Student{ class Book{


private String name; private String title;
private float gpa; private Author author;

private Student next; private Book next;

//rest of the methods //rest of the methods


} }

8 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Linked List Operations
 Traversal
 Search, print, update etc.
 Insertion
 Deletion

9 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Search Print
 Algorithm: SEARCH(head, V)  Algorithm: PRINT(head)
 Input: reference to first node of list and value to be
searched  Input: reference to first node of list
 Output: return node if value is found other wise null  Output: print all nodes
 Steps:  Steps:
start
start
1. Set ptr=head
2. While (ptr != NULL) 1. Set ptr=head
3. if ptr.data==V 2. While (ptr != NULL)
4. return ptr 3. print ptr.data
5. end if 4. ptr=ptr.next
6. ptr=ptr.next // update ptr so it can refer to next node
5. End While
7. End While
8. return NULL end
end

10 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Insertion
 Inserting a new node involves linking:
 Linking this node to its logical predecessor and successor node, so all nodes still
remain linked
 There can be various scenarios to insert a new node
 Insertion at start
 Insertion at end
 Insertion at given location

11 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Insertion at Start Insertion at End
 Special case: list is empty  Special case: list is empty
head Ø head Ø

data Ø data Ø

 General Case: list is not empty  General case: list is not empty

head data data Ø head data data Ø

data Ø data Ø

12 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Insertion at Start
 Algorithm: INSERT_START(head, newestNode)  Algorithm: INSERT_END(head, newestNode)
 Input: head and new node  Input: head node and node to be inserted
 Output: list with new node inserted  Output: list with new node inserted
 Steps:  Steps:

Start Start:
1. If head==NULL // list is empty
1. newestNode.next=head //link to head
2. head=newestNode
2. head=newestNode //update head
3. Else //list is not empty. Search last node
End
4. Set curr=head
5. While(curr.next!= NULL)
6. curr=curr.next
7. End While // loop will terminate when reached to last
node
8. curr.next=newestNode // update the next link of last
node
9. End If
End
13 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Insertion at Location
 Location must be valid:  Case 2: in middle
 >=0 and <=size, assume index of first node=0  Let say 2
previous node node at target index
 Case 1: index =0 (Insertion at start)
head data data data *
 List can be empty

head Ø data Ø

data Ø
 Case 3: index =size (Insertion at end)
 List can be non-empty

head data data * head data data Ø

data Ø
data Ø
*: Remaining list is not shown
14 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Insertion at Location
 Algorithm: INSERT_LOCATION(head, newestNode)
 Input: head node and node to be inserted
 Output: list with new node inserted
 Steps:

 Discussed in class

15 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Deletion
 Deleting a new node involves unlinking:
 Unlinking the node in a way that its logical predecessor gets connected to next node of
list to maintain linking
 There can be various scenarios to delete a node
 Deletion at start
 Deletion at end
 Deletion of a node, data is given
 Deletion of a node, node number(index) is given

16 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Deletion
 Deletion From Start
 Special case: list is empty, no deletion
 General case:

head data data data Ø

 Deletion From End


 Special case: list is empty, no deletion
 Special case: only one node
head data Ø Ø

 General case

head data data data Ø Ø


previous node last node

17 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Deletion
 Algorithm: DELETE_END(head)
 Algorithm: DELETE_START(head)
 Input: reference to first node
 Input: reference to first node  Output: new list with last node deleted
 Output: new list with first node deleted  Steps:
Start:
 Steps:
1. If head!=NULL // list is not empty
Start 2. Set curr=head, prev=NULL
1. If head!=NULL// list is not empty 3. While (curr.next!=NULL)
4. prev=curr
2. head=head.next 5. curr=curr.next
3. End If 6. End While
7. If prev==null //curr is head
End
8. head=NULL
9. Else
10. prev.next=NULL
11. End If
12. End If
End

18 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Deletion at Location
 Location must be valid:  Special case: index is last (deletion at end)
 >=0 and <size, assume index of first
node=0
 Special case: index =0 (deletion at start) data Ø Ø
head data data
previous node target node

head data data Ø


 Having index in middle or at last have
same effect as we only need to update
 General case: index is in middle previous node.
 Let say 2

previous node target node next node

head data data data Ø

19 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Deletion at Location
 Algorithm: DELETE_LOCATION(head, index) Start:
1. Set nodeCount=0
 Input: reference to first node and Index
2. If head!=NULL // list is not empty
 Output: new list with node deleted 3. Set curr=head, prev=NULL
 Steps: 4. While (curr.next!=NULL)
5. nodeCount=nodeCount+1
6. If index== nodeCount-1
7. If prev==NULL //curr is head, case 1
8. head=head.next
9. Else //case 2 and 3
10. prev.next=curr.next
11. End If
12. return
13. End If
14. prev=curr //go to next nodes
15. curr=curr.next
16. End While
17. End If
 End

20 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Tail Node
 Whenever we need to update last node, we need to search for it starting from first
node. This process involves loop which can be avoided:
 By maintaining a reference to last node just like we do for first node.
 It will save time for insertion at end
 How?
 Insertion at End
 List is not empty
head data data data Ø tail
 tail.next=newestNode;
 tail=NewestNode
 List is empty
 head=newestNode
 tail=newestNode
 Deletion at End
 Need to search for 2nd last node, no going back from last to previous node

21 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Types of Linked List
 Depending upon how links are maintained, there can be variations of linked
list:
 Singly Linked List (Discussed is previous slides)
 Every node contains only one next link which points to next node in list
 Last nodes points to NULL.

head data data data Ø tail

 Doubly Linked List


 Circular Linked List

22 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Doubly Linked List
 Every node contains two links, next which points to next node and previous
which points to previous node in list

head Ø data data data Ø tail

 Previous link of first node is NULL


 Next link of last node is NULL

 Doubly linked list can be traversed from start to end and from end to start.

23 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Insertion at Start
 List is empty  Algorithm: INSERT_START(head, tail, newstNode)
 Input: head node and new node
head Ø tail  Output: list with new node inserted
 Steps:

Ø data Ø Start
1. newestNode.next=head
2. If head!=NULL // list was not empty
 List is not empty
3. head.prev=newestNode
4. Else // list was empty
head Ø data data Ø 5. tail=newestNode // update tail, as this is first node
6. End If
7. head=newestNode // head would always be updated
Ø data Ø tail
End

24 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Insertion at End
 Algorithm: INSERT_END(head, tail, newestNode)
 List is empty
 Input: head node and node to be inserted
 Output: list with new node inserted
head Ø tail
 Steps:
Start:
Ø data Ø 1. If head!=NULL // list not empty
2. tail.next=newestNode
 List is not empty 3. newestNode.prev=tail
4. tail=newestNode
5. Else //list is empty
head Ø data data Ø tail 6. head=newestNode
7. tail=newestNode
8. End If
Ø data Ø
End

25 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Insertion at Location
 Case 1: index is 0 (Insertion at start)  Case 2: in middle
 List is empty  Let say 4
previous node node at target index
head Ø tail *
* data data

Ø data Ø
Ø data Ø

 List is not empty  Case 3: index=size (Insertion at end)


head Ø data data Ø
data data Ø tail
*

Ø data Ø tail
Ø data Ø

*: Remaining list is not shown


26 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Insertion at Location
 Algorithm: INSERT_LOCATION(head, tail, index, 7. While (curr != NULL)
newestNode) 8. nodeCount=nodeCount+1
 Input: head node, new node and index of new node 9. If (index== nodeCount -1) //case 2, middle

 Output: list with new node inserted 10. (curr.prev).next=newestNode


11. newestNode.prev=curr.prev
 Steps:
12. newestNode.next=curr
Start 13. curr.prev=newestNode
1. If Index==0 // case 1, start 14. return
2. If head!=NULL // list is empty 15. End If
3. newestNode.next=head 16. curr=curr.next
4. head.prev=newestNode 17. End While
5. Else 18. If index==size-1 //case 3, last
6. tail=newestNode 19. tail.next=newestNode
7. End If 20. newestNode.prev=tail
8. head=newestNode // head would always refer to new node if index is 0 21. tail=newestNode
9. Else 22. return
6. Set curr=head.next, prev= head, nodeCount=1 23. End If
End
27 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Deletion at Start
 List has only one node  Algorithm: DELETE_START(head, tail)
 Input: reference to first and last node
head tail  Output: new list with node deleted
Ø data Ø
 Steps:
You should review the following algorithm yourself
Ø
Start
1. If head!=NULL // list is not empty
 List has at least 2 nodes
2. head=head.next If head!=NULL
3. head.prev=NULL // unlink node
head *
Ø data data 4. Else // there is only one node
5. tail=NULL
Ø 6. End If
7. End If
End
*: Remaining list is not shown
28 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Deletion at End
 Algorithm: DELETE_END(head, tail)
 List has only one node
 Input: reference to first and last node
 Output: new list with node deleted
head Ø data Ø tail
 Steps:
Start:
Ø 1. If head!=NULL // list is not empty
2. If head.next==NULL // there is only one node
 List has at least 2 nodes 3. head=NULL
4. tail=NULL
5. Else //there are multiple nodes
data data tail
* 6. tail=tail.prev
7. tail.next=NULL
Ø 8. End If
9. End If
End
*: Remaining list is not shown
29 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Deletion at Location
 Case 1: index is 0 (Deletion at start)  Case 2: in middle
 List has only one node  Let say 4
previous node node at target index next node
head Ø data Ø tail *
* data data data

 Case 3: index is last (Deletion at end)


 List has multiple nodes

* data data tail


head Ø data data *

Ø
Ø

*: Remaining list is not shown


30 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Deletion at Location
 Algorithm: DELETE_LOCATION(head, tail, Index)
 Input: reference to first node, last node and Index
 Output: new list with node deleted
 Steps:

 Discussed in class

31 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Circular Linked List
 Single Circular
 Every node contains only one next link which points to next node in list.
 Last nodes points to first node of list

head data data data tail

 Doubly Circular
 Every node contains two links, one points to next node and one point to previous node in sequence.

head data data data tail

 Previous link of first node points last node


 Next link of last node points to first node

32 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Circular Linked List
 What change will be required in following algorithms of both single and
double circular linked list:
 Insert
 Delete
 Search

 When loop will terminate?

33 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Equivalence Testing with linked list
 Read 3.5.2 of text Book Data Structures and algorithms in Java

34 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Removing Duplicates
 Discussed in class

35 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Cloning a Linked List
 Read 3.6.2 of text book Data Structures and Algorithms in Java

36 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Reversing a doubly linked List
 Discussed in class
 If list is empty or only one node present then no need to reverse
 It involves swapping of next and previous pointers throughout the list
 The tail and head references are swapped at the end of reversing
head tail

next
next
next

next
previous

null
previous

previous

previous
16 17 10 20
null
previous
previous previous

head next tail


next

next

next
null
previous
previous

previous

previous
20 10 17 16
null
previous previous previous previous

37 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Array vs. Linked List
 Indexing (get/set)  Memory allocation
rd rd
 Access 3 index of array vs 3 node of list  Static vs. dynamic
 Searching  Contiguous vs linked
 If data is un ordered  Space utilization
 Search until found or end  Array is fixed whereas linked list can
 If data is ordered
grow/shrink
 ArrayMiddle, Lower, Upper index
 Single node vs single cell
calculation is straight forward
 LinkedListCan we do binary search over
linked list(it should be efficient than linear
search)?
 Add/Delete
 Shifting vs. changing links

38 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
Applications of Linked List
 Where size is not fixed, and no random access.
 Few example:
 Other data structures
 Stack, queue, trees, skip list, graphs
 Browser’s back button
 To go to previous URLs
 Card Game
 Deck of cards, no random access

39 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore
 Find the middle of a given linked list
 Total/2
 Full n half pointer
 Odd pointer
 Nth node from the end of a Linked List

40 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019


Safdar Computer Science Department- CIIT Lahore

Вам также может понравиться