Module 1
Module 1
MODULE 1
● A data structure is a special way of organizing and storing data in a computer so that it can be
used efficiently.
● Example:Array, Linked List, Stack, Queue, Tree, Graph etc Each of these mentioned data
structures has a different special way of organizing data so we choose the data structure based on
the requirement.
● Primitive data structures are the fundamental data structures. It can be operated directly on
the data and machine instructions.
Example: integer, real, character, floating point number, and pointer.
● These data types consists of characters that cannot be divided and hence they also called simple
data types.
● Non-primitive data structures are the data structures that are created using the primitive data
structures. Non-primitive data structures are classified into two categories
1. Linear data structures
2. Non-Linear data structures.
ARRAY
● Arrays are defined as the collection of similar types of data items stored at contiguous memory locations.
● It is one of the simplest data structures where each data element can be randomly accessed by using its
index number.
Properties of array
● Each element in an array is of the same data type and carries the same size that is 4 bytes.
● Elements in the array are stored at contiguous memory locations from which the first element is stored at the
smallest memory location.
● Elements of the array can be randomly accessed since we can calculate the address of each element of the
array with the given base address and the size of the data element.
Representation of an array
As per the above illustration, there are some of the following important points -
In the above image, we have shown the memory allocation of an array arr of size 5. The array follows a 0-
based indexing approach. The base address of the array is 100 bytes. It is the address of arr[0]. Here, the
size of the data type used is 4 bytes; therefore, each element will take 4 bytes in the memory.
Basic operations
● Search - It is used to search an element using the given index or by the value.
Array Insertion
This operation is performed to insert one or more elements into the array.
Program
include<stdio.h>
void main()
{
int a[10],value,pos,size,i;
printf("\n enter the number of elements in the array");
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("enter the position to insert the element");
scanf("%d",&pos);
printf("\n enter the value");
scanf("%d",&value);
for(i=size;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=value;
size++;
for(i=0;i<size;i++)
{
printf("%d\n",a[i]);
}
}
Deletion operation
This operation removes an element from the array and then reorganizes all of the array elements.
#include<stdio.h>
void main()
{
int a[10],value,pos,size,i;
printf("\n enter the number of elements in the array");
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("enter the position to delete the element");
scanf("%d",&pos);
for(i=pos-1;i<=size-1;i++)
{
a[i]=a[i+1];
}
size--;
for(i=0;i<size;i++)
{
printf("%d\n",a[i]);
}
}
Advantages of Array
● Array provides the single name for the group of variables of the same type. Therefore, it is easy to
remember the name of all the elements of an array.
● Traversing an array is a very simple process; we just need to increment the base address of the array in
order to visit each element one by one.
● Any element in the array can be directly accessed by using the index.
Disadvantages of Array
● Array is homogenous. It means that the elements with similar data type can be stored in it.
● In array, there is static memory allocation that is size of an array cannot be altered.
● There will be wastage of memory if we store less number of elements than the declared size.
2D Array
● 2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be
represented as the collection of rows and columns.
Declare 2D Array
int arr[rows][columns];
int x = a[i][j];
where i and j is the row and column number of the cell respectively.
Initializing 2D Arrays
int arr[2][2] = {0,1,2,3};
● The number of elements that can be present in a 2D array will always be equal to (number of
rows * number of columns).
There are two main techniques of storing 2D array elements into memory
In row major ordering, all the rows of the 2D array are stored into the memory contiguously.
Considering the array shown in the above image, its memory allocation according to row major
order is shown as follows.
first, the 1st row of the array is stored into the memory completely, then the 2 nd row of the array is
stored into the memory completely and so on till the last row.
According to the column major ordering, all the columns of the 2D array are stored into the
memory contiguously. The memory allocation of the array which is shown in in the above image is
given as follows.
first, the 1st column of the array is stored into the memory completely, then the 2 nd row of the array
is stored into the memory completely and so on till the last column of the array.
sparse matrix
● Sparse matrices are those matrices that have the majority of their elements equal to zero. In other
words, the sparse matrix can be defined as the matrix that has a greater number of zero elements
than the non-zero elements.
Linked List
● A linked list is a linear data structure, in which the elements are not stored at contiguous memory
• In simple words, a linked list consists of nodes where each node contains a data field and a
● Linked List can be defined as collection of objects called nodes that are randomly stored in the
memory.
• A node contains two fields i.e. data stored at that particular address and the pointer which contains the
• Linked lists are dynamic data structures. i.e., they can grow or shrink during the execution of a program.
• Linked lists have efficient memory utilization. Here, memory is not pre-allocated. Memory is allocated
• In inserting a data item at a specified position and deletion of the data item from the given position.
• Many complex applications can be easily carried out with linked lists.
• It consumes more space because every node requires a additional pointer to store address of the next
node.
• Basically we can put linked lists into the following four items:
● Doubly linked list - Doubly linked list is a complex type of linked list in which a node contains a pointer to
the previous as well as the next node in the sequence. Therefore, in a doubly-linked list, a node consists of
three parts: node data, pointer to the next node in sequence (next pointer), and pointer to the previous
node (previous pointer).
● Circular singly linked list - In a circular singly linked list, the last node of the list contains a pointer to the
first node of the list. We can have circular singly linked list as well as circular doubly linked list.
● Circular doubly linked list - Circular doubly linked list is a more complex type of data structure in which a
node contains pointers to its previous node as well as the next node. Circular doubly linked list doesn't
contain NULL in any of the nodes. The last node of the list contains the address of the first node of the list.
The first node of the list also contains the address of the last node in its previous pointer.
● A single linked list is one in which all nodes are linked together in some sequential manner. Hence, it is
also called as linear linked list.
Singly linked list can be defined as the collection of ordered set of elements.
b. link part.
Data part of the node stores actual information that is to be represented by the node
• The link part of the node stores the address of its immediate successor.
• One way chain or singly linked list can be traversed only in one direction. In other words, we can say
that each node contains only next pointer, therefore we can not traverse the list in the reverse direction.
1. Insertion
2. Deletion
3. Display
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...
● Allocate the space for the new node and store data into the data part of the node. This will be done by
the following statements.
● Make the link part of the new node pointing to the existing first node of the list. This will be done by using the
following statement.
newnode->next = head;
● At the last, we need to make the new node as the first node of the list this will be done by using the
following statement.
head = newnode;
newnode->next=head;
head=newnode
In order to insert a node at the last, there are two following scenarios which need to be mentioned.
● The condition (head == NULL) gets satisfied. Hence, we just need to allocate the space for the
node by using malloc statement in C. Data and the link part of the node are set up by using the
following statements.
newnode->data = item;
● Since, newnode is the only node that will be inserted in the list hence, we need to make this node
pointed by the head pointer of the list. This will be done by using the following Statements.
Head = newnode;
The condition Head = NULL would fail, since Head is not null. Now, we need to declare a
temporary pointer temp in order to traverse through the list. temp is made to point the first node of
the list.
Temp = head
● Then, traverse through the entire linked list using the statements:
● At the end of the loop, the temp will be pointing to the last node of the list. Now, allocate the space for
the new node, and assign the item to its data part. Since, the new node is going to be the last node of
the list hence, the next part of this node needs to be pointing to the null. We need to make the next
part of the temp node (which is currently the last node of the list) point to the new node
Temp = head;
temp->next = newnode;
newnode->next = NULL;
Temp->next=newnode;
Newnode->next=NULL;
In order to insert an element after the specified number of nodes into the linked list, we need to skip the
desired number of elements in the list to move the pointer at the position after which the node will be
inserted. This will be done by using the following statements
temp=head;
for(i=0;i<pos;i++)
{
temp = temp->next;
}
o Allocate the space for the new node and add the item to the data part of it. This will be done by
using the following statements.
newnode->data = item;
o Now, we just need to make a few more link adjustments and our node at will be inserted at the specified
position. Since, at the end of the loop, the loop pointer temp would be pointing to the node after which the
new node will be inserted. Therefore, the next part of the new node must contain the address of the next
part of the temp.
newnode→ next = temp → next;
temp ->next = newnode;
Newnode->next=temp->next;
Temp->next-newnode;
temp= head;
head = head->next;
Now, free the pointer temp which was pointing to the head node of the list. This will be done by using the
following statement.
free(temp) ;
temp
mp Temp=head;
Head=head->next;
Free(temp);
temp = head;
while(temp->next != NULL)
{
prevnode = temp;
temp=temp->next;
}
o Now, we just need to make the pointer prevnode point to the NULL and the last node of the list that is
pointed by temp will become free. It will be done by using the following statements.
prevnode->next = NULL;
free(temp);
prevnode temp
Prevnode->next=NULL;
Free(temp);
o In order to delete the node, which is present after the specified node, we need to skip the desired
number of nodes to reach the node after which the node will be deleted. We need to keep track of the
two nodes. The one which is to be deleted the other one if the node which is present before that node.
For this purpose, two pointers are used: temp and prevnode.
temp=head;
for(i=0;i<pos;i++)
{
prevnode= temp;
temp= temp->next;
o Make the next of prevnode (points to the specified node) point to the next of temp (the node which is to
be deleted).
prevnode->next = temp->next;
free(temp);
prevnode temp
Prevnode->next=temp->next;
Free(temp);
Program:
#include<stdio.h>
#include<stdlib.h>
void display();
void insertf();
void insertl();
void insertmid();
void deletefront();
void deletelast();
void deletemid();
void sl();
struct node
{
int data;
struct node *next;
}*head,*temp,*newnode,*prevnode;
int main()
{
int c;
printf("menu\n");
printf("\n 1.Insertf 2.Insertl 3.Insertmid 4.deletefront 5.deletelast 6.deletemid 7.display 8.Exit 9.create\n");
do
{
printf("enter your choice");
scanf("%d",&c);
switch(c)
{
case 1:insertf();
break;
case 2:insertl();
break;
case 3:insertmid();
break;
case 4:deletefront();
break;
case 5:deletelast();
break;
case 6:deletemid();
break;
case 7:display();
break;
case 8:exit(0);
case 9:sl();
break;
}
}while(1);
return 0;
}
void insertf()
{
int x;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data at begnning");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}
void display()
{
printf("\n The linked list \n");
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
void insertl()
{
int x;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data at end");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
{
head=temp=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void insertmid()
{
int x,pos;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data ");
scanf("%d",&x);
printf("enter the position you want to insert");
scanf("%d",&pos);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
{
head=temp=newnode;
}
else
{
temp=head;
for(int i=1;i<pos-1;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
}
}
void deletefront()
{
if(head==NULL)
{
printf("nothing to delete");
}
else
{
temp=head;
head=head->next;
free(temp);
printf("The node is deleted\n");
}
}
void deletelast()
{
if(head==NULL)
{
printf("nothing to delete");
}
else
{
temp=head;
while(temp->next!=0)
{
prevnode=temp;
temp=temp->next;
}
prevnode->next=NULL;
free(temp);
printf("Node is deleted\n");
}
}
void deletemid()
{
int i,pos;
printf("enter the position to delete");
scanf("%d",&pos);
if(head==NULL)
{
printf("nothing to delete");
}
else
{
temp=head;
for(i=1;i<pos;i++)
{
prevnode=temp;
temp=temp->next;
}
prevnode->next=temp->next;
free(temp);
printf("node deleted");
}
}
void sl()
{ int x;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n enter the data ");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
1. Insertion
2. Deletion
3. Display
Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Newnode->prev=NULL
newnode->next=head;
head->prev=newnode;
head=newnode;
Step 1: Create a newNode with given value and newNode → next = NULL.
newNode → prev=tail
tail=newnode
Tail
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
Step 5: Keep moving the temp to its next node until position- 1
Step 7: Assign
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
newnode->next->prev=newnode;
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
newnode->next->prev=newnode;
Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...
Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4: If it is FALSE, then assign head=head→ next and head → prev=NULL and delete temp.
Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with tail.
Step 4: Assign tail=tail->previous and tail→ next =NULL and last delete temp
Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4: Keep moving the temp until it reaches to the position to delete the node
Step 5:Then,
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
free(temp);
Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4: Keep displaying temp → data until temp reaches to the last node
Displaying a Double Linked List
Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4: Keep displaying temp → data until temp reaches to the last node
Program
#include<stdio.h>
#include<stdlib.h>
void display();
void insertf();
void insertl();
void insertmid();
void deletefront();
void deletelast();
void deletemid();
void createdll();
struct node
int data;
}*head,*temp,*newnode,*tail;
int main()
int c;
printf("menu\n");
printf("\n 1.Insertf 2.Insertl 3.Insertmid 4.deletefront 5.deletelast 6.deletemid 7.display 8.Exit9.create \n");
do
scanf("%d",&c);
switch(c)
case 1:insertf();
break;
case 2:insertl();
break;
case 3:insertmid();
break;
case 4:deletefront();
break;
case 5:deletelast();
break;
case 6:deletemid();
break;
case 7:display();
break;
case 8:exit(0);
case 9:createdll();
break;
}while(1);
return 0;
}
void insertf()
int x;
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=newnode;
else
newnode->next=head;
head->prev=newnode;
head=newnode;
void display()
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
void insertl()
int x;
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=newnode;
else
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
void insertmid()
int x,pos;
scanf("%d",&x);
scanf("%d",&pos);
newnode->data=x;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=newnode;
else
temp=head;
for(int i=1;i<pos-1;i++)
temp=temp->next;
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
newnode->next->prev=newnode;
}
void deletefront()
if(head==NULL)
printf("nothing to delete");
else
temp=head;
head=head->next;
head->prev=NULL;
free(temp);
void deletelast()
if(tail==NULL)
printf("nothing to delete");
else
temp=tail;
tail=tail->prev;
tail->next=NULL;
free(temp);
void deletemid()
int i,pos;
scanf("%d",&pos);
if(head==NULL)
printf("nothing to delete");
else
temp=head;
for(i=1;i<pos;i++)
temp=temp->next;
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
free(temp);
printf("node deleted\n");
}
void createdll()
int x;
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=tail=newnode;
else
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
Circular linked list is one, which has no beginning and no end. A single linked list can be made a circular
linked list by simply storing address of the very first node in the link field of the last node.
Advantages of Circular Linked Lists:
1) Any node can be a starting point. We can traverse the whole list by starting from any point. We just need
2)There is no null value present in the next part of any of the nodes.
3) Used in CPU scheduling algorithms.
Operations
• Insertion
• Deletion
• Display
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows...
tail->next=head;
tail
newnode->next=head;
head=newnode;
tail->next=head;
Step 3: If it is Empty then, set head =last= newNode and newNode → next = head.
tail
tail->next=newnode;
tail=newnode;
tail->next=head;
Insertion in circular linked list after specified Node
In order to insert an element after the specified number of nodes into the linked list, we need to skip the
desired number of elements in the list to move the pointer at the position after which the node will be
inserted. This will be done by using the following statements
temp=head;
for(i=0;i<pos;i++)
{
temp = temp->next;
}
o Allocate the space for the new node and add the item to the data part of it. This will be done by
using the following statements.
newnode->data = item;
o Now, we just need to make a few more link adjustments and our node at will be inserted at the specified
position. Since, at the end of the loop, the loop pointer temp would be pointing to the node after which the
new node will be inserted. Therefore, the next part of the new node must contain the address of the next
part of the temp.
newnode→ next = temp → next;
temp ->next = newnode;
Deletion
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
tail
temp
head=head->next;
tail->next=head;
free(temp);
Step 3: If it is FALSE Then, declare a pointer temp and set 'temp = head’
Step 4: move temp to its next node. Repeat the same until temp reaches to the last node in the list.
prevnode=temp;
temp=temp->next;
free(temp);
prevnode temp
prevnode->next=head;
free(temp);
Deletion in singly linked list after the specified node :
o In order to delete the node, which is present after the specified node, we need to skip the desired
number of nodes to reach the node after which the node will be deleted. We need to keep track of the
two nodes. The one which is to be deleted the other one if the node which is present before that node.
For this purpose, two pointers are used: temp and prevnode.
temp=head;
for(i=0;i<pos;i++)
{
prevnode= temp;
temp= temp->next;
o Make the next of prevnode (points to the specified node) point to the next of temp (the node which is to
be deleted).
prevnode->next = temp->next;
free(temp);
Program:
#include<stdio.h>
#include<stdlib.h>
void display();
void insertf();
void insertl();
void insertmid();
void deletefront();
void deletelast();
void deletemid();
void createcll();
struct node
{
int data;
}*head,*temp,*newnode,*
tail,*prevnode;
int main()
int c;
printf("menu\n");
3.Insertmid 4.deletefront
5.deletelast 6.deletemid
7.display 8.Exit9.create
\n");
do
scanf("%d",&c);
switch(c)
case 1:insertf();
break;
case 2:insertl();
break;
case 3:insertmid();
break;
case 4:deletefront();
break;
case 5:deletelast();
break;
case 6:deletemid();
break;
case 7:display();
break;
case 8:exit(0);
case 9:createcll();
break;
}while(1);
return 0;
void insertf()
int x;
newnode=(struct
node*)malloc(sizeof(struct
node));
begnning");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
newnode->next=head;
head=newnode;
tail->next=head;
void display()
\n");
temp=head;
while(temp->next!=head)
printf("%d->",temp-
>data);
temp=temp->next;
printf("%d",temp->data);
}
void insertl()
int x;
newnode=(struct
node*)malloc(sizeof(struct
node));
end");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
tail->next=newnode;
tail=newnode;
tail->next=head;
void insertmid()
int x,pos;
newnode=(struct
node*)malloc(sizeof(struct
node));
scanf("%d",&x);
scanf("%d",&pos);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
temp=head;
for(int i=1;i<pos-1;i++)
temp=temp->next;
newnode->next=temp-
>next;
temp->next=newnode;
}
void deletefront()
if(head==NULL)
printf("nothing to delete");
else
temp=head;
head=head->next;
tail->next=head;
free(temp);
printf("The node is
deleted\n");
void deletelast()
if(tail==NULL)
printf("nothing to delete");
else
temp=head;
while(temp-
>next!=head)
prevnode=temp;
temp=temp->next;
prevnode->next=head;
free(temp);
printf("The node is
deleted\n");
void deletemid()
int i,pos;
for(i=1;i<pos;i++)
{
prevnode=temp;
temp=temp->next;
}
prevnode->next=temp-
>next;
free(temp);
printf("node deleted\n");
}
}
void createcll()
{
int x;
newnode=(struct
node*)malloc(sizeof(struct
node));
printf("\n enter the data ");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
{
head=tail=newnode;
}
else
{
tail->next=newnode;
tail=newnode;
tail->next=head;
}
}