Linked List
Linked List
LINKED LIST
This is a special kind of list where data items are linked to one
another.
Each data item has address of its next element.
Data Next
Node
OPERATIONS ON LINKED LIST
Following basic operations are performed on linked list:
Create a new linked list.
Head Node
100
Head node
100
pre data next
NULL 2 200 100 4 300 200 6 NULL
200 300
100
3) Circular Linked List:
Each node has two parts data and next.
Head Node
100
Head node
100
pre data next
300 2 200 100 4 300 200 6 100
100 200 300
HOW TO CREATE A SINGLY
LINKED LIST?
HOW TO CREATE A LINKED LIST?
HOW TO CREATE A LINKED LIST?
HOW TO CREATE A LINKED LIST?
HOW TO CREATE A LINKED LIST?
PROGRAM TO CREATE AND
TRAVERSE A LINKED LIST
createList(n);
return 0;
}
Here data part num contains an integer value and next pointer stores
the address of the next node.
INSERTION AT THE BEGIN
Head Node
100
100
9 NULL
Head Node
100
100 500
100
Head Node
100
Head Node
200
4 300 6 NULL
200 300
1. Copy the address of first node
i.e. head node, to some temp
variable say temp.
Head Node
100
Head Node
100
2 200 4 NULL
100 200
1. Traverse to the last node of the
linked list keeping track of the
second last node in some temp
variable say temp1.
Head Node
100
Head Node
100
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
prevNode = head;
if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print the data
of current node
temp = temp->next; // Move to next node
}
}
}
ALTERNATE PROGRAM: DELETE FROM SPECIFIC POSITION