2 OVERVIEW
Definition & Representation
Why Linked List?
Types of memory representation & memory allocation
Data Structure of Linked List Node
Linked List/Singly Linked List(SLL)/One-way List operations
Traversing, Searching, Insertion, and Deletion
Stack & Queue Implementation using Linked List
Linked Stacks and Queues (Multiple Stacks & Queues)
Circular SLL
Doubly Linked lists(DLL)/Two-way list and Circular DLL
SLL & DLL with Header
Applications of Linked lists
Polynomials, Sparse Matrix representation
3 Definition & Representation
Linked List is a Dynamic Linear Data Structure that can grow and shrink at runtime
Consists of items, called node, that are linked to each other
A node contains two fields
an information: holds the actual data/record
a next : contains the address of the next node of the list
An external pointer points to the first element of the list (firstNode)
The next address field of the last node contains a NULL pointer
The list with no nodes is called empty list or null list
A list can be initialized to an empty list by setting external pointer to NULL
firstNode
info next info next info next info next
NULL
4 Why Linked List?
In arrays, a fixed amount of storages (MAXSIZE) remains allocated even if stack or
queue uses smaller amount or no storage at all
No more than that fixed size (MAXSIZE) of storage may be allocated thus
introducing the possibility of overflow
In dynamic arrays, array can grow in at runtime, but insertion or removal of
elements in between leads to shifting subsequent elements.
There will be circumstances where there will be space allocated but not getting
used
Linked list is a dynamic solution that allocated only enough space for the data you
need to store
Insertion and removal of elements at any position is easily possible with just one
basic operation without the need of shifting elements
5 Types of memory representation & memory
allocation
Static Representation using Arrays
Dynamic Representation
6 Data Structure of Linked List Node
typedef struct node
{
int info;
struct node *next;
} Node;
typedef Node * NodePtr; firstNode
NodePtr firstNode = NULL; NULL
7 Node Creation & Initialization
NodePtr getNode()
info next
{ p
NodePtr p = (NodePtr) malloc(sizeof(Node)); addr
if(p == NULL)
{
printf(“Error: Memory Allocation Failed”);
exit(0);
} info next
p
p->next = NULL; addr NULL
return p;
}
8 Linked List/Singly Linked List/One-way List
operations
Traversing
Print, Count, Sum, Product
Searching
Linear Search
Insertion
At font, At end, At any position, After given element, Into a sorted list
Deletion
At font, At end, At any position, Of given element
info next info next info next info next
firstNode NULL
9 Traversing & Printing values of node
void printList()
{
if(firstNode == NULL)
{
printf("\nEmpty List!\n");
return;
}
for(NodePtr p = firstNode; p != NULL; p = p->next)
{
printf("%d ", p->info);
}
} p
info next info next info next info next
firstNode 10 20 30 40 NULL
p
10 Searching info next info next info next info next
firstNode
void search(int key) 10 20 30 40 NULL
{
if(firstNode == NULL)
{
printf("\nEmpty List!\n");
return;
}
int position = 0;
for(NodePtr p = firstNode; p != NULL; p = p->next)
{
position++;
if(p->info == key)
{
printf(“Element %d found at position: %d", p->info, position);
break;
} } }
node
11 Insertion: At font 20
void insertNodeAtFront(Node node)
{
firstNode info next
NodePtr newNode = getNode();
NULL firstNode 20 NULL
newNode->info = node.info;
if(firstNode == NULL) info next info next
{
firstNode X 10 30 NULL
firstNode = newNode;
} info next
else
newNode 20 Node to be
{ inserted
newNode->next = firstNode;
firstNode = newNode; info next info next info next
} firstNode
20 10 30 NULL
}
newNode
12 Insertion: At end
void insertNodeAtEnd(Node node)
{
NodePtr newNode = getNode(); p newNode
newNode->info = node.info;
info next info next info next
firstNode
if(firstNode == NULL) 10 20 NULL 30 NULL
{
firstNode = newNode;
} Node to be
else inserted
{
NodePtr p;
for(p = firstNode; p->next != NULL; p = p->next); //traverse to end of list
p->next = newNode;
}
info next info next info next
} firstNode
10 20 30 NULL
p
13 Insertion: After given element,
info next info next info next
firstNode
void insertNodeAfterValue(Node node, int value)
{
10 20
X 30 NULL
if(firstNode == NULL)
printf(“Empty List”);
else info next
newNode
{ 40 NULL
NodePtr p;
for(p = firstNode; p != NULL && p->info != value; p = p->next);
if(p == NULL)
{
printf(“Key Element not found. Insertion Failed.”);
Node to be
return; inserted
}
NodePtr newNode = getNode();
newNode->info = node.info;
newNode->next = p->next;
p->next = newNode; info next info next
info next info next
}
firstNode 10 20 40 30 NULL
}
14 Homework
Insertion
At any position
Into a sorted list
15 Deletion: At font
int deleteNodeAtFront()
{
if(firstNode == NULL)
{
printf("Error: Empty List!\n");
return;
}
NodePtr p = firstNode;
int value = p->info;
firstNode = firstNode->next;
free(p);
return value;
}
q p
16 Deletion: At End info next
info next info next
firstNode
int deleteNodeAtEnd() 10 20 30 NULL
{
if(firstNode == NULL)
{ Node to be
printf("Error: Empty List!\n"); deleted
return;
}
NodePtr p = firstNode, q;
for(; p->next != NULL; p = p->next) //traverse to end of list
q = p;
int value = p->info;
if(p == firstNode)
firstNode = NULL
else q
q->next = NULL;
free(p); info next info next
return value; firstNode
10 20 NULL
}
17 Deletion: Of given element
void deleteNodeWithValue(int value)
{
if(firstNode == NULL)
{
printf("Error: Empty List!\n");
return;
}
Node to be
NodePtr p = firstNode, q; deleted
for(; p != NULL && p->info != value; p = p->next)
q = p; q p
info next info next info next
if(p == firstNode) firstNode
firstNode = NULL 10 20 30 NULL
else
q->next = p->next;
free(p);
} info next info next
firstNode
10 30 NULL
18 Deletion: At any position,
To be done as homework
19 Insertion Deletion Summary
Insertion
Insert at font
Insert at end
Insert at any position
Insert after given element
Insert Into a sorted list
Deletion
Deletion at font
Deletion at end
Deletion at any position
Deletion of given element
20 Stack & Queue Implementation using SLL
Implementation of Stack & Queue follows the same operations of SLL
For Stack (Linked Stack) info next info next info next
Insert at font of SLL (top) top 10 20 30 NULL
Deletion at font of SLL (top)
For Queue (Linked Queue) front rear
Insert at end of SLL (rear) info next info next info next
Deletion at font of SLL (front) 10 20 30 NULL
21 Linked Stacks and Queues (Multiple Stacks & Queues)
Multiple Stacks Data Structure
top
0
1
2
3
4
5
6
7
8
9
Multiple Stacks Data Structure operation Push & Pop
22
top
0
1
2
3
4
5 NULL
6
7 NULL
8
9
23
Multiple Queues Data Structure
front rear
0 0
1 1
2 2
3 NULL NULL 3
4 4
24
Multiple Queues Data Structure Operation addQ (Add item to ith queue)
25
Multiple Queues Data Structure Operation delete (Delete item from ith queue)
26 Circular SLL
CSLL is same as SLL, but instead of last node storing NULL value, it stores
address of the first node
Reference to the list will be pointing to the last node rather than first node
We get reference to first node just by giving lastNode->next
Will have easy access to both last node as well as first node of the list
lastNode
info next info next info next info next
10 20 30 40
27 Circular SLL: Insert @ Front
void cinsertNodeAtFront(Node node)
{ lastNode
NodePtr newNode = getNode(); newNode
*newNode = node; info next info next info next info next
50 10 20 30 40
if(lastNode == NULL)
{
lastNode = newNode;
lastNode->next = lastNode;
} Node to be
else inserted at front
{
newNode->next = lastNode->next;
lastNode->next = newNode;
}
}
28 Circular SLL: Insert @ End
void cinsertNodeAtEnd(Node node) lastNode newNode
{
NodePtr newNode = getNode(); info next info next info next info next
*newNode = node;
10 20 30 40 50
if(lastNode == NULL)
{
lastNode = newNode;
lastNode->next = lastNode;
}
else Node to be
inserted at end
{
newNode->next = lastNode->next;
lastNode->next = newNode;
lastNode = newNode;
}
}
29 Circular SLL: Delete @ Front Node to be
deleted
int cdeleteNodeAtFront() lastNode
{
if(lastNode == NULL) info next info next info next info next
{
10 20 30 40
printf(“Empty List”);
return -1;
}
else
{
NodePtr p = lastNode->next;
lastNode
lastNode->next = lastNode->next->next;
int value = p->info;
info next info next info next
free(p);
return value; 20 30 40
} }
Circular SLL: Delete @ End
30
int cdeleteNodeAtEnd()
{ p lastNode
q
if(lastNode == NULL)
{
printf(“Empty List”); info next info next info next info next
return -1;
} 10 20 30 40
else
{
NodePtr q = lastNode, p = lastNode;
int value = p->info; Node to be
for(; q->next != lastNode; q=q->next); deleted
if (q == lastNode) lastNode = NULL; lastNode
else
{
info next info next info next
q->next = lastNode->next;
lastNode = q;
10 20 30
}
free(p);
return value;
} }
31 Doubly Linked lists(DLL)/Two-way list & Circular DLL
Doubly Linked List
firstNode
left info right left info right left info right left info right
NULL 10 20 30 40 NULL
Circular DLL
firstNode
left info right left info right left info right left info right
10 20 30 40
32 DLL Data Structure
typedef struct dllnode
{
struct dllnode *left left info right
int info;
struct dllnode *right;
} DLLNode;
typedef Node * NodePtr;
firstNode
NodePtr firstNode = NULL;
NULL
33 Node Creation & Initialization
NodePtr getNode()
left info right
{ p
NodePtr p = (NodePtr) malloc(sizeof(Node)); addr
if(p == NULL)
{
printf(“Error: Memory Allocation Failed”);
exit(0);
} left info right
p
p->left = p->right = NULL; addr NULL NULL
return p;
}
34 Insertion @ Font in DLL
void insertNodeAtFront(Node node)
{ newNode firstNode
NodePtr newNode = getNode();
newNode->info = node.info;
left info right left info right left info right
if(firstNode == NULL) NULL 10 NULL 20 30 NULL
{
firstNode = newNode;
} firstNode
else
{
left info right left info right left info right
newNode->right = firstNode;
firstNode->left = newNode; NULL 10 20 30 NULL
firstNode = newNode;
}
}
35 Insertion @ Font in Circular DLL
newNode firstNode
void cinsertNodeAtFront(Node node)
{
NodePtr newNode = getNode(); left info right left info right left info right
newNode->info = node.info; 30 10 20
if(firstNode == NULL)
{
firstNode = newNode;
firstNode->left = firstNode->right = firstNode;
}
else
{
newNode->right = firstNode; firstNode
newNode->left = firstNode->left
firstNode->left->right = newNode;
left info right left info right left info right
firstNode->left = newNode;
firstNnode = newNode; 30 10 20
} }
36 Delete @ Font in DLL
int deleteNodeAtFront()
{
if(firstNode == NULL) firstNode
{
printf(“Empty List”); left info right left info right left info right
} NULL 10 20 30 NULL
else
{
NodePtr p = firstNode;
int value = p->info; firstNode
firstNode = firstNode->right;
left info right left info right
firstNode->left = NULL;
free(p); NULL 20 30 NULL
return value;
} }
37 Delete @ Font in Circular DLL firstNode p
int deleteNodeAtFront()
left info right left info right left info right
{
if(firstNode == NULL) 30 10 20
{
printf(“Empty List”);
}
else
{
firstNode
NodePtr p = firstNode;
int value = p->info;
firstNode->left->right = firstNode->right; left info right left info right
firstNode->right->left = firstNode->left; 10 20
firstNode = firstNode->right;
free(p);
return value;
} }
38 Homework
Insertion @ End in DLL
Insertion @ End in Circular DLL
Insertion @ a given position in DLL
Delete @ End in DLL
Delete @ End in Circular DLL
Deletion @ a given position in DLL
39 SLL and Circular SLL with Header Node
SLL with Header Node
data next info next info next info next
headerNode
10 20 30 NULL
Circular SLL with Header Node
headerNode
data next info next info next info next
10 20 30
40 DLL & Circular DLL with Header Node
Doubly Linked List
headerNode
left data right left info right left info right left info right
NULL 20 30 40 NULL
Circular DLL
headerNode
left data right left info right left info right left info right
20 30 40
42 Applications of Linked lists
Polynomials using SLL & Circular SLL
Representation
Evaluation: Functions & Program
Addition: Functions & Program
Sparse Matrix using Circular Singly Linked List
Representation
43 Polynomials using SLL
Polynomial DS
typedef struct node
{
int coef, exp; coef exp next
struct node * next;
}Node;
typedef Node * NodePtr;
NodePtr a, b;
44 Polynomial Addition
Example
a = 3x14 + 2x8 +1
a 3 14 2 8 1 0 NULL
b = 8x14 – 3x10 + 10x6
b 8 14 -3 10 10 6 NULL
c = 11x14 – 3x10 + 2x8 + 10x6 +1
c 11 14 -3 10 2 8 10 6 1 0 NULL
NodePtr padd(NodePtr a, NodePtr b) rear else if(a->exp > b->exp)
{ {
45NodePtr c, rear; c attach(a->coef, a->exp, rear);
int sum; rear = rear->next;
c = rear = getNode(); //dummy node a = a->next;
}
while(a && b) }
{
if(a->exp < b->exp) //Add remaining nodes to c
{ for(; a; a = a->next, rear = rear->next)
attach(b->coef, b->exp, rear); attach(a->coef, a->exp, rear);
rear = rear->next; for(; b; b = b->next, rear = rear->next)
b = b->next; attach(b->coef, b->exp, rear);
}
else if(a->exp == b->exp) //Delete first dummy node
{ NodePtr tmp = c;
sum = a->coef + b->coef; c = c->next;
attach(sum, a->exp, rear); free(tmp);
rear = rear->next; return c;
a = a->next; }
b = b->next;
}
46
void attach(int coef, int exp, NodePtr rear)
{
if(coef == 0)
return;
NodePtr n = getNode();
n->coef = coef;
n->exp = exp;
rear->next = n;
}
rear
c
rear
c 11 14 -3 10 2 8 10 6 1 0 NULL
47 Polynomials using Circular SLL
Example
a = 3x14 + 2x8 +1
3 14 2 8 1 0 a
b = 8x14 – 3x10 + 10x6
8 14 -3 10 10 6 b
c = 11x14 – 3x10 + 2x8 + 10x6 +1
11 14 -3 10 2 8 10 6 1 0 c
48 Polynomial Addition using Circular SLL with Header
Example
a = 3x14 + 2x8 +1
aHead - - 3 14 2 8 1 0
b = 8x14 – 3x10 + 10x6
bHead - - 8 14 -3 10 10 6
c = 11x14 – 3x10 + 2x8 + 10x6 +1
cHead - - 11 14 2 8 10 6 1 0
-3 10
Polynomial Addition of CSLL with Header
NodePtr pcadd(NodePtr aHead, NodePtr bHead) else if(a->exp > b->exp)
{ 49 {
NodePtr a = aHead->next, b = bHead->next; attach(a->coef, a->exp, rear);
NodePtr cHead, rear; rear = rear->next;
cHead = rear = getCHeaderNode(); a = a->next;
int sum; }
}
while(a != aHead && b != bHead) for(; a!=aHead; a=a->next, rear=rear->next)
{ attach(a->coef, a->exp, rear);
if(a->exp < b->exp) for(; b!=bHead; b=b->next, rear=rear->next)
{ attach(b->coef, b->exp, rear);
attach(b->coef, b->exp, rear);
rear = rear->next; return cHead;
b = b->next; }
}
else if(a->exp == b->exp)
{
sum = a->coef + b->coef; NodePtr getCHeaderNode()
attach(sum, a->exp, rear); {
rear = rear->next; NodePtr p = getNode();
a = a->next; p->next = p;
b = b->next; return p;
} }
50
void attach(int coef, int exp, NodePtr rear)
{
if(coef == 0)
return;
NodePtr n = getNode();
n->coef = coef;
n->exp = exp;
n->next = rear->next;
rear->next = n;
}
cHead - - 11 14 2 8 10 6 1 0
-3 10
52 Sparse Matrix using Circular Singly Linked List
DS
next
Head Node down right
Element Node row col value
Circular SLL Representation of Sparse Matrix
53
Sparse Matrix
2 0 0 0
4 0 0 3
0 0 0 0
8 0 0 1
0 0 6 0
Head Node
next
down right
Element Node
row col value
down right
54 Text Books
1. Ellis Horowitz and Sartaj Sahni, Fundamentals of Data Structures in C,
2nd Ed, Universities Press, 2014.
2. Seymour Lipschutz, Data Structures Schaum's Outlines, Revised 1st Ed,
McGraw Hill, 2014.