Unit 3
Unit 3
LIMITATIONS OF ARRAYS
A B C ∅
He
ad
A linked list is a series of
connected nodes
Each node contains at
least
A piece of data (any type)
Pointer to the next node in the list
node
Head: pointer to the first
node A
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to
full; otherwise may result in much memory our need) there is no waste of memory.
waste.
Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]
Arrays vs Linked List
(Summary)
Types of lists
head
a b c d
🡪 Creating a List
🡪 Traversing in List
🡪 Inserting an element in a list
🡪 Deleting an element from a list
🡪 Searching a list
🡪 Reversing a list
Creating a node
struct node{
int data; // A simple node of a linked list
node*next;
}*start; //start points at the first node
start=NULL ; initialised to NULL at beginning
Traversal Singly Linked Lists
ALGORITHM FOR TRAVERSING A LINKED LIST
Step 1:
[INITIALIZE] SET PTR = START
Step 2:
Repeat Steps 3 and 4 while PTR != NULL
Step 3:
Apply Process to PTR->DATA
Step 4:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
START
9 1 7 3 4 2 6 5 X
START
START, PTR
9 X
1 7 3 4 2 6 5
START
void insert_end(node* p)
{
node *q=start;
if(start==NULL)
{
start=p;
Printf(”\nNode inserted successfully at the
end…!!!\n”);
}
else{
while(q->link!=NULL)
q=q->link;
q->next=p;
}
}
Inserting after an element
Here weagain need to do 2 steps :-
start
START
7 3 4 2 6 5 X
START
Deleting the last node
Here we apply 2 steps:-
start
45 85 node3
Deleting the last node
ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR 1 7 3 4 2 6 5 X
Step 8: EXIT
START, PREPTR, PTR
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
void del_last()
{
if(start==NULL)
printf(”\nError….List is
empty”); else
{
node* q=start;
while(q->link->link!=NULL)
q=q->link;
node* temp=q->link;
q->link=NULL;
delete temp;
Printf(”\nDeleted successfully…”);
}
}
}
Deleting a particular node
To be deleted
Deleting a particular node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE
LINKED LIST
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X
void del(int c)
{
node* q=start;
for(int i=2;i<c;i++)
{
q=q->link;
if(q==NULL)
Printf(”\nNode not
found\n”);
}
if(i==c)
{ node* p=q->link; //node to be deleted
q->link=p->link; //disconnecting the node p
delete p;
Printf(“Deleted Successfully”);
}
}
Searching in a SLL
🡪 Searching involves finding the required element in
the given list
🡪 We can use various techniques of searching like
linear search or binary search where binary search
is more efficient in case of Arrays
🡪 But in case of linked list since random access is not
available it would become complex to do binary
search in it
🡪 We can perform simple linear search traversal
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL // not found
Step 5: EXIT // found, output POS
void search(int x)
{
node*temp=start;
while(temp!=NULL)
{
if(temp->data==x)
{
Printf(“FOUND %d”,temp->data);
break;
}
temp=temp->next;
}
}
Reversing a linked list
🡪 We can reverse a linked list by reversing the
direction of the links between 2 nodes
🡪 We make use of 3 structure pointers say p,q,r
Head P p q NULL
q r
NULL
1 2 3 4 5 6 7
Inserting a node in CSLL
Here also we have three cases:-
START, PTR
1 7 3 4 2 6 5
PTR
START
9 1 7 3 4 2 6 5
START
Circular linked list
Algorithm to insert a new node at the
end of the circular linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != START
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR ->NEXT = New_Node
Step 10: EXIT
Circular linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5 9
PTR
START
Circular linked list
Algorithm to insert a new node after
a node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
Deleting a node in CSLL
Here also we have three cases:-
START, PTR
1 7 3 4 2 6 5
PTR
START
7 3 4 2 6 5
START
Circular linked list
Algorithm to delete the last node of the
circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = START
Step 7: FREE PTR
Step 8: EXIT
Circular linked list
1 7 3 4 2 6 5
1 7 3 4 2 6 5
PREPTR
PTR
START
1 7 3 4 2 6
START
Circular linked list
Algorithm to delete the node after a given
node from the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step 7: SET PREPTR->NEXT = PTR->NEXT
Step 8: FREE PTR
Step 9: EXIT
Circular linked list
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 6 5
START
Doubly Circular Linked List
1. Doubly circular linked list is a linked data structure that
consists of a set of sequentially linked records called nodes.
A B C
NULL 11 786 200 656 400 786 777 NULL
START
2 X
X 9 1 7 3 4
START
void insert_beg(node *p)
{
if(start==NULL)
{
start=p;
Printf("\nNode inserted successfully at the
beginning\m");
}
else
{
node* temp=start;
temp->previous=p //making 1st node’s previous point to the
start=p;
; new node
p->next=temp; //making next of the new node point to
1st node the
Printf("\nNode inserted successfully at the
beginning\n");
}
Inserting at the end
Inserting at the end
Algorithm to insert a new node at the end of the doubly linked
list
START, PTR
X 1 7 3 4 2 9 X
PTR
void insert_end(node* p)
{
if(start==NULL)
{
start=p;
Printf("\nNode inserted successfully at the end");
}
else
{
node* temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->previous=temp;
Printf("\nNode inserted successfully at the
end\n");
}
}
Inserting after a node
START, PTR
X 1 7 3 4 2 X
START PTR
X 1 7 3 9 4 2 X
START
void insert_after(int c,node* p)
{
temp=start;
for(int i=1;i<c-1;i++)
{
temp=temp->next;
}
p->next=temp->next;
temp->next->previous=p;
temp->next=p;
p->previous=temp;
printf("\nInserted
successfully");
}
Deleting a node in DLL
Here also we have three cases:-
a b c
START, PTR
7 3 4 2 X
void del_at(int c)
{
node*s=start;
{
for(int i=1;i<c-1;i++)
{
s=s->next;
}
node* p=s->next;
s->next=p->next;
p->next->previous=s;
delete p;
printf("\nNode number and deleted successfully");
}
}
}
Deleting the first node
Algorithm to delete the first node from the doubly linked
list
START, PTR
7 3 4 2 X
Deleting the last node
START, PTR
X 1 3 5 7 8 9 X
1
START PTR
X 1 3 5 7 8 X
START
Deleting the node after a specific
location
Algorithm to delete the node after a given node from the
doubly linked list
START, PTR
X 1 3 4 7 8 9 X
1
START
PTR
X 1 3 4 8 9 X
START
Circular Doubly Linked List
• A circular doubly linked list or a circular two way linked
list is a more complex type of linked list which contains a
pointer to the next as well as previous node in the
sequence.
• The difference between a doubly linked and a circular
doubly linked list is same as that exists between a singly
linked list and a circular linked list.
• The circular doubly linked list does not contain NULL in
the previous field of the first node and the next field of
the last node.
Circular Doubly Linked List
• Rather, the next field of the last node stores the address
of the first node of the list, i.e; START.
• Similarly, the previous field of the first field stores the
address of the last node.
Circular Doubly Linked List
• Since a circular doubly linked list contains three parts in
its structure, it calls for more space per node and for
more expensive basic operations.
• However, it provides the ease to manipulate the
elements of the list as it maintains pointers to nodes in
both the directions . The main advantage of using a
circular doubly linked list is that it makes searches twice
as efficient. START
2 3 4
1 1
Inserting a node in CDLL
1 7 3 4 2
START
2
9 1 7 3 4
START
Insertion at the END in CDLL
🡪 Algorithm to insert a new node at the end
of the circular doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET New_Node->PREV = START->PREV
Step 7: SET START->PRE->NEXT=New_Node
Step 8: SET START->PREV= New_Node
Step 7: EXIT 1 7 3 4 2
START
1 7 3 4 2 9
START
Circular Doubly Linked List
🡪 Algorithm to insert a new node after a
node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT->PREV = New_Node
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Circular Doubly Linked List
Algorithm to insert a new node after a node
1 7 3 4 2
START, PTR
1 7 3 4 2
PTR
9
START
1 7 3 9 4 2
START
Deleting a node in DLL
Here also we have three cases:-
START, PTR
3 5 7 8 9
1
START
Circular Doubly Linked List
Algorithm to delete the last node of the circular doubly
linked list
START PTR
X 1 3 5 7 8
START
Circular Doubly Linked List
Algorithm to delete the node after a given node from the
circular doubly linked list
START
1 3 5 7 8 91
START PTR
1 3 4 8 9
START
Circular Doubly Linked List
🡪 A header linked list is a special type of linked list which
contains a header node at the beginning of the list.
🡪 So, in a header linked list START will not point to the first
node of the list but START will contain the address of the
header node.
🡪 There are basically two variants of a header linked list-
🡪 Grounded header linked list which stores NULL in the
next field of the last node
🡪 Circular header linked list which stores the address of
the header node in the next field of the last node.
Here, the header node will denote the end of the list.
Circular Doubly Linked List
Header Node
1 2 3 4 5 6 X
START
Header Node
1 2 3 4 5 6
START
Circular Doubly Linked List
Algorithm to traverse a Circular Header Linked List
🡪 The cache in your browser that allows you to hit the BACK
button (a linked list of URLs)
6 2 3 8 -3 18 0 0 23
0 2 0 2 4
Index
represents
exponents
•This is why arrays aren’t good to represent
polynomials:
6 2 0 0 -3 0 ………… 0 16
WASTE OF
SPACE!
•Advantages of using an Array:
P1 23 9 18 7 41 6 18 7 3 0
P2 4 6 10 4 12 1 8 0
struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;
b 14 10 10x6
8x 3x
b
8 14 -3 10 10 6 null
Adding
Polynomials
3 14
a
2 8 1 0
8 14 -3 10 6
b 10
11 14 a->expon == b->expon
d
3 14 2 8 1 0
a
8 14 -3 10 6
10 b
11 14 -3 a->expon < b->expon
10
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8
d
a->expon > b->expon
Dynamic Memory Management
🡪 The Library makes use of a dynamic memory manager which handles allocation
and deallocation of dynamic memory.
🡪 The methods used for allocation and deallocation are wrapper functions around
the native malloc, calloc, realloc, and free methods.
🡪 Hence, the functionality of teh module is very similar to the native C interface
but it allows for structured error recovery and application termination in case of
failure. It covers especially the following three situations:
🡪 Handling of allocation and deallocation of dynamic memory
🡪 Recovering from temporary lack of available memory
🡪 Panic handling in case a new allocation fails
Standard library functions for dynamic
memory management
Sparse Matrix
🡪 In computer programming, a matrix can be defined with a 2-dimensional array.
Any array with 'm' columns and 'n' rows represent a m X n matrix.
🡪 There may be a situation in which a matrix contains more number of ZERO
values than NON-ZERO values. Such matrix is known as sparse matrix.
🡪 Sparse matrix is a matrix which contains very few non-zero elements.
🡪 When a sparse matrix is represented with a 2-dimensional array, we waste
a lot of space to represent that matrix.
🡪 For example, consider a matrix of size 100 X 100 containing only 10
non-zero elements. In this matrix, only 10 spaces are filled with non-zero
values and remaining spaces of the matrix are filled with zero.
🡪 That means, totally we allocate 100 X 100 X 2 = 20000 bytes of space to
store this integer matrix. And to access these 10 non-zero elements we
have to make scanning for 10000 times. To make it simple we use the
following sparse matrix representation.
Sparse Matrix Representations
🡪 In computer programming, a matrix can be defined with a 2-dimensional array.
Any array with 'm' columns and 'n' rows represent a m X n matrix.
🡪 There may be a situation in which a matrix contains more number of ZERO
values than NON-ZERO values. Such matrix is known as sparse matrix.
🡪 Sparse matrix is a matrix which contains very few non-zero elements.
🡪 When a sparse matrix is represented with a 2-dimensional array, we waste a lot
of space to represent that matrix.
🡪 For example, consider a matrix of size 100 X 100 containing only 10 non-zero
elements. In this matrix, only 10 spaces are filled with non-zero values and
remaining spaces of the matrix are filled with zero.
🡪 That means, totally we allocate 100 X 100 X 2 = 20000 bytes of space to store
this integer matrix. And to access these 10 non-zero elements we have to make
scanning for 10000 times. To make it simple we use the following sparse matrix
representation.
Sparse Matrix Representations