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

Singly Linked List

1) The document discusses singly linked lists, including their representation, creation, traversal, insertion, search, deletion, and summary. 2) A singly linked list contains nodes where each node has a data field and a pointer to the next node. It allows traversal in only one direction from the first node. 3) Key operations on singly linked lists include traversing the list, inserting and deleting nodes at the beginning, end, or a specified position through adjustments to pointers between nodes.

Uploaded by

Jatin Dadlani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Singly Linked List

1) The document discusses singly linked lists, including their representation, creation, traversal, insertion, search, deletion, and summary. 2) A singly linked list contains nodes where each node has a data field and a pointer to the next node. It allows traversal in only one direction from the first node. 3) Key operations on singly linked lists include traversing the list, inserting and deleting nodes at the beginning, end, or a specified position through adjustments to pointers between nodes.

Uploaded by

Jatin Dadlani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1

DATA STRUCTURE
Topic Name-
Singly Linked List Presenter Name
Yash Malviya
6.12.2022 2

SINGLY LINKED LIST

CONTENT
Introduction
Representation
Creation
Traversal
Insertion
Search
Deletion
Summary
SINGLY LINKED LIST

Introduction
What is a linked list?
 A linked list is a linear data structure, in which the
elements are not stored at contiguous memory
locations. 

 In simple words, a linked list consists of nodes where


each node contains a data field and a reference(link) to
the next node in the list.

6.12.2022 3
4

TOPIC ONE
Singly Linked List
Singly Linked List
What is a singly linked list?

A Singly Linked List is a specialized case of


a generic linked list. In a singly linked list,
each node links to only the next node in the
sequence, i.e. if we start traversing from the
first node of the list, we can only move in one
direction(pun intended).

5
Representation Of Linked List
1. Static , Sequential or Array Representation.
 The linked list will maintained or represented by two parallel arrays of same size.

 One array for the actual data item is called info field of the node.
 Other array for the address called the next field of the nodes.

6
Representation of Linked List 2

2. Dynamic or Pointers or Linked Representation

The size of the linked list may increase or decrease according to


the requirement of application so dynamic representation of linked list
do not have limitation and use space proportional to the actual number
of element of the list.
Features:
It want ultimate size of the linked list to declare in advance.
Array implementation of the linked list is very simple and efficient in
random access of the elements.

7
OPERATION ON SINGLY LINKED LIST
Creating Singly Linked List:

1.Create a class Node which has two attributes: data and next. Next is a pointer to the next node.
2.Create another class which has two attributes: head and tail.
3.Add Node() will add a new node to the list:
 Create a new node.
 It first checks, whether the head is equal to null which means the list is empty.
 If the list is empty, both head and tail will point to the newly added node.
 If the list is not empty, the new node will be added to end of the list such that tail's next will point to the newly
added node. This new node will become the new tail of the list.
4.display() will display the nodes present in the list:
 Define a node current which initially points to the head of the list.
 Traverse through the list till current points to null.
 Display each node by making current to point to node next to it in each iteration.

9/3/20XX 8
Traversal of Singly Linked List
• Traversing is the most common operation that is performed in almost
every scenario of singly linked list. Traversing means visiting each
node of the list once in order to perform some operation on that. This
will be done by using the following statements.
ptr = head;   
        while (ptr!=NULL)  
            {  
                 ptr = ptr -> next;  
          }  

9
Insertion
Insertion of an element into the linked list at various position :
Insertion of a node into a linked list require a free node in which the
information can be inserted and then the node can be inserted and then
the node can inserted into a linked list.
We have three types of insertion in a linked list:
Before header or at beginning.
At the end of list.
At the specified position.
Insertion.2

Insertion at beginning in singly linked list.


• Inserting a new element into a singly linked list at beginning is quite simple. We just need to make a few
adjustments in the node links. There are the following steps which need to be followed in order to insert a
new node in the list at beginning.
• Allocate the space for the new node and store data into the data part of the node. This will be done by the
following statements.
1. ptr = (struct node *) malloc(sizeof(struct node *));  
2.             ptr → data = item   
• Make the link part of the new node pointing to the existing first node of the list. This will be done by
using the following statement.
1. ptr->next = head;  
• At the last, we need to make the new node as the first node of the list this will be done by using the
following statement.
1. head = ptr;  
Algorithm For insertion at beginning in singly linked
list.
• Step 1: IF PTR = NULL
• Write OVERFLOW
• Go to step 7
• [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 = HEAD
• Step 6: SET HEAD = NEW_NODE
• Step 7: EXIT
   
12
Insertion at End
• 1. Declare head pointer and make it as NULL.

2. Create a new node with the given data. And make the new node => next as
NULL.
    (Because the new node is going to be the last node.)

3. If the head node is NULL (Empty Linked List),
•          make the new node as the head.

4. If the head node is not null, (Linked list already has some elements),
•          find the last node.
•          make the last node => next as the new node.
13
Algorithm
• Step 1: IF PTR = NULL Write OVERFLOW
    Go to Step 1

   [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 PTR = HEAD
• Step 7: Repeat Step 8 while PTR - > NEXT != NULL
• Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
• Step 9: SET PTR - > NEXT = NEW_NODE
• Step 10: EXIT

14
Insertion at Specified Position
• To insert a given data at a specified position, the below algorithm is to
be followed:  
• Traverse the Linked list up to position-1 nodes.
• Once all the position-1 nodes are traversed, allocate memory and the
given data to the new node.
• Point the next pointer of the new node to the next of current node.
• Point the next pointer of current node to the new node.

15
Algorithm
• STEP 1: IF PTR = NULL
• WRITE OVERFLOW
    GOTO STEP 12
   END OF IF
• STEP 2: SET NEW_NODE = PTR
• STEP 3: NEW_NODE → DATA = VAL
• STEP 4: SET TEMP = HEAD
• STEP 5: SET I = 0
• STEP 6: REPEAT STEP 5 AND 6 UNTIL I
• STEP 7: TEMP = TEMP → NEXT
• STEP 8: IF TEMP = NULL
• WRITE "DESIRED NODE NOT PRESENT"
     GOTO STEP 12
    END OF IF
 END OF LOOP

16
Deletion of an element in singly liked list

• If the node be deleted, that element should be search all over the list
still the node find, Then it should be deleted.

As like insertion the deletion is also performed in three ways


Deletion at the beginning position of the linked list.
Deletion at the ending position of linked list.
Deletion at a specified position in linked list.

17
Deletion At Beginning
• Deleting a node from the beginning of the list is the simplest operation of all. It just
need a few adjustments in the node pointers. 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.
1.ptr = head;  
2.            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.
1.free(ptr)  

18
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

19
Deletion at End
• Steps required for deleting the node:-
• If the Linked list has only one node then make head node null
• Else traverse to the end of the linked list
• While traversing store the previous node i.e. 2nd last node
• Change the next of 2nd last node to null
• Free/delete memory of the the last node
• Now, 2nd last node becomes the last node.

20
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

21
Deletion at specific position
• If the head is NULL, return.
• Create a node temp and make it point to the head.
• If the position is 0, it means that we have to delete the head of the list. We can easily do that by head =
temp → next and free(temp). As temp was pointing to the head, we have incremented the head and
freed temp.
• If the position is not 0, we will proceed. Now, we will write a loop with i = 0 to i < (position -1). As we
have discussed, to delete a node, we need a pointer to its previous node. So while traversing the loop,
we will increment temp.
• Now, after the loop ends, if the temp is NULL or temp → next is NULL, we will simply return as the
position is more than the number of nodes in the linked list.
• If the above condition fails, we will have temp pointing to the (position – 1)th node. Now, we simply
have to change the required pointers.
• We will save the temp → next → next in a new node next1. Now, we will free temp → next, as it is the
node at the given position. In the end, temp → next = next1. – In this way, there is no unnecessary
memory leak and the node at the given position is getting deleted successfully.

22
Algorithm
• STEP 1: IF HEAD = NULL
• WRITE UNDERFLOW
    GOTO STEP 10
   END OF IF
• 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
• STEP 8: I = I+1
• END OF LOOP
• STEP 9: TEMP1 → NEXT = TEMP → NEXT
• STEP 10: FREE TEMP
• STEP 11: EXIT 23
Summary

SINGLY LINKED LIST


In this Presentation we have learned about linked lists and singly linked list. There
function
And there usage. We have learned how to create a linked list and how to insert elements
at
Different Part of a linked list such as at beginning , At end or at Specific Position. Also
we have learned How to delete a note at three position At Beginning, At End or at
Specified Position……..

24
6.12.2022 25

SINGLY LINKED LIST

THANK YOU

You might also like