Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

SINGLY LINKED LIST

Presented By,
M.Sangeetha
Linked list
 linear data structure
 elements are not stored at contiguous memory
locations
elements in a linked list are linked using pointers
TYPES
DEF:linked list consists of nodes where each node
contains a data field and a reference(link) to the next
node in the list.
Linked list comprise of group or list of nodes in which
each node have link to next node to form a chain
Types of linked list
Singly linked list
Doubly linked list
Circular linked list
Linked List vs Array
Array Linked List
data structure that contains a collection non-primitive data structure contains a
of similar type of data elements collection of unordered linked elements
known as nodes
Accessing an element in an array is fast  Accessing an element in an array is bit
slower.
Operations in arrays consume a lot of operations in Linked lists is fast
time
Arrays are of fixed size. Linked lists are dynamic and flexible
and can expand and contract its size.
 In array, memory is assigned during  Linked list it is allocated during
compile time  execution or runtime.
Elements are stored consecutively in Elements are stored randomly in Linked
arrays  lists.
requirement of memory is less due to more memory in Linked Lists due to
actual data being stored within the storage of additional next and previous
index in the array referencing elements.
 memory utilization is inefficient in the memory utilization is efficient in the
array linked list.
Why Linked List
arrays have the following limitations:
The size of the arrays is fixed
To inserting a new element in an array, existing
elements have to be shifted.
Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks of Array
 Random access is not allowed. 
Representation of linked list
represented by a pointer to the first node of the linked
list
The first node is called the head.
If the linked list is empty, then the value of the head is
NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
Linked List Representation
Basic Operations
Insertion − Adds an element at the beginning of the
list.
Deletion − Deletes an element at the beginning of the
list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
CREATE NODE
data link

name
struct node
{
int data;
struct node *next;
}*head,*newNode;
ALGORITHM TO CREATE LIST
Being:
struct node *head;.
alloc (head)
If (head == NULL) then
write ('Unable to allocate memory')
End if
Else then
Head->data =data
Head->next=NULL
End
void createList()
{
struct node *newNode, *temp;
int data, i;
int n;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link data field with data
head->next = NULL; // Link address field to NULL
}
INSERT NODE AT THE BEGINNING OF SINGLY LINKED LIST

Create a new node, say newNode points to the newly


created node
INSERT NODE AT THE BEGINNING OF SINGLY
LINKED LIST
Link the newly created node with the head node, i.e.
the newNode will now point to head node.
INSERT NODE AT THE BEGINNING OF SINGLY
LINKED LIST
Make the new node as the head node, i.e. now head
node will point to newNode
Pseudocode
Being:
createSinglyLinkedList (head)
alloc (newNode)
If (newNode == NULL) then
write ('Unable to allocate memory')
End if
Else then
read (data)
newNode.data ← data
newNode.next ← head
head ← newNode
End else
End
C Program
void insertNodeAtBeginning()
{
Struct node *newNode;
int data;
newNode = (struct node*)malloc(sizeof(struct node));
 
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node : ");
scanf("%d", &data);
newNode->data = data; // Link data part
newNode->next = head; // Link address part
head = newNode; // Make newNode as first node
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
TO INSERT NODE AT THE END OF A SINGLY
LINKED LIST
Create a new node and make sure that the address part
of the new node points to NULL i.e.
newNode ->next=NULL
Contd..
Traverse to the last node of the linked list and connect
the last node of the list with the new node, i.e. last
node will now point to new node. (lastNode->next =
newNode)
Pseudocode
Begin:
createSinglyLinkedList (head)
alloc (newNode)
If (newNode == NULL) then
write ('Unable to allocate memory')
End if
Else then
read (data)
newNode.data ← data
newNode.next ← NULL
temp ← head
While (temp.next != NULL) do
temp ← temp.next
End while
temp.next ← newNode
End else
End
C Program
void insertNodeAtEnd()
{
int data;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node : ");
scanf("%d", &data);
newNode->data = data; // Link the data part
newNode->next = NULL;
temp = head;
// Traverse to the last node
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode; // Link address part
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
INSERT NODE AT THE MIDDLE OF SINGLY
LINKED LIST
Create a new node
Contd..
Traverse to the n-1th position of the linked list and
connect the new node with the n+1th node. Means the
new node should also point to the same node that the
n-1th node is pointing to. (newNode->next = temp-
>next where temp is the n-1th node).
Contd..
Now at last connect the n-1th node with the new node
i.e. the n-1th node will now point to new node. (temp-
>next = newNode where temp is the n-1th node).
Pseudocode
Input : n position to insert data in the list
Begin:
createSinglyLinkedList (head)
alloc (newNode)
If (newNode == NULL) then
write ('Unable to allocate memory.')
End if
Else then
read (data)
newNode.data ← data
temp ← head
For i ← 2 to n-1
temp ← temp.next
If (temp == NULL) then
break
End if
End for
If (temp != NULL) then
newNode.next ← temp.next
temp.next ← newNode
End if
End else
End
C Program
void insertNodeAtMiddle()
{
int i,data,position;
newNode = (struct node*)malloc(sizeof(struct node));
printf("nEnter data to insert at middle of the list: ");
scanf("%d", &data);
printf("Enter the position to insert new node: " );
scanf("%d", &position);
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
C Program Contd..
else
{
newNode->data = data; // Link data part
newNode->next = NULL;
temp = head;
// Traverse to the n-1 position
for(i=2; i<=position-1; i++)
{
temp = temp->next;
if(temp == NULL)
break;
}
if(temp != NULL)
{
/* Link address part of new node */
newNode->next = temp->next;
/* Link address part of n-1 node */
temp->next = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
else
{
printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n");
}
}
}
https://forms.gle/rrLPYTMqcwkitB6z9

You might also like