0% found this document useful (0 votes)
4 views

Linked List

The document provides an overview of linked lists, explaining their structure, operations, and types including singly, doubly, circular, and circular doubly linked lists. It details how to create and traverse a singly linked list using C programming, including memory allocation and node linking. Additionally, it covers insertion and deletion operations at various positions within the list.
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)
4 views

Linked List

The document provides an overview of linked lists, explaining their structure, operations, and types including singly, doubly, circular, and circular doubly linked lists. It details how to create and traverse a singly linked list using C programming, including memory allocation and node linking. Additionally, it covers insertion and deletion operations at various positions within the list.
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/ 38

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?
1. The first step of creating a linked list of n nodes starts from
defining a node structure.

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

 Here, data is the data we want to store in list.


 *next is a pointer to the same structure type.

 The data part data contains an integer value and the next pointer
stores the address of the next node (if exists, otherwise NULL).
HOW TO CREATE A LINKED LIST?
2. Declare a pointer to the node type variable to store the link of
the first node of the linked list. Say,

struct node *head;

Note: You can also declare variable of node type along with the node
structure definition.

3. Input the number of nodes to create from the user, and store it
in some variable say n.

4. Declare two more helper variables of the node type, say


struct node *newNode, *temp;
HOW TO CREATE A LINKED LIST?
5. If n > 0 then, create our first node i.e. head node. Use dynamic
memory allocation to allocate memory for a node. Say
head = (struct node*)malloc(sizeof(struct node));

6. If there is no memory to allocate for the head node i.e.


head == NULL
then print some error message and terminate the program,
otherwise, move to the below step.

7. Input data from the user and assign it to head using


head->data = data;

8. Initially head node points to NULL. Hence, assign:


head->next = NULL;
HOW TO CREATE A LINKED LIST?
9. Creating other nodes: Copy the reference of head to some other
temporary variable, say
temp = head;
We will use temp to store the reference of the previous node.

10. Allocate memory and assign memory reference to newNode, say


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

11. If memory got allocated successfully, then read data from the
user and assign to the data section of the new node. Say
newNode->data = data;
12. Make sure the new node points to NULL.
13. Now link the previous node with the newly created node i.e.
temp->next = newNode;
HOW TO CREATE A LINKED LIST?
14. Make the current node as the previous node using
temp = temp->next;
15. Repeat steps 10-14 for the remaining (n – 2) other nodes.

How to traverse a linked list?


1. Create a temporary variable for traversing. Assign reference of
the head node to it, say temp = head.
2. Repeat the below steps till temp != NULL.
3. temp->data contains the current node data. We can print it or
perform some calculations on it.
4. Once done, move to the next node using
temp = temp->next;
5. Go back to 2nd step.
PROGRAM TO CREATE AND TRAVERSE A
LINKED LIST
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}
struct node *head; //or simply write *head;

/* Functions to create and display list */


void createList(int n);
void traverseList();

int main()
{
int n;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
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
DELETE FROM END

Head Node

100

2 200 4 300 6 NULL

100 200 300

Head Node

100

2 200 4 NULL

100 200
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

You might also like