You are on page 1of 42

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.

 Each data element is known as a node and consists of two


parts:
1) Data part which contains the value
2) Next part which contains the address of the next element.

Data Next

Node
OPERATIONS ON LINKED LIST
 Following basic operations are performed on linked list:
 Create a new linked list.

 Traverse all the data items of the list.

 Insert a new data item in the list at begin, end and


specified position.
 Delete a data item from the list from begin, end and
specified position.
 Search a data item and its memory location in the list.

 Concatenate two linked list.


TYPES OF LINKED LIST
Basically, there are four types of linked list:
1) Singly Linked List:
Each node has two parts: data and next.

 The last node contains NULL value in next part.

A head node has the address of the first node.

Head Node

100

1 200 4 300 8 NULL

100 200 300


2) Doubly Linked List:
Each node has three parts data, pre and next. Here, pre contains
the address of previous node.
Last node contains NULL value in next part.

First node contains NULL value in pre part.

A head node has the address of the first node.

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.

Here, last node contains address of first node in next part.

A head node has the address of the first node.

Head Node

100

1 200 4 300 8 100

100 200 300


4) Circular Doubly Linked List:
Each node has three parts data, pre and next. Here, pre
contains the address of previous node.
Last node contains the address of first node in next part.

First node contains the address of last node in pre part.

A head node has the address of the first node.

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);

printf("\nData in the list \n");


traverseList();

return 0;
}

/* Create a list of n nodes */


void createList(int n)
{
struct node *newNode, *temp;
int data, i;

head = (struct node *)malloc(sizeof(struct node));

// Terminate if memory not allocated


if(head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
// Input data of node from the user
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

// Create n - 1 nodes and add to list


temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data; // Link data field of newNode
newNode->next = NULL; // Make sure new node points to
NULL

temp->next = newNode; // Link previous node with


newNode
temp = temp->next; // Make current node as previous
node
}
}

/* Display entire list */


void traverseList()
{
struct node *temp;

// Return if list is empty


if(head == NULL)
{
printf("List is empty.");
return;
}
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of
current node
temp = temp->next; // Move to next
node
}
}
ALTERNATE PROGRAM: CREATION &
TRAVERSAL

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

2 200 4 300 6 NULL

100 200 300

1 newNode 1. Create a new node, say newNode


2. Link the newly created node with
400 the head node, i.e. the newNode
will now point to the head node.
3. Make the new node as the head
Head Node node, i.e. now head node will
point to newNode.
400

1 100 2 200 4 300 6 NULL

400 100 200 300


INSERTION AT THE END
Head Node

100

2 200 4 300 6 NULL

100 200 300

9 NULL

New Node 500

Head Node

100

2 200 4 300 6 500 9 NULL

100 200 300 500


INSERTION AT SPECIFIED POSITION

Head Node New Node 5 NULL

100 500

2 200 4 300 6 NULL

100 200 300


Head Node

100

2 200 4 500 5 300 6 NULL

100 200 500 300


DELETE FROM BEGIN

Head Node

100

2 200 4 300 6 NULL

100 200 300

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.

2. Move the head to the second


node of the linked list i.e. head
= temp->next.

3. Disconnect the connection of


the first node to the second
node
DELETE FROM END

Head Node

100

2 200 4 300 6 NULL

100 200 300

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.

2. If the last node is the head node


then make the head node as
NULL. else disconnect the second
last node with the last node i.e.
temp1->next = NULL.
3. Free the memory occupied by the
nth node, i.e. temp node
DELETE FROM SPECIFIC POSITION

Head Node

100

2 200 4 300 6 400 8 NULL

100 200 300 400

Head Node

100

2 200 4 400 8 NULL

100 200 400


PROGRAM: DELETE FROM SPECIFIC POSITION

/* Delete the intermediate node of the linked list */

void deleteMiddleNode(int position)


{
int i;
struct node *toDelete, *prevNode;

if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
prevNode = head;

for(i=2; i<=position; i++)


{
prevNode = toDelete;
toDelete = toDelete->next;
if(toDelete == NULL)
break;
}

if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;

prevNode->next = toDelete->next;
toDelete->next = NULL;

/* Delete nth node */


free(toDelete);

printf("SUCCESSFULLY DELETED NODE \n");


}
else
{
printf("Invalid position unable to delete.");
}
}
}
/* Display entire list */

void displayList()
{
struct node *temp;

/* If the list is empty i.e. head = NULL */

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

1. Traverse to the nth node of the


singly linked list and also keep a
reference of n-1th node in some
temp variable, say temp1.

2. Reconnect the n-1th node with the


n+1th node, i.e. temp1->next =
temp->next (Where temp1 is n-1th
node and temp node is the nth
node and temp->next is the n+1th
node).

3. Free the memory occupied by the


nth node, i.e. temp node

You might also like