Linked List

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

LINKED LIST

Definition: A linked list is a data structure which is collection of zero or more nodes where each
node is connected to one or more nodes.

Node= data + link

Data stores the value and link contains the next nodes address.
Representation of Linked List:

o Linked List can be defined as collection of objects called nodes that are randomly stored in
the memory.
o A node contains two fields.
o Data stored at that particular address and the pointer which contains the address of the next
node in the memory.
o The last node of the list contains pointer to the null.

Example

Types of Linked List :

• Singly linked lists

• Doubly linked lists

• Circular singly linked lists

• Circular doubly linked lists

1
Singly Linked List

A singly linked list is a collection of zero or more nodes where each node has two or more fields but
only one link field which contains address of the next node.

Each node in the list can be accessed using the link field which contains address of the next node.

Singly Linked List


• A Singly Linked list is a Linked list which is a collection of objects called nodes that are
randomly stored in the memory.
• A node contains two fields i.e. info(data) stored at that particular address and the
link(pointer) which contains the address of the next node in the memory.
• Data part of the node stores actual information
• Link part of the node stores the address of its immediate successor.
• The last node contains a pointer to the null.
• Singly Linked list can be traversed in one direction.

Example:

• The first node contains the address of the next node, i.e., 200.
• The second node contains the address of the last node, i.e., 300.
• Third node contains the NULL value in its address part as it does not point to any node.
• The pointer that holds the address of the initial node is known as a head pointer.

Representation of the node in a singly linked list

struct node
{
2
int data;
struct node *next;
}

Doubly Linked List


A doubly-linked list is a linear collection of nodes where each node is divided into three parts:
 info – This is a field where the information has to be stored
 llink – This is a pointer field which contains address of the left node or previous node in the list
 rlink – This is a pointer field which contains address of the right node or next node in the list

The pictorial representation of a doubly linked list.

Example

• The node in a doubly-linked list has two address parts;


• One part stores the address of the next while the other part of the node stores the previous
node's address.
• The initial node in the doubly linked list has the NULL value in the address part, which
provides the address of the previous node.

Representation of the node in a doubly linked list

struct node
{
int data;
struct node *next;
struct node *prev;
}

3
Circular linked list

A circular singly linked list is a singly linked list where the link field of last node of the list contains
address of the first node.

Example:

Doubly Circular linked list

The doubly circular linked list has the features of both the circular linked list and doubly linked list.

Operations on Singly List

• Insertion of a node into the list

• Deletion of a node from the list

• Search a node in the list

• Display the contents of the list

Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

4
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the single linked list...

• 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→next = NULL and head = newNode.
• Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.

Inserting At End of the list


We can use the following steps to insert a new node at end of the single linked list...

• Step 1 - Create a newNode with given value and newNode → next as 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, define a node pointer temp and initialize with head.
• Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
• Step 6 - Set temp → next = newNode.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the single linked list...

• 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 → next = NULL and head = newNode.
• Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
• Step 5 - Keep moving the temp to its next node until it reaches to the node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
• Step 6 - Every time check whether temp is reached to last node or not. If it is reached to last
node then display 'Given node is not found in the list!!! Insertion not possible!!!' and
terminate the function. Otherwise move the temp to next node.
• Step 7 - Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the single linked list...

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

5
• Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate
the function.
• Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
• Step 4 - Check whether list is having only one node (temp → next == NULL)
• Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
• Step 6 - If it is FALSE then set head = temp → next, and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the single linked list...

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


• Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate
the function.
• Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
• Step 4 - Check whether list has only one Node (temp1 → next == NULL)
• Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
(Setting Empty list condition)
• Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat
the same until it reaches to the last node in the list. (until temp1 → next == NULL)
• Step 7 - Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the single linked list...

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


• Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate
the function.
• Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
• Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the
last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.
• Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
• Step 6 - If it is reached to the exact node which we want to delete, then check whether list
is having only one node or not
• Step 7 - If list has only one node and that is the node to be deleted, then set head = NULL and
delete temp1 (free(temp1)).
• Step 8 - If list contains multiple nodes, then check whether temp1 is the first node in the list
(temp1 == head).
• Step 9 - If temp1 is the first node then move the head to the next node (head = head →
next) and delete temp1.
• Step 10 - If temp1 is not first node then check whether it is last node in the list (temp1 →
next == NULL).
• Step 11 - If temp1 is last node then set temp2 → next = NULL and
delete temp1 (free(temp1)).
• Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 →
next and delete temp1 (free(temp1)).
6
Displaying a Single Linked List
We can use the following steps to display the elements of a single linked list...

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


• Step 2 - If it is Empty then, display 'List is Empty!!!' and terminate the function.
• Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
• Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the last
node
• Step 5 - Finally display temp → data with arrow pointing to NULL (temp → data ---> NULL).

Searching in singly linked list

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.

Algorithm

o Step 1: SET PTR = HEAD


o Step 2: Set I = 0
o STEP 3: IF PTR = NULL

WRITE "EMPTY LIST"


GOTO STEP 8
END OF IF

o STEP 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL


o STEP 5: if ptr → data = item

write i+1
End of IF

o STEP 6: I = I + 1
o STEP 7: PTR = PTR → NEXT

[END OF LOOP]

o STEP 8: EXIT

Traversing in 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;
7
while (ptr!=NULL)
{
ptr = ptr -> next;
}

Algorithm

o STEP 1: SET PTR = HEAD


o STEP 2: IF PTR = NULL

WRITE "EMPTY LIST"


GOTO STEP 7
END OF IF

o STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL


o STEP 5: PRINT PTR→ DATA
o STEP 6: PTR = PTR → NEXT

[END OF LOOP]

o STEP 7: EXIT

/* Using dynamic variables and pointers Write a C program to construct a singly linked
list.The operations to be supported are
Inserting a node in the front of the list
Deleting the node based on Roll- No
displaying all the nodes in the list */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int rollno;
char name[20];
struct node *ptr;
};
typedef struct node Node;
Node *head,*temp,*temp1,*temp2;
main()
{
int ch;
head=NULL;
do
{
printf("\n 1:linsert\n 2:ldelete\n 3: display\n4:exit\n");
scanf("%d",&ch);
8
switch(ch)
{
case 1: linsert();
break;
case 2: ldelete();
break;
case 3: ldisplay();
break;
case 4: exit(0);
default : printf("wrong choice");
}
}while(ch!=4);
getch();
}
linsert()
{
temp=(Node *) malloc(sizeof(Node));
printf("Enter the roll number\n");
scanf("%d",&temp->rollno);
printf("Enter the name\n");
scanf("%s",temp->name);
if(head==NULL)
{
head=temp;
temp->ptr=NULL;
}
else
{
temp->ptr=head;
head=temp;
}
}

ldelete()
{
int r;
if(head==NULL)
printf("singly linked list is empty\n");
else
{
printf("Enter the rollno which is to be deleted\n");
scanf("%d",&r);
temp1=NULL;
temp=head;
while(temp!=NULL && temp->rollno!=r)
{
temp1=temp;
temp=temp->ptr;
}
9
if(temp==NULL)
printf("A node with rollno=%d is not present in list\n",r);
else if(head->rollno==r)
{
head=head->ptr;
free(temp);
}
else
{
temp2=temp->ptr;
temp1->ptr=temp2;
free(temp);
}
}
}
ldisplay()
{
if(head==NULL)
printf("Singly linked list is empty");
else
{
for(temp1=head;temp1->ptr!=NULL;temp1=temp1->ptr)
{
printf("Rollno=%d\tName=%s\n",temp1->rollno,temp1->name);
}
printf("Rollno=%d\t Name=%s\n",temp1->rollno,temp1->name);
}
}

10

You might also like