0% found this document useful (0 votes)
10 views59 pages

DS and AA3

The document provides an overview of linked lists as a dynamic data structure, highlighting their advantages over arrays, such as ease of insertion and deletion. It explains the structure of a linked list, including nodes, head, and tail, and details various operations like insertion, deletion, and traversal. Algorithms for searching, printing, and inserting nodes in different positions within the list are also presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views59 pages

DS and AA3

The document provides an overview of linked lists as a dynamic data structure, highlighting their advantages over arrays, such as ease of insertion and deletion. It explains the structure of a linked list, including nodes, head, and tail, and details various operations like insertion, deletion, and traversal. Algorithms for searching, printing, and inserting nodes in different positions within the list are also presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Edited with the trial version

of Foxit Advanced PDF


Editor
To remove this notice,
visit:
www.foxitsoftware.com/shop
ping

Linked List

Data Structure and Algorithms

Slides credit: Ms. Saba Anwar


Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Outline www.foxitsoftware.com/shopping

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

2 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Limitations of www.foxitsoftware.com/shopping

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 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Dynamic Data Structure www.foxitsoftware.com/shopping

Data structures whose size is not known and they can grow/shrink during use are
created using dynamic memory allocation concept.

Each block of data is allocated dyanamically in memory using the new


operator.

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
Do not forget to delete the dyanmically allocated memory using
delete operator.
4 25/10/2020
So size is actually increasing/decreasing whereas its not
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List www.foxitsoftware.com/shopping

Linked list is a linear collection of homogenous data elements where each


element is connected through a link. The links are maintained using pointers.
A single element in linked list is normally called Node. Every node has
two parts:
Data: actual information
Next Link- a reference to next node in memory

data next

If next link is NULL, it means it is the last node of list.


In order to build a linked list we need to maintain both data and address to next
node
5 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List www.foxitsoftware.com/shopping

Linked list with 4 nodes


Head/Start

We always need location of the first node of list in order to process it for
any purpose
Head or Start node is reference which points to address of the first node of list.
Because all nodes are connected only through pointers, so if we have this head, any node
can
be accessed by traversal.
If head is NULL, it means list is empty
6 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List www.foxitsoftware.com/shopping

Linked list with 4 nodes


Head/Start

Tail

We always need to know the last node of list.


Tail node last node of list whose next pointer will be null.
We can use head to traverse through list to search for tail node.
Or just like head we can maintain a tail reference too

7 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Node www.foxitsoftware.com/shopping

Node can be represented using either structure or class.


Class is also used in C++, but we will focus on struct only. Such kind of struct, which has
pointer referring to the same type of variable, is called self-reeferential structure.

C++
struct Node{
int data;
Node Operations: Node* next;
}
Constructing a new node
Node* node=new Node

Accessing the data value


node->data

Accessing the next pointer node->next

8 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Node examples www.foxitsoftware.com/shopping

Node can have individual data members or other objects as well.

class Student{ class Location{

private: public:

String name; int house;

float gpa; int street;

Location address; String town;

Student* next; String city;

//methods }
}

9 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List Memory Representation www.foxitsoftware.com/shopping

Linked list has nodes and memory has cells, nodes of list are distributed in
memory cells, each node is an object that is dynamically created at run time
and a free memory cell is allocated to that node.
Let say head node of a linked list is located at memory cell 1009. Its data is only
an
integer value.
It points to list’s 2nd node which is located at memory location 1001
2nd node points to 3rd node which is located at 1011.
1009 1001 1011
head 15 1001 78 1011 8 NULL

3rd node points to NULL address, means it is end of list

10 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List Operations www.foxitsoftware.com/shopping

Traversal
Search, print, update etc.
Insertion
Deletion

11 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Search www.foxitsoftware.com/shopping

Algorithm: SEARCH(Head, V)
Input: reference to first node of list and value to be
searched
Output: return node if value is found other wise null
Steps:
startSet
1. ptr=Head
2. While (ptr != NULL)
3. if ptr.data==V
4. return ptr
5. end if
6. ptr=ptr.next // update ptr so it can refer to next
7. End While node
8. return NULL
end

12 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Search www.foxitsoftware.com/shopping

Algorithm: PRINT(Head)
Input: reference to first node of list
Output: print all nodes
Steps:
start
1. Set ptr=Head
2. While (ptr != NULL)
3. print ptr.data
4. ptr=ptr.next // update ptr so it can refer to next node
5. End While
end

13 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion www.foxitsoftware.com/shopping

Inserting a new node involves two things:


Creating a new node
Linking this node to its logical predecessor and successor node, so all nodes still
remain linked
There can be three scenarios to insert a new node
Insertion at Start
Insertion at End
Insertion at Middle

14 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping

If List is Empty List is not Empty


head NULL 1009 1001
head data 1001 data NULL
Create a Node and Update Head
1009 Create new node, and update head
head data NULL pointer 1009 1001
head data 1001 data NULL

1099
data 1009

15 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping

Algorithm: INSERT_START(Head, V)
Input: head and data for new node
Output: list with new node inserted
Steps:
Start
1. newestNode = new Node(V) //create new node
2. If Head==NULL // list is empty
3. Head=newestNode
4. Else //list is not empty
5. newestNode.next=Head
6. Head=newestNode
7. End If
End

16 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start (2) www.foxitsoftware.com/shopping

Algorithm: INSERT_START(Head, newstNode)


Input: head node and new node
Output: list with new node inserted
Steps:
Start
1. If Head==NULL // list is empty
2. Head=newestNode
3. Else //list is not empty
4. newestNode.next=Head
5. Head=newestNode
6. End If
End

17 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping

If List is Empty Non-Empty List


head NULL 1009 1001
head data data NULL
1001
Create a Node and Update Head Create new node
1009 1009 1001
head data NULL head data 1001 data 1099

Update pointer of last node


1099
data NULL

18 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping

If list is not empty then we need to traverse the list to find the last node in order to link the newly crated node in existing list.
Algorithm: INSERT_END(Head, V)
Input: head node and data to be inserted
Output: list with new node inserted
Steps:
Start:
1. newestNode = new Node(V) //create new node
2. If Head==NULL // list is empty
3. Head=newestNode
4. Else //list is not empty. Search last node
5. Set ptr=Head
6. While(ptr.next!= NULL)
7. ptr=ptr.next
8. End While // loop will terminate when last node is found
9. ptr.next=newestNode // update the next pointer of ptr(last node)
10. End If
End

19 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Middle www.foxitsoftware.com/shopping

Let say we want to insert 13 in following list, at location 2.


1009 1001 1099
head
9 1001 7 1099 11

In this case, first


NULL we need to locate the 2nd node. That is node with data=7.

Now this will become 3rd node and new node will be inserted before this node.

1009 1001 1099


head 9 1019 7 1099 11
1019 NULL
13 1001

20 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Middle www.foxitsoftware.com/shopping

In order to insert somewhere middle of linked list, we need to maintain two


pointers, current and previous.
1009 1001 1099
head 9 7 1099 11
1001 NULL

prev curr
So when new node is inserted, we can easily change next links of new
node and previous node.
1009 1001 1099
head 9 1019 7 1099 11
1019 NULL
13 1001

21 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insert at Middle www.foxitsoftware.com/shopping

Algorithm: INSERT_MIDDLE(Head, loc, newestNode)


Input: head node, new node and loc of new node
Output: list with new node inserted 8. While (Curr != NULL)
Steps: 9. nodeCount=nodeCount+1
Start 10. If nodeCount ==Loc
1. If Head==NULL // list is empty 11. newestNode.next=Curr
2. Head=newestNode 12. prev.next=newestNode
3. Else If Loc==1 // update head 13. Break
4. newestNode.next=Head 14. End If
15. Prev=Curr
5. Head=newestNode
16. Curr=Curr.next
6. Else
17. End While
7. Set Curr=Head, Prev= NULL
18. End If
8. nodeCount=0
End

22 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insert at Middle (2) www.foxitsoftware.com/shopping

Algorithm: INSERT_MIDDLE(Head, loc, newestNode)


Input: head node, new node and loc of new node
Output: list with new node inserted 9. if (prev!=NULL)
10. prev.next=newestNode
Steps:
11. else
Start 12. newestNode.next=Head
1. If Head==NULL // list is empty 13. Head=newestNode
2. Head=newestNode 14. End if
3. Else 8. Break
9. End If
4. Set Curr=Head, Prev= NULL,
10. Prev=Curr
nodeCount=0
11. Curr=Curr.next
5. While (Curr != NULL) 12. End While
6. nodeCount=nodeCount+1 13. End If
7. If nodeCount ==Loc End
8. newestNode.next=Curr
23 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Variations of Insert www.foxitsoftware.com/shopping

How the working of algorithm will be changed:


Insertion after a given data value/location
Insertion in a sorted linked list
List is already sorted and after insertion it must remain
sorted No sorting algorithm required

24 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping

Deleting a new node involves two things:


Unlinking the node in a way that its logical predecessor gets connected to next node of
list to maintain linking
There can be three scenarios to delete node
Deletion from Start
Deletion from End
Deletion from Middle

25 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping

Deletion From Start


1009 1001 1099
head 9 1001 7 1099 11
NULL

1001 1099
head 7 1099 11
NULL
Deletion From End
1009 1001 1099
head 9 1001 7 1099 11 NULL

1009 1001
head 9 1001 7 NULL

26 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping

Algorithm: DELETE_END(Head)
Algorithm: Input: reference to first node
DELETE_START(Head) Output: new list with lat node deleted
Steps:
Input: reference to first Start:
node 1. If Head!=NULL// list is not empty
2. If Head.next==NULL// there is only one node
Output: new list with first 3. Head=NULL
node deleted 4. Else
5. Set Curr=Head, Prev=NULL
Steps:
1. If Head!=NULL// list is not 6. While (Curr.next!=NULL)
Startempty 7. Prev=Curr
8. Curr=Curr.next
2. Head=Head.next 9. End While
10. Prev.next=NULL
3. End If End If
End 11.
12.End
If End

27 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping

Deletion at Location
This case involves searching a specific node to delete
We also need to find current and previous node pointers in order to maintain links.
Current will be our required node. Let say we need to delete 2nd from following list
We need to search 2nd in list, and then need to find its previous node also, so before
deletion we can link previous node to next node of 2nd node
1009 1001 1099
head 9 1001 7 1099 11
NULL

prev curr

28 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping

1009 1001 1099


head 9 1001 7 1099 11
NULL

prev curr

1009 1001 1099


head 9 1099 7 1099 11
NULL

prev curr

1009 1099
head 9 1099 11
NULL

29 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping

Algorithm: 1. While (Curr!=NULL)


DELETE_LOCATION(Head, Loc) 2. nodeCount=nodeCount+1
Input: reference to first node and loc
3. If (nodeCount==Loc)
Output: new list with node deleted
Steps: 4. Prev.next=Curr.next
Start: 5. break
1. Set nodeCount=0
2. If Head!=NULL // list is not empty 6. End If
3. If Loc==1 // this is head node 7. Prev=Curr
4. temp = Head 8. Curr=Curr.next
5. Head=Head.next
6. delete temp
9. End While
7. Else 10. End If
8. Set Curr=Head, 11. End If
30
Prev=NULL 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Variations of DELETE www.foxitsoftware.com/shopping

How DELETE_LOCATION will change?


If a data value is given instead of location.?
If a node is given instead of location?

31 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Types of Linked List www.foxitsoftware.com/shopping

Depending upon how links are maintained, there can be variationsof linked
list:
Singly Linked List
We have discussed it already
Doubly
🞂​
Linked List
Circular Linked
List

32 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Singly Linked List www.foxitsoftware.com/shopping

Every node contains only one next link which points to next node in
list 1009 1001 1099
head 9 1001 7 1099 11
NULL

Last nodes points to NULL.

33 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Doubly Linked List www.foxitsoftware.com/shopping

Every node contains two links, next which points to next node and previous
which points to previous node in list
1009 1001 1099
head 1099
NULL 1 0 079 1001 1009 10 011 1001 10 0195 NULL

Note that prev and next links hold address of nodes. So, prev link of node located at
1001 points to 1009 and next link points to 1099.
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.
34 25/10/2020
If we have tail node
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping

If List is Empty? If List is non-Empty?


head NULL 1009 1001
head NULL 1001 NULL
1009 10 011
Create new node, and update head 7
100new
9 node
Create
1009 1011
head NULL 7 NULL NULL 15 1009

Change links
1009 1001
head 1011 1001 NULL
1009 10 011
1007
9
1011
1009
NULL 1009
15
35 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping

Algorithm: INSERT_START(Head, newstNode)


Input: head node and new node
Output: list with new node inserted
Steps:
Start
1. If Head==NULL // list is empty
2. Head=newestNode
3. Else //list is not empty
4. newestNode.next=Head
5. Head.prev=newestNode
6. Head=newestNode
7. End If
End

36 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping

If List is Empty? If List is non-Empty?


head NULL 1009 1001
head NULL 1001 NULL
1009 10 011
Create new node, and update head 7
100new
9 node
Create
1009 1011
head NULL 7 NULL 15
NULL 1009 NULL
Change links

1009 1001
head NULL 1001
1009 1 0 011
10079 1011
1011
NULL
1001 1 009
15

37 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping

If list is not empty then we need to traverse the list to find the last node in order to link the newly crated node in existing list.
Algorithm: INSERT_END(Head, V)
Input: head node and data to be inserted
Output: list with new node inserted
Steps:
Start:
1. newestNode = new Node(V) //create new node
2. If Head==NULL // list is empty
3. Head=newestNode
4. Else //list is not empty. Search last node
5. Set ptr=Head
6. While(ptr.next!= NULL)
7. ptr=ptr.next
8. End While // loop will terminate when last node is found
9. ptr.next=newestNode // update the next pointer of ptr(last node)
10. newestNode.prev=ptr
11.End
If End

38 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Middle www.foxitsoftware.com/shopping

Let say we want to insert 13 in following list, at location 2.


1009 1001 1099
head NULL 10 079 1001 1099 11
1009 10 011
NULL
New Node 1019
NULL 13 NULL
Linking
1009 1001 1099
head NULL 1019 11
1009 1 0 011 1099
NULL
10079
1019
1009 13
1001
39 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Middle www.foxitsoftware.com/shopping

In case of doubly linked list, we only need to keep one pointer that points
to current node. Carefully read the statements. One for each link.
1009 1001 1099
head NULL
NULL 1 0 079 1001 1009 1 0 011 1099 11
(Curr.prev).next=newestNode
curr
newestNode.prev=Curr.prev
newestNode.next=Curr
Curr.prev=newestNode
1009 1001 1099
head 11
NULL 1 0 079 1019 1009 1 0 011 1099
NULL

1019
1009 13
1001
40 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insert at Middle www.foxitsoftware.com/shopping

Algorithm: INSERT_MIDDLE(Head, loc, newestNode)


Input: head node, new node and loc of new node 8. Set nodeCount=0
Output: list with new node inserted 9. While (Curr != NULL)
10. nodeCount=nodeCount+1
Steps:
11. If nodeCount ==Loc
Start 12. (Curr.prev).next=newestNode
1. If Head==NULL // list is empty 13. newestNode.prev=Curr.prev
2. Head=newestNode 14. newestNode.next=Curr
3. Else If Loc==1 // insertion at start 15. Curr.prev=newestNode
16. Break
4. newestNode.next=Head
17. End If
5. Head.prev=newestNode 18. Curr=Curr.next
6. Head=newestNode 19. End While
7. Else 20. End If
End
6. Set Curr=Head
7.
41 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion at Start www.foxitsoftware.com/shopping

If this is the last node? Else?


1009 1009 1001
head NULL 7 NULL head NULL 1001 NULL
1009 10 011
10079

Update head Update head


Delete node
Update prev link of next node
Delete node
head NULL 1001
head NULL 10 011 NULL

42 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion at End www.foxitsoftware.com/shopping

If this is the last node? Else?


1009 1009 1001
head NULL 7 NULL head NULL 1001
1009 1 0 011
10079 NULL
Update head Update next link of prev node
Delete node
Delete node
1009
head NULL
head NULL 7 NULL

43 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping

Algorithm: DELETE_END(Head)
Algorithm: Input: reference to first node
DELETE_START(Head) Output: new list with node deleted
Steps:
Input: reference to first node Start:
If Head!=NULL// list is not empty
Output: new list with node deleted 1.
2. If Head.next==NULL// there is only one node
Steps: 3. Head=NULL
Else
Start
4.

5. Set Curr=Head
1. If Head!=NULL// list is not 6. While (Curr.next!=NULL)
7. Curr=Curr.next
empty 8. End While
2. Head=Head.next 9. (Curr.prev).next=NULL
10. Curr.prev=NULL
3. Head.prev=NULL 11. End If
End If
4. End If
12.

End End
//if you are maintaining tail reference, then it must be updated
during deletion

44 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion at Location www.foxitsoftware.com/shopping

Let say we need to delete 2nd node.

1009 1001 1099


head NULL 10 079 1001 1099 1001 11
1009 1 0 011
NULL

curr

1009 1099
head NULL 1099 1009 11
NULL
10079

45 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping

Algorithm: DELETE_LOCATION(Head, Loc)


Input: reference to first node and loc
Output: new list with node deleted 1. While (Curr!=NULL)
Steps: 2. nodeCount=nodeCount+1
3. If (nodeCount==Loc)
Start:
4. (Curr.prev).next=Curr.next
1. Set nodeCount=0
5. (Curr.next).prev=Curr.prev
2. If Head!=NULL // list is not empty 6. break
3. If Loc==1 // delete at start 7. End If
4. Head=Head.next 8. Curr=Curr.next
5. Head.prev=NULL 9. End While
6. Else 10. End If
7. Set Curr=Head 11. End If

46 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Circular Linked List-Single www.foxitsoftware.com/shopping

Every node contains only one next link which points to next node in
list. 1009 1001 1099
head 9 1001 7 1099 11 1009

Last nodes points to First node of list


What change needs to be done in singly linked list algorithms?
How to know that node is last node in list?

47 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Circular Linked List-Double www.foxitsoftware.com/shopping

Doubly linked list, with last node pointing to first node and first node
pointing to last node.
1009 1001 1011
head 1011 1001
1007 1009 1 0 011 1001 100195 1009
9 1011

Previous link of first node is Last node


Next link of last node is first node

48 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Circular Linked List www.foxitsoftware.com/shopping

What change will be required in following algorithms of both single


and double linked list:
Insert
Delete
Search

When
loop
will
termina
te?

49 25/10/2020
INSERTION IN AN EMPTY LIST

static Node addToEmpty(Node last, int data)


{
// This function is only for empty list
if (last != null)
return last;

// Creating a node dynamically.


Node temp = new Node();

// Assigning the data.


temp.data = data;
last = temp;
// Note : list was empty. We link single
node
// to itself.
temp.next = last;

return last;
}
INSERTION AT THE BEGINNING OF THE LIST

static Node addBegin(Node last, int data)


{
if (last == null)
return addToEmpty(last, data); To insert a node at the beginning of the list, follow
these steps:
// Creating a node dynamically
• Create a node, say T
Node temp = new Node();
• Make T -> next = last -> next
// Assigning the data • last -> next = T
temp.data = data;

// Adjusting the links


temp.next = last.next;
last.next = temp;

return last;
}
INSERTION AT THE END OF THE LIST

static Node addEnd(Node last, int data)


{
if (last == null)
return addToEmpty(last, data); To insert a node at the end of the list, follow these
steps:
// Creating a node dynamically. • Create a node, say T
Node temp = new Node(); • Make T -> next = last -> next
• last -> next = T
// Assigning the data.
• last = T
temp.data = data;

// Adjusting the links.


temp.next = last.next;
last.next = temp;
last = temp;

return last;
}
static Node addAfter(Node last, int data, int item)
{
if (last == null)
return null;

Node temp, p;
p = last.next;
do { To insert a node in between the two nodes, follow these steps:
if (p.data == item) { • Create a node, say T.
temp = new Node(); • Search for the node after which T needs to be inserted, say that
temp.data = data; node is P.
temp.next = p.next; • Make T -> next = P -> next;
p.next = temp; • P -> next = T.
if (p == last)
last = temp;
return last;
}
p = p.next;
} while (p != last.next);

System.out.println(item + " not present in the


list.");
return last;
}
Tail Node
🞂​ Whenever we do insertion/deletion to end of list, we need to search for last
node, it involves loop
🞂​ Can we avoid this loop?
🞂​ By maintaining a reference to last node just like we do for first node.
🞂​ It will save time
🞂​ How?

1009 1001
head data 1001 data NULL tail

54 CIIT Lahore 01/10/2015


Tail Node
🞂​ Whenever we do insertion/deletion to end of list, we need to search for last
node, it involves loop
🞂​ Can we avoid this loop?
🞂​ By maintaining a reference to last node just like we do for first node.
🞂​ Will save time?
🞂​ Let Say Tail points to last node
🞂​ Insertion at End needs last node 1009 1001
🞂​ Tail.next=newestNode; head NULL tail
🞂​ Tail=NewestNode data 1001 data

🞂​ If double linked list, then set prev link of new node


🞂​ Deletion at End needs 2nd last node
🞂​ In single linked list: No benefit, we always need to search for 2nd last
node
🞂​ In Double: we can go to previous node of double linked list.
55 CIIT Lahore 01/10/2015
Modified Insert_End –Single Linked List
🞂​ Algorithm: INSERT_END(Head, Tail, V)
🞂​ Input: head node and data to be
inserted
🞂​ Output: list with new node inserted
🞂​ Steps:
Start:newestNode
1. = new Node(V) //create new node
2. If Head==NULL // list is empty
3. Head=newestNode
4. Tail=newestNode
5. Else //list is not empty. Search last node
6. Tail.next=newestNode // update the next pointer of ptr(last
7. Tail=newestNode; node)
8.End
If End

56 CIIT Lahore 01/10/2015


Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Array vs. Linked List www.foxitsoftware.com/shopping

Memory allocation
Static vs. dynamic
Contiguous vs linked
Space utilization
Array is fixed whereas linked list can grow/shrink
Single node vs single cell

58 CIIT Lahore 25/10/2020


Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Applications of Linked List www.foxitsoftware.com/shopping

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

59 01/10/2015
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Practice Problems www.foxitsoftware.com/shopping

Sorting linked list


Remove duplicates
Sorted vs unsorted
Move Node
Given two lists, remove node from one list and push it to start of other
list
Sorted Merge
Merge two sorted lists into a new sorted list
Sorted Insert
List will remain sorted after insertion
Reversing list
By rearranging nodes
Copying
Shallow vs. deep copy
Concatenating
Append one list to other list’s end
60 01/10/2015

You might also like