Linked List
Linked List
DATA STRUCTURES
Unit 1-Linear structures
Year/Sem : II/III
Subject Code: 1151CS102
Topic : Linked list
Faculty Name : Mrs.Vijitha.S
Date : 08.08.2020
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Contents
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
The size of the linked list is not fixed, and data items
can be added at any locations in the list.
and Project
Unlike an array, it is not stored sequentially in the
Management
(SEPM)
memory.
8/08/2020 Department of Computer Science and Engineering
Linked list
Example of Linked List:
Format : [data,address]
Head->[3,1000]->[43,1001]->[21,1002]
and Project
Management
(SEPM)
struct LinkedList
{ data next ---
int data;
struct LinkedList *next;
};
and Project
Management
The data field stores the element and the next is a
(SEPM)
node p;
p = head;
while(p != NULL)
{
p = p->next; Management
and Project
} (SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct
and Project
node));
Management
(SEPM)
/* Connect nodes */
one->next = two; two three null
two->next = three;
one two three
three->next = NULL;
and Project
/* Save address of second node in head */
Management
(SEPM)
Second node = two;
8/08/2020 Department of Computer Science and Engineering
Singly linked list
(SEPM)
E A B
and Project C D -NULL
Management
(SEPM)
HEAD
8/08/2020 Department of Computer Science and Engineering
Insertion at the beginning
void insert(item)
{
struct node *newNode;
newNode = malloc(sizeof(struct
node));
newNode->data = item;
newNode->next = head;
head = newNode;
}
HEAD 48 17 142 //
Step 1 Step 2
Step 3
HEAD 93
HEAD 48 17 142 //
Step 1 Step 2
Step 3
HEAD 48 17 142 93 //
8/08/2020 Department of Computer Science and Engineering
Insertion at specified position
void Insert (int X, List L, Position P)
{
position Newnode;
Newnode = malloc (size of (Struct Node));
If (Newnode! = NULL)
{
Newnode ->Element = X;
Newnode ->Next = P-> Next;
P-> Next = Newnode;
}
8/08/2020
} Department of Computer Science and Engineering
Insertion at specified position
HEAD 48 17 142 //
Step 1 Step 2
HEAD 48 17 //
142
HEAD 48 17 142
Step 1
Step 2
HEAD 48 17 142 //