0% found this document useful (0 votes)
14 views27 pages

Lecture 10 DSA-1

Uploaded by

msaddiqueshareef
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)
14 views27 pages

Lecture 10 DSA-1

Uploaded by

msaddiqueshareef
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/ 27

Linked List

Prepared By: Ms. Saima Safdar


Link List
• Link list is linear data structure that include a series of
connected nodes. Here each node store the data and
address of next node.
Difference
Types:

1. Single linked list

2. Double linked list

3. Circular linked list

4. Circular double linked list


Single link list

• A single linked list is one in which all nodes are


linked together in some sequential manner
Linked List Memory Representation
 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


Doubly Link List
• A doubly linked list is a data structure that consists of a set of
nodes, each of which contains a value and two pointers, one
pointing to the previous node in the list and one pointing to the
next node in the list. This allows for efficient traversal of the list
in both directions, making it suitable for applications where
frequent insertions and deletions are required
Circular Link List
• A circular linked list is one which has no beginning
and no ending. The null pointer in the last node of a
linked list is replaced with the address of its first node
such a list is called single circular linked list.
Doubly Circular Link List
Linked List Operations

•  Traversal
•  Search, print, update etc.
•  Insertion
•  Deletion
Node
 Node can be represented using either structure or
class
 Class is also used in C++, but
C++we will focus on struct only. java
struct Node class Node
{ {
int data; int 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.data

 Accessing the next


node->next node.next
pointer
Node examples
 Node can have individual data members or other objects as well.

class Student{
private String name;
private float gpa;
private Student next;
}

class Book{

private String title;


private Author author;
private Book next;
//methods
}
Search
 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
Search
 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
5. End While node
end
Insertion
 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
Insertion at Start
 If List is Empty List is not
head NULL Empty 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
Insertion at End
 If List is Empty  Non-Empty
head NULL List 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
Insertion at End
 If list is not empty then we need to traverse the list to find the last node in order to link the newly created 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
10. End If node)
End
Insertion at
Middle
 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
Insertion at
Middle
 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
Deletio
n
 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
Deletio
n
 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
DELETE
 Algorithm: DELETE_END(Head)
 Input: reference to first node
 Output: new list with lat node deleted
 Steps:
Start:
1.If Head!=NULL// list is not empty
2. If Head.next==NULL// there is only one node
3. Head=NULL
4. Else
5. Set Curr=Head, Prev=NULL
6. While (Curr.next!=NULL)
7. Prev=Curr
8. Curr=Curr.next
9. End While
10. Prev.next=NULL
11. End If
12.End If
End
Deletio
n
 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
1009 1001 1099
deletion we can link previous node to next node of 2nd node
head 9 1001 7 1099 11
NULL

prev curr
Deletion
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
THE END

You might also like