The document discusses different types of linked lists including singly linked lists, doubly linked lists, and circular linked lists. It provides examples of how to implement these linked lists in C by defining node structures containing data fields and pointer fields. The document also demonstrates how to perform operations on linked lists such as inserting nodes, deleting nodes, and finding the middle node.
1. L I N K E D L IST
(Download file to view animation)
Dr. S. Lovelyn Rose
PSG College of Technology
Coimbatore, India
2. SINGLY LINKED LIST
DATA ADDRESS
Data 1 Data 2 . . . . . . Data n ADDRESS
DOUBLY LINKED LIST
ID Name Desig Address
Start
of Node2
ADDRESS Data 1 Data 2 . . . . . Data n ADDRESS
Start NULL Data 1 Address of
ID Name Desig Address of
Node 2
Node 3
Previous node Data part Next node
Address of Data 2 Address of
NodeID1 Name Desig NULL 3
Node
Address of Data 3
Node 2 NULL
3. CIRCULAR LINKED LIST
SINGLY
Start
Data 1 Node 2 Data 2 Node 3 Data 3 Node1
Node 1 Node 2 Node 3
DOUBLY
Start
Node3 Data 1 Node 2 Node 1 Data 1 Node 3
Node 1 Node 2 Node 2 Data 1 Node1
Node 3
4. struct node
{
int x;
x c[10] next
char c[10];
struct node *next;
}*current;
6. Create a linked list for maintaining the employee details
such as Ename,Eid,Edesig.
Steps:
1)Identify the node structure struct node
{
2)Allocate space for the node
int Eid;
3)Insert the node in the list char Ename[10],Edesig[10];
struct node *next;
} *current;
EID EName EDesig next
7. Number of nodes
Allocation
Insert_N_Nodes(N)
and
{ Assignment
Start=current=create_node();
Start,
current
011 ABI HR NULL
8. for(i=1;i<n-1;i++)
{
current->next=create_node();
current=current->next;
} }
Start,
current
011 ABI HR
i=1 012 Banu DBA NULL
10. Start
20 30
Insert_a_node(Start)
{ Start
current= create_node(); 10
current->next=Start; 20
Start=current;
} current 30
10
Note : Passing Start as a parameter implies the list already exists
11. Start
10 20 30 40
25
Steps:
1)Have a pointer (slow) to the node after which the new
node is to be inserted
2)Create the new node to be inserted and store the address
in ‘current’
3) Adjust the pointers of ‘slow’ and ‘current’
12. Use two pointers namely, fast and slow
Move the ‘slow’ pointer by one element in the list
For every single movement of ‘slow’, move ‘fast’ by 2
elements
When ‘fast’ points to the last element, ‘slow’ would be
pointing to the middle element
This required moving through the list only once
So the middle element is found in a time complexity of
O(n)
13. {
current=(struct node *)malloc(sizeof(struct node))
fast=slow=Start Start 10
do
fast slow
{
if(fast->next->next!=null) 20
fast=fast->next->next current
else break;
slow=slow->next 25 30
}while(fast->next!=null)
current->next=slow->next
slow->next=current 40
}
Note : fast and slow are below the node they point to