Chapter 12 - Advance Structures
Chapter 12 - Advance Structures
Advance Structures
◼ Sparse Matrixes
➢ What is a Sparse Matrix and Where is it Used?
➢ Representation of Sparse Matrix
➢ Assignments - I
◼ The same set of data may be organized in different ways, depending upon what
operations we want to perform upon them. Each of these organizations
➢ refers to a different data structure, and
➢ supports a different set of operations.
Data Structures
5 10 0 0 2 0
0 1 9 0 0 0 In this matrix, out of 36
0 0 0 7 0 0 elements only 7 elements are
A [6] [6] =
0 0 0 0 0 0 non-zero. Such a matrix is
0 0 3 0 0 0
◼ In order to avoid this wastage of memory space, sparse matrixes are normally
represented (stored) in an alternative form, in which we explicitly store only the
non-zero elements of the matrix along with there positions. This representation
of is called the 3-tuple form / representation.
Sparse Matrix in Regular Form The Same Sparse Matrix in 3-Tuple Form
4 5 5
0 0 0 8 0
0 3 8
2 0 0 0 0
A [4] [5] = 1 0 2
0 0 3 0 0 S [6] [3] =
2 2 3
2 0 0 0 5
3 0 2
3 4 5
/* PR6_7.c: This program converts a 4 × 5 sparse matrix (given in regular form) to 3-tuple form. */
#include <stdio.h>
#include <conio.h>
#define ROW 4
#define COL 5
4 0 5 0 0
0 0 0 6 0
0 3 0 9 0
0 0 0 0 0
4 5 5
0 0 4
0 2 5
1 3 6
2 1 3
2 3 9
Example:
struct Node
{
int info;
struct Node *next;
};
[Fig. 12.3: A
Single
Linked List]
struct Node
{
int info;
struct Node *next;
};
[NOTE]: “How to create a node” is discussed when we discuss “how to create a single linked
list” in the next section.
① Create the first node (since every single linked list contains at least one node we must
create at least one node, i.e., the first node in the list).
② Create and insert subsequent nodes on demand (till the user wants) to the end
of the linked list.
[Cont.]
while(ch=='Y'||ch=='y')
{
printf("\nEnter the value (info field) of the new node: ");
scanf("%d", &item);
InsertAtEndInSLL(start, item);
printf("\nDo you want to add more nodes (press 'Y' to
continue, any other key to quit)?: ");
ch = getche();
}
return start;
}
/* End of function CreateSLL() */
[Cont.]
/* Step 1: Move a pointer “ptr” to the last node of the linked list. */
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
/* Step 2: Create a new node. Store “item” in its “info” field and “NULL” in its “next” field. */
newNode = (SNode *)malloc(sizeof(SNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->next = NULL;
/* Step 3: Point the “next” part of the last node (pointer “ptr”) to the new node */
ptr->next = newNode;
◼ C Function:
/* PR12_1.c: A program to create a single linked list and then traverse it. */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
while(ch=='Y'||ch=='y')
{
printf("\nEnter the value (info field) of the new node: ");
scanf("%d", &item);
InsertAtEndInSLL(start, item);
printf("\nDo you want to add more nodes (press 'Y' to
continue, any other key to quit)?: ");
ch = getche();
}
return start;
}
/* End of function CreateSLL() */
[Cont.]
/* Step 1: Move a pointer “ptr” to the last node of the linked list. */
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
/* Step 2: Create a new node. Store “item” in its “info” field and “NULL” in its “next” field. */
newNode = (SNode *)malloc(sizeof(SNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->next = NULL;
/* Step 3: Point the “next” part of the last node (pointer “ptr”) to the new node */
ptr->next = newNode;
Do you want to add more nodes (press ‘Y' to continue, any other key
to quit)?: y
Enter the value (info field) of the new node: 3
A new node with info field 3 added to the linked list.
Do you want to add more nodes (press ‘Y' to continue, any other key
to quit)?: y
Enter the value (info field) of the new node: 9
A new node with info field 9 added to the linked list.
Do you want to add more nodes (press ‘Y' to continue, any other key
to quit)?: n
◼ Technique:
① Create a new node. Store “item” in its “info” field and point its “next” field to “start”.
② Point “start” to the new node.
/* Step 1: Create a new node. Store “item” in its “info” field and point its “next” field to “start”. */
newNode = (SNode *)malloc(sizeof(SNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->next = start;
◼ Technique:
[NOTE]: We have already seen “how to insert a node at the end of a single linked list”
(through the function “InsertAtEndInSLL()”) in “creating a single linked list”.
/* Step 1: Move a pointer “ptr” to the last node of the linked list. */
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
/* Step 2: Create a new node. Store “item” in its “info” field and “NULL” in its “next” field. */
newNode = (SNode *)malloc(sizeof(SNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->next = NULL;
/* Step 3: Point the “next” part of the last node (pointer “ptr”) to the new node */
ptr->next = newNode;
◼ Technique:
① Move a pointer “ptr” to the node containing “data” (i.e., the node after which a new
node is to be inserted).
② Create a new node. Store “item” in its “info” field and point its “next” field to “ptr->next”.
③ Point “ptr->next” to the new node.
/* Function to insert a node at an intermediate position after a given node (a node containing “data” in
its info field) in a single linked list. */
void InsertAfterNodeInSLL (SNode *start, int data, int item)
{
SNode *newNode, *ptr;
printf("A new node with info field %d added after the node
containing %d in the linked list.\n ", newNode->info, ptr->info);
}
◼ [NOTE] - Considerations while deleting the first and the last nodes from a single
linked list:
➢ If the linked list contains just one node, then after deletion of the node,
the linked list shall be empty. So, in such case, after deletion of the
node, “start” should point to NULL.
➢ When the node is deleted its memory space must be freed (released).
return start;
}
◼ Technique:
① Move a pointer “ptr1” to the last but one node, and another pointer “ptr2” to the last
node.
② Store “NULL” in “ptr1->next”.
③ Release the node pointed out by “ptr2”.
return start;
}
◼ Technique:
① Move a pointer “ptr1” to the node containing “data” (i.e., whose next node is to be
deleted), and another pointer “ptr2” to the node which is to be deleted (i.e., ptr1->next).
② Point “ptr1->next” to “ptr2->next”.
③ Release the node pointed out by “ptr2”.
/* Function to delete an intermediate node after a given node (a node that contains “data” in its info
field) in a single linked list. */
void DeleteAfterNodeInSLL (SNode *start, int data)
{
SNode *ptr1, *ptr2;
/* Step 1: Move a pointer “ptr1” to the node containing “data” (whose next node is to be deleted),
and another pointer “ptr2” to the node which is to be deleted (i.e., ptr1->next). */
ptr1 = start;
while((ptr1->info != data)&&(ptr1->next != NULL))
{
ptr1 = ptr1->next;
}
if(ptr1->next == NULL)
{
printf("\n%d is not found in the linked list.", data);
exit(0);
}
ptr2 = ptr1->next;
◼ Technique:
Move a pointer “ptr” from “start” to the last node till the “value” is not found in the “info”
fields of one of the nodes. If the “value” is found, display the “node number”, otherwise
display “value not found”.
ptr = start;
while(ptr != NULL)
{
if(ptr->info == item)
{
printf("\n%d is found at node %d.", item, count);
exit(0);
}
count++;
ptr = ptr->next;
}
printf("\n%d is not found in the linked list.", item);
}
◼ C Function:
[Fig. 12.3: A
Double
Linked List]
struct Node
{
int info;
struct Node *prev;
struct Node *next;
};
[NOTE]: “How to create a node” is discussed when we discuss “how to create a double linked
list” in the next section.
◼ Technique: The process is same as creating a single linked list. It’s a two-step
process.
① Create the first node (since every double linked list contains at least one node we
must create at least one node, i.e., the first node in the list).
② Create and insert subsequent nodes on demand (till the user wants) to the end
of the linked list.
[Cont.]
while(ch=='Y'||ch=='y')
{
printf("\nEnter the value (info field) of the new node: ");
scanf("%d", &item);
InsertAtEndInDLL(start, item);
printf("\nDo you want to add more nodes (press 'Y' to
continue, any other key to quit)?: ");
ch = getche();
}
return start;
}
/* End of function CreateDLL()*/
[Cont.]
/* Step 1: Move a pointer "ptr" to the last node of the linked list. */
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
/* Step 2: Create a new node. Store "item" in its "info" field, "ptr" in its "prev" field,
and "NULL" in its "next" field. */
newNode = (DNode *)malloc(sizeof(DNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->prev = ptr;
newNode->next = NULL;
ptr->next = newNode; /* Step 3: Point the "next" part of the last node to the new node */
◼ C Function:
/* PR12_2.c: A program to create a double linked list and then traverse it. */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
while(ch=='Y'||ch=='y')
{
printf("\nEnter the value (info field) of the new node: ");
scanf("%d", &item);
InsertAtEndInDLL(start, item);
printf("\nDo you want to add more nodes (press 'Y' to
continue, any other key to quit)?: ");
ch = getche();
}
return start;
}
/* End of function CreateDLL() */
[Cont.]
/* Step 1: Move a pointer "ptr" to the last node of the linked list. */
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
/* Step 2: Create a new node. Store "item" in its "info" field, "ptr" in its "prev" field,
and "NULL" in its "next" field. */
newNode = (DNode *)malloc(sizeof(DNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->prev = ptr;
newNode->next = NULL;
ptr->next = newNode; /* Step 3: Point the "next" part of the last node to the new node */
Do you want to add more nodes (press ‘Y' to continue, any other key
to quit)?: y
Enter the value (info field) of the new node: 3
A new node with info field 3 added to the linked list.
Do you want to add more nodes (press ‘Y' to continue, any other key
to quit)?: y
Enter the value (info field) of the new node: 9
A new node with info field 9 added to the linked list.
Do you want to add more nodes (press ‘Y' to continue, any other key
to quit)?: n
◼ Technique:
① Create a new node. Store “item” in its “info” field, “NULL” in its “prev” field, and point its
“next” field to “start”.
② Point “start->prev” to the new node.
③ Point “start” to the new node.
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 12.82
◼ C Function:
/* Step 1: Create a new node. Store “item” in its “info” field, “NULL” in its “prev” field,
and point its “next” field to “start”. */
newNode = (DNode *)malloc(sizeof(DNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->prev = NULL;
newNode->next = start;
return start;
}
◼ Technique:
[NOTE]: We have already seen “how to insert a node at the end of a double linked list”
(through the function “InsertAtEndInDLL()”) in “creating a double linked list”.
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 12.84
◼ C Function:
/* Step 1: Move a pointer “ptr” to the last node of the linked list. */
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
/* Step 2: Create a new node. Store “item” in its “info” field, point its “prev” field to “ptr”,
and store “NULL” in its “next” field. */
newNode = (DNode *)malloc(sizeof(DNode));
if (newNode == NULL)
{
printf("\nOut of memory space");
return;
}
newNode->info = item;
newNode->prev = ptr;
newNode->next = NULL;
ptr->next = newNode; /* Step 3: Point the “next” field of the last node to the new node. */
◼ Technique:
① Move a pointer “ptr1” to the node containing “data” (i.e., to the node after which a new
node is to be inserted), and another pointer “ptr2” to its next node (i.e., to ptr1->next).
② Create a new node. Store “item” in its “info” field, point its “prev” field to “ptr1” and its
“next” field to “ptr2”.
③ Point “ptr2->prev” to the new node.
④ Point “ptr1->next” to the new node.
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 12.86
◼ C Function:
/* Function to insert a node at an intermediate position after a given node (a node containing “data” in
its info field) in a double linked list. */
void InsertAfterNodeInDLL (DNode *start, int data, int item)
{
DNode *newNode, *ptr1, *ptr2;;
/* Step 1: Move a pointer “ptr1” to the node containing “data” (i.e., the node after which a new
node is to be inserted), and another pointer “ptr2” to its next node (i.e., ptr1->next). */
ptr1 = start;
while((ptr1->info != data)&&(ptr1->next != NULL))
{
ptr1 = ptr1->next;
}
if(ptr1->next == NULL)
{
printf("\n%d is not found in the linked list.", data);
exot(0);
}
ptr2 = ptr1->next;
[Cont.]
printf("A new node with info field %d added after the node
containing %d in the linked list.\n ", newNode->info, ptr1->info);
}
◼ [NOTE] - Considerations while deleting the first and the last nodes from a
double linked list:
➢ If the linked list contains just one node, then after deletion of the node,
the linked list shall be empty. So, in such case, after deletion of the
node, “start” should point to NULL.
➢ When the node is deleted its memory space must be freed (released).
return start;
}
◼ Technique:
① Move a pointer “ptr1” to the last but one node, and another pointer “ptr2” to the last
node.
② Store “NULL” in “ptr1->next”.
③ Release the node pointed out by “ptr2”.
return start;
}
◼ Technique:
① Move a pointer “ptr1” to the node containing “data” (i.e., whose next node is to be
deleted), a pointer “ptr2” to the node which is to be deleted (i.e., ptr1->next), and one
more pointer “ptr3” to it next node (i.e., to ptr2->next).
② Point “ptr3->prev” to “ptr1”.
③ Point “ptr1->next” to “ptr3”.
④ Release the node pointed out by “ptr2”.
Dr. N. K. Pani, Dept. of CSEA, IGIT Sarang | 12.95
◼ C Function:
/* Function to delete an intermediate node after a given node (a node that contains “data” in its info
field) in a double linked list. */
void DeleteAfterNodeInDLL (DNode *start, int data)
{
DNode *ptr1, *ptr2, *ptr3;
/* Step 1: Move a pointer “ptr1” to the node containing “data” (i.e., whose next node is to be
deleted), a pointer “ptr2” to the node which is to be deleted (i.e., ptr1->next), and one more pointer
“ptr3” to it next node (i.e., to ptr2->next). */
ptr1 = start;
while((ptr1->info != data)&&(ptr1->next != NULL))
{
ptr1 = ptr1->next;
}
if(ptr1->next == NULL)
{
printf("\n%d is not found in the linked list.", data);
exit(0);
}
ptr2 = ptr1->next;
ptr3 = ptr2->next;
◼ Technique: The process is exactly same as in the case of single linked lists.
Move a pointer “ptr” from “start” to the last node till the “value” is not found in the “info”
fields of one of the nodes. If the “value” is found, display the “node number”, otherwise
display “value not found”.
ptr = start;
while(ptr != NULL)
{
if(ptr->info == item)
{
printf("\n%d is found at node %d.", item, count);
exit(0);
}
count++;
ptr = ptr->next;
}
printf("\n%d is not found in the linked list.", item);
}
◼ Variations:
➢ Single Circular Linked Lists: It is a single linked list in which, the “next”
field of the last node points back to the first node to form a circle instead
of pointing to NULL.
The technique of these operations is similar to that of the single / double linked
lists, with just one addition. Here, we also have to manipulate the extra pointers
in the first and the last nodes (wherever applicable).
◼ Variations:
➢ Single linked list
with a header node:
➢ Single circular
linked list
with a header node:
p(x) = σ𝑛
𝑖=0 𝑎𝑖 𝑥𝑖 e.g., p(x) = 37𝑥 6 + 21𝑥 4 + 10𝑥 2 + 3
is represented as:
is represented as: