0% found this document useful (0 votes)
5 views

Week6 LinkedList

The document provides an overview of linked lists, a linear data structure consisting of nodes connected by pointers, with each node containing data and a link to the next node. It explains how to insert and delete nodes at both the beginning and end of the list, as well as how to traverse, search, and update nodes within a linked list. The document also includes example code snippets for various operations on linked lists.

Uploaded by

logify99
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)
5 views

Week6 LinkedList

The document provides an overview of linked lists, a linear data structure consisting of nodes connected by pointers, with each node containing data and a link to the next node. It explains how to insert and delete nodes at both the beginning and end of the list, as well as how to traverse, search, and update nodes within a linked list. The document also includes example code snippets for various operations on linked lists.

Uploaded by

logify99
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/ 45

Linked List

Department of Computer Science and Software


Engineering
Capital University of Sciences & Technology,
Islamabad Pakistan
List using arrays

 Expensive for insertion.


 Expensive for deletion.
Linked List Basics

 Linked list is a linear data structure which is made up of


nodes connected together by pointers.

 Each node has two main parts :


 Data - contains the data/value to be stored
 Link – contains address of the next node.

 Each Node in a Linked List is created Dynamically.


Example

 Storing 1,2, 3 in a LinkedList


Example

 Storing 1,2, 3 in a LinkedList


 A head or start pointer is used to keep track of the starting of
linked list.
Linked List
Basics
Linked list insert node

 Linked list insert node


 Beginning
 End
Beginning:

 Create a new Node.


 Make link part of the newly created node equal to head
pointer & adjust the head pointer to point to the newly created
node.
Insert at the End of LinkedList

 Create a new Node.


 Traverse the Linked list to reach the last Node & point
the link part of the last Node to newly created Node.
 Make the link part of newly created Node equal to NULL.
 Special Case: If list is empty, create a new node and
point head pointer to it.
Linked list Delete node

 We can delete a node at the beginning or at the end of the


linked list.

 Firstly check if the list is empty ot not.


 Linked list delete node
 Beginning
 End
Beginning:

 Create a temporary pointer (ptr) & equate it to head to point


to the first node.
 Make the head pointer equal to link part of the first node.
 Delete the first node using ptr pointer
End:
 Create a temporary pointer (ptr)
 Check if link part of first node is NULL i.e. Only one node is
present. Delete the node and make head equal to NULL.
 If more than one nodes, Traverse to the last node using
pointer ptr & keep track of second last node using pointer
prev.
 Make the link part of second last node as NULL using prev &
delete last node using ptr
Linked List Traversal
 The idea here is to step through the list from beginning to
end. For example, we may want to print the list.
 Start with the head of the list. Access the content of the head
node if it is not null.
 Then go to the next node(if exists) and access the node
information
 Continue until no more nodes (that is, you have reached the
null node)
Linked List Traversal
 //Function to display linked list
 void dispLink()
 {
 Node *temp=head;
 while(temp!=NULL)
 {
 cout<<temp->data<<" ";
 temp=temp->link;
 }
 cout<<"\n";
 }
Linked List node Searching
 To search any value in the linked list, we can
traverse the linked list and compares the value
present in the node.
Linked List node Searching
 bool searchLL(Node head, int val)
 {
 Node temp = head // creating a temp variable pointing to the head of the
linked list
 while( temp != NULL) // traversing the list
 {
 if( temp.data == val )
 return true
 temp = temp.next
 }
 return false
 }
Linked List node Updation

 To update the value of the node, we just need to set the data
part to the new value.
 Below is the implementation in which we had to update the
value of the first node in which data is equal to val and we
have to set it to newVal.
Linked List node Updation
 void updateLL(Node head, int val, int newVal)
 {
 Node temp = head
 while(temp != NULL)
 {
 if( temp.data == val)
 {
 temp.data = newVal
 return
 }
 temp = temp.next
 }
 }

You might also like