Data Structures and Algorithms: Module - 3 Linked List
Data Structures and Algorithms: Module - 3 Linked List
Algorithms
Module -3
Linked List
Dr. R. Jothi, SCOPE
VIT Chennai
Outline
Linked List data structure
List over Array
Applications
2000
4000
2004
2004
2008
2060
2012
3100
2016
2016
Linked List
A linked list consists of a sequence of elements
(nodes) that are not necessarily physically adjacent
Each node contains
A piece of data
Pointer to next node in the list
Head node
Pointer to the first node
The last node contains NULL link
A B C
Head
Structure of a node in the List
//If data is integer //If data is a structure
struct nd { struct nd {
int data; struct item data;
struct nd *next; struct nd *next;
} node; } node;
Array Vs. Linked List
Arrays are suitable for:
Inserting/deleting an element at the end.
Randomly accessing any element.
Searching the list for a particular value.
Linked lists are suitable for:
Inserting/deleting are easier
Applications where sequential access is required.
In situations where the number of elements cannot be predicted
beforehand, so no memory wastage
Item to be
tmp 88 inserted
5 99 23
curr
88
Pseudocode for insertion
struct node {
struct item data;
struct nd * next;
};
Item to be deleted
A B C
tmp
curr
A B C
Pseudo-code for deletion Spring 2012
struct node {
struct item data;
struct nd * next;
};
The list we have seen so far is singly linked list, as the nodes
of the has only one link to its successor
If the nodes have two links one for predecessor and one for
successor, then it is called as doubly linked list. (Discussed in
next class)
head
A B C
Summary of Operations on Linked List
Create
head
Insert
Delete
Search
Traversing the list
Implementation of Singly Linked List : Example
Consider the structure of a node as follows:
struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};
head
roll
name
age
A B C
node *head;
………
head = create_list();
//in main()
traverse(head);
Insertion in Linked List
At beginning
At end
At middle (before/after some node)
head
A B C
X
Dr. R. Jothi, VIT Chennai
Insertion at End
When a node is added at the end,
– Two next pointers need to be modified
• Last node now points to the new node.
• New node points to NULL.
head
A B C X
Item to be
tmp X inserted
head A B C
curr
X Order?
void insert (node **head)
Spring 2012
{ Programming and Data Structure
int k = 0, rno;
node *p, *q, *new;
p = *head;
{
int rno;
node *p, *q;
p = head;
if (p->roll == rno)
/* Delete the first element */
{
head = p->next;
free (p);
}
else
Spring 2012
{ Programming and Data Structure
while ((p != NULL) && (p->roll != rno))
{
q = p;
p = p->next;
}
top
top
top
new->value = element;
new->next = *top;
top = new;
return top;
}
Dr. R. Jothi, VIT Chennai
Pop
node * pop (stack *top)
{
int t;
stack *p;
if (*top == NULL)
{
printf (“\n Stack is empty”);
exit(-1);
}
else
{
t = top->value;
p = top;
top = top->next;
printf(“%d”,t);
free (p);
return top; Dr. R. Jothi, VIT Chennai
}
Queue using Linked List
ENQUEUE
front rear
DEQUEUE
front rear