0% found this document useful (0 votes)
71 views71 pages

Unit Iii - DS - PPT

Here are the key steps to insert a node in the middle of a singly linked list: 1. Create a new node with the data to be inserted 2. Traverse the list until the node before the position where you want to insert the new node 3. Set the next of the new node to the next of the pre location node 4. Make the next of pre location node point to the new node 5. Update head if the new node is inserted at the beginning This allows insertion of a node between two existing nodes in the linked list.

Uploaded by

Rexline S J
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)
71 views71 pages

Unit Iii - DS - PPT

Here are the key steps to insert a node in the middle of a singly linked list: 1. Create a new node with the data to be inserted 2. Traverse the list until the node before the position where you want to insert the new node 3. Set the next of the new node to the next of the pre location node 4. Make the next of pre location node point to the new node 5. Update head if the new node is inserted at the beginning This allows insertion of a node between two existing nodes in the linked list.

Uploaded by

Rexline S J
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/ 71

UNIT III

Linked List
Linked List
• Linked List can be defined as collection of objects
called nodes that are randomly stored in the memory.
• A node contains two fields i.e. data stored at that particular
address and the pointer which contains the address of the
next node in the memory.
• The last node of the list contains pointer to the null.
Why use linked list?

 It allocates the memory dynamically.


 All the nodes of linked list are non-contiguously stored in the
memory and linked together with the help of pointers.
 Sizing is no longer a problem since we do not need to define its
size at the time of declaration.
 List grows as per the program's demand and limited to the
available memory space.
Applications of Linked list

 polynomials can be represented.


 Using linked list, we can implement stack, queue, tree, and
other various data structures.
 Graph can be implemented as a linked list.
Representation of a Linked list

• Linked list can be represented as the connection of nodes in

which each node points to the next node of the list. The

representation of the linked list is shown below -


Representation of a Linked list
• Doubly Linked List is a variation of Linked list in which navigation is possible in
both ways, either forward and backward easily as compared to Single Linked List.
Following are the important terms to understand the concept of doubly linked
list.
• Link − Each link of a linked list can store a data called an element.
• Next − Each link of a linked list contains a link to the next link called Next.
• Prev − Each link of a linked list contains a link to the previous link called Prev.
• LinkedList − A Linked List contains the connection link to the first link called First
and to the last link called Last.
Representation of a Linked list

• Consider an example where the marks obtained by the

student in three subjects are stored in a linked list as shown

in the figure.
Representation of a Linked list

• Consider an example where the marks obtained by the

student in three subjects are stored in a linked list as shown

in the figure.
How to declare a linked list?

• Linked list contains two parts, and both are of different types, i.e., one is the
simple variable, while another is the pointer variable. We can declare the linked
list by using the user-defined data type structure.
• The declaration of linked list is given as follows –
struct node  
{  
int data;  
struct node *next;  
}  

In the above declaration, we have defined a structure named as node that contains

two variables, one is data that is of integer type, and another one is next that is a

pointer which contains the address of next node.


Types of Linked list

Linked list is classified into the following types –

 Singly-linked list

 Doubly linked list

 Circular singly linked list

 Circular doubly linked list


Operations performed on Linked list
The basic operations that are supported by a list are mentioned
as follows –
 Insertion - This operation is performed to add an element into
the list.
 Deletion - It is performed to delete an operation from the list.

 Display/ Traverse - It is performed to display the elements of


the list.
 Search - It is performed to search an element from the list
using the given key.
Singly-linked list
• Singly linked list  can be defined as the collection of ordered set
of elements called nodes.
• A node in the singly linked list consist of two parts: data part
and link part.
• Data part of the node stores actual information that is to be
represented by the node while the link part of the node stores
the address of the next node.
• The structure of the node in the Singly Linked List is
Singly-linked list

The basic operations that are supported by a Singly-linked list are


mentioned as follows –
 Traversing in singly linked list - visiting each node of the list
once
 Insertion - This operation is performed to add an element into
the list.
 Deletion - It is performed to delete an operation from the list.
 Search - It is performed to search an element from the list
using the given key.
 Updating: To update a node.
 Sorting: To arrange nodes in a linked list in a specific order.
 Merging: To merge two linked lists into one.
Traversing in singly linked list
• Traversing means visiting each node of the list once in order to perform
some operation on that.
The algorithm for traversing a 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)
• This will be done by using the following statements.
ptr = head;   
        while (ptr!=NULL)  
            {  
                 print Ptr→ Data
ptr = ptr -> next;  
            }  
Traversing in singly linked list
Algorithm
STEP 1: SET PTR = HEAD
STEP 2: IF PTR = NULL
   WRITE "EMPTY LIST"
  GOTO STEP 7
  END OF IF
STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL
STEP 5: PRINT PTR→ DATA
STEP 6: PTR = PTR → NEXT
[END OF LOOP]
STEP 7: EXIT
Traversing in singly linked list
     
   if(ptr == NULL)    
struct node  
      {      
{           printf("Empty list..");      
     }         
    int data;    else     
     {        
    struct node *next;  
      printf("printing values . . . . .\n"); 
};                 while (ptr!=NULL)      
        {        
void traverse()             printf("\n%d",ptr->data);     
             ptr = ptr -> next;         
    {               }        
  }    
  struct node *ptr;       
   }  
      ptr = head;   

 
singly linked list - Searching

 Searching is performed in order to find the location of a

particular element in the list.

 Searching any element in the list needs traversing through

the list and make the comparison of every element of the list

with the specified element.

 If the element is matched with any of the list element then

the location of the element is returned from the function.


singly linked list - Searching

Algorithm STEP 4: REPEAT STEP 5 TO 7

Step 1: SET PTR = HEAD UNTIL PTR != NULL

Step 2: Set I = 0 STEP 5: if ptr → data = item

  write i+1
STEP 3: IF PTR = NULL
 End of IF
  WRITE "EMPTY LIST"
STEP 6: I = I + 1
  GOTO STEP 8
STEP 7: PTR = PTR → NEXT
  END OF IF
[END OF LOOP]

STEP 8: EXIT
Linked List node Searching
int searchNode(struct node *head, int key)
{
struct node *ptr;

*ptr = head;
while(ptr != NULL)
{
if(ptr ->data == key) return 1;
ptr = ptr ->next;
}
}
Linked List node Searching
Visual Representation – Item found
Linked List : 10  20  30  NULL
Key : 20
Linked List node Searching
Visual Representation – Not Found
Linked List : 10  20  30  NULL
Key : 80
singly linked list - Insertion
The insertion into a singly linked list can be performed at
different positions. Based on the position of the new node being
inserted, the insertion is categorized into the following
categories.
Insertion
Insertion in singly linked list at beginning:

Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then, set

newNode→Data =20 & newNode→next = NULL and head = newNod

Step 4 - If it is Not Empty then, set


newNode→Data =20
newNode→next = head
head = newNode.
Insertion at the beginning
Insertion

struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = 20;

newNode->next = head;

head = newNode;
Insertion at the End

2. Insert at the End

Step 1 - Create a newNode with given value

newNode → Data = 20 and newNode → next = NULL.

Step 2 - Check whether list is Empty (head == NULL).

Step 3 - If it is Empty then, set head = newNode.

Step 4 - If it is Not Empty then, ptr = head


Insertion at the End

Step 5 - Keep moving the ptr to its next node until it reaches to

the last node in the list (until ptr → next is equal to NULL).
while(ptr->next != NULL)
{
ptr = ptr->next;
}

Step 6 - set ptr → next = newNode.


Insertion

struct node *newNode;


newNode = malloc(sizeof(struct node));
newNode->data = 20;
newNode->next = NULL;
struct node *ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newNode;
Insertion
Insertion at the Middle

3. Insert at the Middle

 Allocate memory and store data for new node

 Traverse to node just before the required position of new

node

 Here Prelocation is the node value after which we want to

insert the newNode.

 Finally, Set 'newNode → next = PrePtr→ next' and 'PrePtr

→ next = newNode'
Insertion at the Middle
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 20;
struct node *ptr = head;
preptr = ptr;
for(int i=2; i < =prenodeposition; i++)
{
if(preptr->next != NULL) newNode->next = preptr->next;
{ preptr->next = newNode;
preptr= preptr->next;
Insertion
single linked list _Deletion

In a the deletion operation can be performed in three ways.

They are as follows...

 Deleting from Beginning of the list

 Deleting from End of the list

 Deleting a Specific Node


Deleting from Beginning of the list
Since the first node of the list is to be deleted, therefore, we

just need to make the head, point to the next of the head. This

will be done by using the following statements.

ptr = head;

head = ptr->next;

Now, free the pointer ptr which was pointing to the head node

of the list. This will be done by using the following statement.


Deleting from Beginning of the list
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]

Step 2: SET PTR = HEAD


Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT
Deleting from Beginning of the list
Deleting from End of the list
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]

Step 2: SET PTR = HEAD


Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]

Step 6: SET PREPTR -> NEXT = NULL


Step 7: FREE PTR
Step 8: EXIT
Deleting from End of the list
Deleting from middle of the list

3. Delete from middle


 Traverse to element before the element to be deleted
(prevNode)

prevNode->next = Deletenode->next

Free the memory occupied by the n th node i.e. Delete node.


Deleting from MIDDLE of the list
struct node *ptr = head;
for(int i=2; i < =prenodeposition; i++)
{
preptr = ptr;

ptr= ptr->next;
}
preptr->next = ptr ->next;
Delete ptr;
Deleting from MIDDLE of the list

STEP 2: SET TEMP = HEAD


STEP 3: SET I = 0
STEP 4: REPEAT STEP 5 TO 8 UNTIL I
STEP 5: TEMP1 = TEMP
STEP 6: TEMP = TEMP → NEXT
STEP 7: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
Deleting from Middle of the list

STEP 8: I = I+1
END OF LOOP

STEP 9: TEMP1 → NEXT = TEMP →


NEXT
STEP 10: FREE TEMP
STEP 11: EXIT
Doubly linked list
 Doubly linked list  can be defined as the collection of
ordered set of elements called nodes.
 A node in the Doubly linked list consist of three parts: data
part , pointer to the next node in sequence (next pointer) ,
pointer to the previous node (previous pointer). A sample
node in a doubly linked list is shown in the figure..
Doubly linked list
Doubly linked list
 In C, structure of a node in doubly linked list can be given as :

struct node

struct node *prev;

int data;

struct node *next;

The prev part of the first node and the next part of the last node will
always contain null indicating end in each direction.
Memory Representation of a doubly linked
list
 Memory Representation of a doubly linked list is shown in the following image.
Generally, doubly linked list consumes more space for every node and
therefore, causes more expansive basic operations such as insertion and
deletion. However, we can easily manipulate the elements of the list since the
list maintains pointers in both the directions (forward and backward).
 In the following image, the first element of the list that is i.e. 13 stored at
address 1. The head pointer points to the starting address 1. Since this is the
first element being added to the list therefore the prev of the list contains null.
The next node of the list resides at address 4 therefore the first node contains 4
in its next pointer.
 We can traverse the list in this way until we find any node containing null or -1
in its next part.
Memory Representation of a doubly linked
list
Operations on doubly linked list
SN Operation Description
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after specified node Adding the node into the linked list after the specified
node.
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node having given data Removing the node which is present just after
the node containing the given data.
7 Searching Comparing each node data with the item to be searched and return
the location of the item in the list if the item found else return null.
8 Traversing Visiting each node of the list at least once in order to perform some
specific operation like searching, sorting, display, etc.
Operations on doubly linked list
Operations on doubly linked list
Since, the node being inserted is the first node of the list and therefore it
must contain NULL in its prev pointer. Hence assign null to its previous
part and make the head point to this node.
Algorithm :
Step 1: IF ptr = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]

Step 2: SET NEW_NODE = ptr


Step 3: SET ptr = ptr -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> PREV = NULL
Step 6: SET NEW_NODE -> NEXT = START
Step 7: SET head -> PREV = NEW_NODE
Step 8: SET head = NEW_NODE
Step 9: EXIT
C Function
void insertbeginning(int item)
{

struct node *ptr = (struct node *)malloc(sizeof(struct


node));
if(ptr == NULL)
{
printf("OVERFLOW"); else
} {
else ptr->data=item;
{ ptr->prev=NULL;
ptr->next = head;
if(head==NULL) head->prev=ptr;
{ head=ptr;
ptr->next = NULL; }
ptr->prev=NULL; }
ptr->data=item; }
head=ptr;
}
Insertion in doubly linked list at the end
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]

Step 2: SET NEW_NODE = PTR


Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET TEMP = START
Step 7: Repeat Step 8 while TEMP -> NEXT != NULL
Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]

Step 9: SET TEMP -> NEXT = NEW_NODE


Step 10C: SET NEW_NODE -> PREV = TEMP
Step 11: EXIT
void insertlast(int item)
{

struct node *ptr = (struct node *) malloc(sizeof(struct


node)); else
struct node *temp; {
if(ptr == NULL) temp = head;
{ while(temp->next!
printf("\nOVERFLOW"); =NULL)
{
} temp = temp->next;
else }
{ temp->next = ptr;
ptr->data=item; ptr ->prev=temp;
if(head == NULL) ptr->next = NULL;
{ }
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
Algorithm

STEP 1: IF HEAD = NULL


WRITE UNDERFLOW

GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT

STEP 4: SET HEAD → PREV = NULL

STEP 5: FREE PTR

STEP 6: EXIT
C code

ptr = head;
head = head -> next;

head -> prev = NULL;


free(ptr);

printf("\nNode Deleted\n");
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]

Step 2: SET TEMP = HEAD


Step 3: REPEAT STEP 4 WHILE TEMP->NEXT != NULL
Step 4: SET TEMP = TEMP->NEXT
[END OF LOOP]

Step 5: SET TEMP ->PREV-> NEXT = NULL


Step 6: FREE TEMP
Step 7: EXIT
Singly Linked List as Circular :
 Circular Linked List is a variation of Linked list in which the
first element points to the last element and the last element points
to the first element. Both Singly Linked List and Doubly Linked
List can be made into a circular linked list.
 In singly linked list, the next pointer of the last node points to the
first node.
Doubly Linked List as Circular
In doubly linked list, the next pointer of the last node points to
the first node and the previous pointer of the first node points
to the last node making the circular in both directions.

As per the above illustration, following are the important points to be


considered.
•The last link's next points to the first link of the list in both cases of
singly as well as doubly linked list.
•The first link's previous points to the last of the list in case of doubly
linked list.
Basic Operations

Following are the important operations supported by a

circular list.

insert − Inserts an element at the start of the list.

delete − Deletes an element from the start of the list.

display − Displays the list.


Insertion Operation
Following code demonstrates the insertion operation in a circular linked list based
on single linked list.

else
Example temp := head

insertFirst(data): while next of temp is not head,


do
Begin
temp := next of temp
create a new node done
node -> data := data next of node := head
next of temp := node
if the list is empty, then
head := node
head := node
end if
next of node = head End
Deletion Operation
Following code demonstrates the deletion operation in a circular linked list based on single
inked list.
deleteFirst():
Begin
if head is null, then
it is Underflow and return
else if next of head = head, then
head := null
deallocate head
else
ptr := head
while next of ptr is not head, do
ptr := next of ptr
next of ptr = next of head
deallocate head
head := next of ptr
end if
• Display List Operation
• Following code demonstrates the display list operation in a circular linked list.
• display():
• Begin
• if head is null, then
• Nothing to print and return
• else
• ptr := head
• while next of ptr is not head, do
• display data of ptr
• ptr := next of ptr
• display data of ptr
• end if
• End

You might also like