0% found this document useful (0 votes)
7 views8 pages

Linked List

A linked list is a collection of nodes that are not stored contiguously in memory, where each node contains data and a pointer to the next node. Operations on a singly linked list include insertion (at the beginning, end, or specific location) and deletion (from the beginning, end, or specific node). A doubly linked list includes pointers to both the previous and next nodes, and a circular doubly linked list connects the last node back to the head node.

Uploaded by

valvi kuldip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Linked List

A linked list is a collection of nodes that are not stored contiguously in memory, where each node contains data and a pointer to the next node. Operations on a singly linked list include insertion (at the beginning, end, or specific location) and deletion (from the beginning, end, or specific node). A doubly linked list includes pointers to both the previous and next nodes, and a circular doubly linked list connects the last node back to the head node.

Uploaded by

valvi kuldip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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 i.e. 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.

Uses of Linked List


o The list is not required to be contiguously present in the memory. The node can
reside any where in the memory and linked together to make a list. This achieves
optimized utilization of space.
o list size is limited to the memory size and doesn't need to be declared in advance.
o Empty node can not be present in the linked list.
o We can store values of primitive types or objects in the singly linked list.

Operations on Single Linked List


The following operations are performed on a Single Linked List

 Insertion
 Deletion
 Display

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
Inserting At Beginning of the list
Inserting an element at the beginning of a linked list.

1
2 **Algorithm to insert a new node at the beginning**
3 Step 1: IF AVAIL = NULL
Write OVERFLOW
4 Go to Step 7
5 [END OF IF]
6 Step 2: SET NEW_NODE = AVAIL
7 Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET NEW_NODE-> DATA = VAL
8 Step 5: SET NEW_NODE-> NEXT = START
9 Step 6: SET START = NEW_NODE
10 Step 7: EXIT
11

3.2.) Inserting an element at the end of a linked list :-


Algorithm to insert a new node at the end
1
2 **Algorithm to insert a new node at the end**
3 Step 1: IF AVAIL = NULL
4 Write OVERFLOW
5 Go to Step 1
[END OF IF]
6 Step 2: SET NEW_NODE = AVAIL
7 Step 3: SET AVAIL = AVAIL->NEXT
8 Step 4: SET NEW_NODE -> DATA = VAL
9 Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET PTR = START
10 Step 7: Repeat Step 8 while PTR->NEXT != NULL
11 Step 8: SET PTR = PTR -> NEXT
12 [END OF LOOP]
13 Step 9: SET PTR -> NEXT = NEW_NODE
14 Step 10: EXIT
15
3.3.) Inserting an element before a given node in a linked list :-

Inserting an element before a given node in a linked list


1
2 **Algorithm to insert a new node before a node that has value NUM**
3 Step 1: IF AVAIL = NULL
4 Write OVERFLOW
5 Go to Step 12
6 [END OF IF]
Step 2: SET NEW_NODE = AVAIL
7 Step 3: SET AVAIL = AVAIL -> NEXT
8 Step 4: SET NEW_NODE -> DATA = VAL
9 Step 5: SET PTR = START
10 Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PTR -> DATA != NUM
11 Step 8: SET PREPTR = PTR
12 Step 9: SET PTR = PTR -> NEXT
13 [END OF LOOP]
14 Step 10: PREPTR -> NEXT = NEW_NODE
15 Step 11: SET NEW_NODE -> NEXT = PTR
Step 12: EXIT
16
17
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

4.1.) Deleting the first node of a linked list :-

Deleting the first node of a linked list


1
**Algorithm to delete the first node**
2
Step 1: IF START = NULL
3 Write UNDERFLOW
4 Go to Step 5
5 [END OF IF]
6 Step 2: SET PTR = START
Step 3: SET START = START -> NEXT
7 Step 4: FREE PTR
8 Step 5: EXIT
9

4.2.) Deleting the Last Node from a Linked List :-

Deleting the last node of a linked list


1 **Algorithm to delete the last node**
2 Step 1: IF START = NULL
Write UNDERFLOW
3 Go to Step 8
4 [END OF IF]
5 Step 2: SET PTR = START
6 Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != NULL
7 Step 4: SET PREPTR = PTR
8 Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
9 Step 6: SET PREPTR -> NEXT = NULL
10 Step 7: FREE PTR
11 Step 8: EXIT
12
13

4. 4.3.) Deleting the Node After a Given Node in a Linked List :-

5.
Deleting the node after a given node in a linked list
1
2 **Algorithm to delete the node after a given node**
3 Step 1: IF START = NULL
4 Write UNDERFLOW
Go to Step 1
5
[END OF IF]
6 Step 2: SET PTR = START
7 Step 3: SET PREPTR = PTR
8 Step 4: Repeat Steps 5 and 6 while PREPTR->DATA != NUM
9 Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
10 [END OF LOOP]
11 Step 7: SET TEMP = PTR
12 Step 8: SET PREPTR->NEXT = PTR->NEXT
13 Step 9: FREE TEMP
14 Step 10: EXIT
15
Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a pointer to
the previous as well as the next node in the sequence. Therefore, in a doubly linked list,
a node consists of three parts: node data, 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.

A doubly linked list containing three nodes having numbers from 1 to 3 in their data
part, is shown in the following image.

In C, structure of a node in doubly linked list can be given as :

1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. }

The prev part of the first node and the next part of the last node will always contain null
indicating end in each direction.

Circular Doubly Linked List in C?


It is a kind of doubly linked list in which the node of the list is pointing to the head node and
the previous pointer of the head node points to the last node. It makes doing many
operations in constant time and sorting data is also possible.

Example:
Here is an example of the circular doubly linked list.

You might also like