Department of
Computer Science and Engineering
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
Linked list - Definition
Types of linked list
Singly linked list
Representation of singly linked list
Operations of singly linked list
Advantages
and Project
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Linked list - Definition
Definition:
A linked list is a sequence of data structures, which
are connected together via links.
Linked List is a sequence of links which contains
items.
Each link contains a connection to another link.
Unlike arrays, Management
linked
and Project
list elements are not stored at a
contiguous location;
(SEPM)
the elements are linked using
pointers.
8/08/2020 Department of Computer Science and Engineering
Linked list - Definition
and Project
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Linked list
Linked list:
A node is a collection of two sub-elements or parts.
A data part that stores the element and a next part
that stores the link to the next node.
Each node points to the next node present in the
order.
The first node Management
and Project
is always used as a reference to
(SEPM)
traverse the list and is called HEAD. The last node
points to NULL.
8/08/2020 Department of Computer Science and Engineering
Linked list
and Project
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Linked list
Linked list has two parts:
The data section
The address section that holds the address of the
next element in the list, which is called a node.
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]
In the above example, the number 43 is present at
location 1000 and the address is present at in the
previous node.
and Project
Management
This is how a linked list is represented.
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Linked list
Example of Linked List:
and Project
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Linked list
Declaring a Linked List:
In C language, a linked list can be implemented using
structure and pointers .
struct LinkedList
{ data next ---
int data;
struct LinkedList *next;
};
and Project
Management
The data field stores the element and the next is a
(SEPM)
pointer to store the address of the next node.
8/08/2020 Department of Computer Science and Engineering
Linked list
Creating a node:
typedef struct LinkedList *node; //Define node as pointer
node createNode()
{
node temp; // declare a node
temp = (node)malloc(sizeof(struct LinkedList));
temp->next = NULL;
return temp; //return the new node
}
and Project
Management
null ---temp’s
(SEPM)
next field
Temp’s data field
8/08/2020 Department of Computer Science and Engineering
Linked list
Creating a node:
node addNode(node head, int value)
{
node temp,p; //declare two nodes temp and p
temp = createNode();
temp->data = value;
if(head == NULL)
{
head = temp; //when linked list is empty
}
and Project value
else Management
(SEPM)
{
8/08/2020 Department of Computer Science and Engineering
Linked list
Creating a node:
p = head; /* some data is there
while(p->next != NULL)
{
p = p->next
P temp
}
p->next = temp
}
return head;
} and Project
The new node will always be added after the last
Management
(SEPM)
node. This is known as inserting a node at the rear
end.
8/08/2020 Department of Computer Science and Engineering
Linked list
Traversing the list:
The linked list can be traversed in a while loop by using
the head node as a starting reference:
node p;
p = head;
while(p != NULL)
{
p = p->next; Management
and Project
} (SEPM)
8/08/2020 Department of Computer Science and Engineering
Types of linked list
Types of linked list
Singly Linked List − Item navigation is forward
only.
Doubly Linked List − Items can be navigated
forward and backward.
Circular Linked List − Last item contains link of
the first elementand as next and the first element has
Project
a link to the last element as previous.
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Types of linked list
Singly Linked List:
Item navigation is forward only.
Singly linked lists contain nodes which have a data
field and next field, which points to the next node in
line of nodes.
Each node has data and a pointer to the next node.
and Project
Operations that can be performed on singly linked
Management
(SEPM)
lists include insertion, deletion.
8/08/2020 Department of Computer Science and Engineering
Types of linked list
Singly Linked List:
and Project
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Representation of a singly linked list
and Project
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Singly linked list
8/08/2020 Department of Computer Science and Engineering
Creating a node
* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct
and Project
node));
Management
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Creating a node
/* Assign data values */
one->data = 1;
one two three
two->data = 2; 1 2 3
three->data = 3;
Head
/* 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
In singly linked list, insertion of node into a
linked list requires a free node in which the
information can be inserted and then the node can be
inserted into the linked list.
Insertion at the beginning.
Insertion at the end of list.
Insertion at the specified position.
8/08/2020 Department of Computer Science and Engineering
Insertion at the beginning
Insertion at the beginning:
The new node is always added before the head of
the given Linked List.
And newly added node becomes the new head of
the Linked List.
For example if the given Linked List is 10->15->20-
>25 and we add an item 5 at the front, then the
Linked List becomes
Management 5->10->15->20->25.
and Project
(SEPM)
8/08/2020 Department of Computer Science and Engineering
Insertion at the beginning
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;
}
8/08/2020 Department of Computer Science and Engineering
Insertion at the beginning
HEAD 48 17 142 //
Step 1 Step 2
Step 3
HEAD 93
8/08/2020 Department of Computer Science and Engineering
Insertion at end
Insertion at the end:
The new node is always added after the last node
of the given Linked List.
For example if the given Linked List is 5->10->15-
>20->25 and we add an item 30 at the end, then
the Linked List becomes 5->10->15->20->25->30.
8/08/2020 Department of Computer Science and Engineering
Insertion at end
8/08/2020 Department of Computer Science and Engineering
Insertion at end
void insert(item)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = item;
newNode->next = NULL;
struct node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
8/08/2020 Department of Computer Science and Engineering
Insertion at end
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
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
8/08/2020 Department of Computer Science and Engineering
Insertion at specified position
8/08/2020 Department of Computer Science and Engineering
Deletion
void Delete(int X, List L)
{
position P, Temp;
P = Findprevious (X,L);
If (!IsLast(P,L))
{
Temp = P→Next;
P →Next = Temp→Next;
Free (Temp);
}
8/08/2020 Department of Computer Science and Engineering
}
Deletion
HEAD 48 17 142
Step 1
Step 2
HEAD 48 17 142 //
8/08/2020 Department of Computer Science and Engineering
Delete the list
void DeleteList (List L)
{
position P, Temp;
P = L →Next;
L→Next = NULL;
while (P! = NULL)
{
Temp = P→Next
free (P);
P = Temp;
}}
8/08/2020 Department of Computer Science and Engineering
Find
position Find (int X, List L)
{
position P;
P = L-> Next;
while (P! = NULL && P Element ! = X)
P = P->Next; 1000
return P; L 1000
} P
}
8/08/2020 Department of Computer Science and Engineering
Find previous
position FindPrevious (int X, List L)
{
position P;
P = L;
while (P -> Next ! = Null && P ->Next Element ! = X)
P = P ->Next;
return P;
}
8/08/2020 Department of Computer Science and Engineering
Find next
position FindNext (int X, List L)
{
P = L ->Next;
while (P Next! = NULL && P Element ! = X)
P = P→Next;
return P→Next;
}
8/08/2020 Department of Computer Science and Engineering
Isempty()
int IsEmpty (List L)
{
if (L -> Next = = NULL)
return (1);
}
8/08/2020 Department of Computer Science and Engineering
Islast()
int IsLast (position P, List L)
{
if (P->Next = = NULL)
return;
}
8/08/2020 Department of Computer Science and Engineering
Advantages
1) Insertions and Deletions can be done easily.
2) It space is not wasted as we can get space
according to our requirements.
3) Its size is not fixed.
4) Elements may or may not be stored in consecutive
memory available, even then we can store the data in
computer.
8/08/2020 Department of Computer Science and Engineering
Thank You
Department of Computer Science and Engineering