0% found this document useful (0 votes)
2 views

Module 1

This document provides an overview of data structures, including their definitions, classifications, and operations. It covers primitive and non-primitive data structures, with examples such as arrays and linked lists, detailing their properties, advantages, and disadvantages. Additionally, it explains operations like insertion, deletion, and traversal for both arrays and linked lists, along with code examples for practical understanding.

Uploaded by

joel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 1

This document provides an overview of data structures, including their definitions, classifications, and operations. It covers primitive and non-primitive data structures, with examples such as arrays and linked lists, detailing their properties, advantages, and disadvantages. Additionally, it explains operations like insertion, deletion, and traversal for both arrays and linked lists, along with code examples for practical understanding.

Uploaded by

joel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

DATA STRUCTURES

MODULE 1

INTRODUCTION ON DATA STRUCTURES

● 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.

DATA STRUCTURE CLASSIFICATION

PRIMITIVE DATA STRUCTURE

● 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 STRUCTURE

● 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.

1. LINEAR DATA STRUCTURE


● Elements of Linear data structure are accessed in a sequential manner, however the elements can be
stored in these data structure in any order. Examples of linear data structure are: Linked List, Stack,
Queue and Array.
2. NON-LINEAR DATA STRUCTURE
● This data structure does not form a sequence i.e. each item or element is connected with two or more
other items in a non-linear arrangement. The data elements are not arranged in sequential structure.
Examples of non-linear data structures are Trees and Graphs.

Static Data Structure


In Static data structure the size of the structure is fixed.
Example of Static Data Structures: Array
Dynamic Data Structure
In Dynamic data structure the size of the structure is not fixed and can be modified during the operations
performed on it.
Example of Dynamic Data Structures: Linked List

DATA STRUCTURES OPERATIONS


The data appearing in data structures are processed by means of certain operations.
The following are the operations:
1. Traversing: accessing each record/node exactly once so that certain items in the record may
be processed. (This accessing and processing is sometimes called “visiting” the record.)
2. Searching: Finding the location of the desired node with a given key value, or finding the
locations of all such nodes which satisfy one or more conditions.
3. Inserting: Adding a new node/record to the structure.
4. Deleting: Removing a node/record from the structure.
5. Sorting: Arranging the records in some logical order (e.g., alphabetically according to some
NAME key, or in numerical order according to some NUMBER key, such as social security
number or account number)
6. Merging: Combining the records in two different sorted files into a single sorted file.

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 -

● Index starts with 0.

● The array's length is 10, which means we can store 10 elements.

● Each element in the array can be accessed via its index.


Memory allocation of an array

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

● Traversal - This operation is used to print the elements of the array.

● Insertion - It is used to add an element at a particular index.

● Deletion - It is used to delete an element from a particular index.

● Search - It is used to search an element using the given index or by the value.

● Update - It updates an element at a particular index.

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];

Access data in a 2D array


● We can access the individual cells in a 2D array by using the indices of the cells. There are two
indices attached to a particular cell, one is its row number while the other is its column number.

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).

Memory Representation of 2D Array

There are two main techniques of storing 2D array elements into memory

1. Row Major ordering

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.

2. Column Major ordering

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

locations. The elements in a linked list are linked using pointers.

• In simple words, a linked list consists of nodes where each node contains a data field and a

reference(link) to the next node in the list.

● 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

address of the next node in the memory.

• The last node of the list contains pointer to the null.


Advantages of linked lists:

• 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

whenever it is required and it is de-allocated (removed) when it is no longer needed.

• Insertion and Deletions are easier and efficient.

• Linked lists provide flexibility

• 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.

Disadvantages of linked lists

• It consumes more space because every node requires a additional pointer to store address of the next

node.

• Searching a particular element in list is difficult and also time consuming.

Types of Linked Lists:

• Basically we can put linked lists into the following four items:

1. Single Linked List.

2. Double Linked List.

3. Circular Linked List.

4. Circular Double Linked List.


● Singly-linked list - Singly linked list can be defined as the collection of an ordered set of elements. A node
in the singly linked list consists of two parts: data part and link part. Data part of the node stores actual
information that is to be represented by the node, while the link part of the node stores the address of its
immediate successor.

● 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.

Singly Linked List

● 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.

• A node in the singly linked list consist of two parts:

a. data part and

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.

• Head indicates the first node.

• Link or Next of last node is NULL.

Operations on Singly Linked List

In a single linked list we perform the following operations...

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...

• Inserting At Beginning of the list

• Inserting At End of the list

• Inserting At Specific location in the list

Insertion in singly linked list at beginning

● 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.

newnode = (struct node *) malloc(sizeof(struct node *));

newnode → data = item

● 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

Insertion in singly linked list at the end

In order to insert a node at the last, there are two following scenarios which need to be mentioned.

1. The node is being added to an empty list

2. The node is being added to the end of the linked list

in the first case,

● 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;

newnode -> next = NULL;

● 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;

In the second case,

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:

while (temp→ next != NULL)

temp = temp → next;

● 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;

while (temp -> next != NULL)

temp = temp -> next;

temp->next = newnode;

newnode->next = NULL;
Temp->next=newnode;
Newnode->next=NULL;

Insertion in singly 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 = (struct node *) malloc (sizeof(struct node));

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;

Deletion in singly linked list at beginning


o Deleting a node from the beginning of the list is the simplest operation of all. It just need a few
adjustments in the node pointers. Since the first node of the list is to be deleted, therefore, we just need
to make the head, point to the next of the head. This will be done by using the following statements.

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);

Deletion in singly linked list at the end


o For this purpose, just declare a temporary pointer temp and assign it to head of the list. We also need to
keep track of the second last node of the list. For this purpose, prevnode will be used where temp will
point to the last node and prevnode will point to the second last node of the list.

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);

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);

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;
}
}

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:
1. node data,
2. pointer to the next node in sequence (next pointer) ,
3. pointer to the previous node (previous pointer).
Advantages of doubly linked list
1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
3) We can quickly insert a new node before a given node.

Operations on Doubly Linked list

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

Inserting At Beginning of the list


Step 1: Create a newNode with given value and newNode → previous = NULL.
Step 2: Check whether list is Empty (head == NULL)
Step 3: If it is Empty then, newNode → next =NULL and head=newNode .
Step 4: If it is not Empty then, newNode → next = head
head->prev=newnode and head=newNode

Newnode->prev=NULL
newnode->next=head;
head->prev=newnode;
head=newnode;

Inserting At End of the list

Step 1: Create a newNode with given value and newNode → next = NULL.

Step 2: Check whether list is Empty (head == NULL)

Step 3: If it is Empty then, newNode → previous =NULL and head=newNode .

Step 4: If it is not Empty,

tail→ next=new node

newNode → prev=tail
tail=newnode

Tail

tail->next=newnode;
newnode->prev=tail;
tail=newnode;

Inserting At Specific location in the list

Step 1: Create a newNode with given value.

Step 2: define node pointers temp and temp= head.

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...

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

Step 1: Check whether list is Empty (head == NULL)

Step 2: If it is Empty then, display 'List is Empty!

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.

Deleting from End of the list

Step 1: Check whether list is Empty (head == NULL)

Step 2: If it is Empty then, display 'List is Empty!

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

Deleting a Specific Node from the list

Step 1: Check whether list is Empty (head == NULL)

Step 2: If it is Empty then, display 'List is Empty!

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);

Displaying a Double Linked List

Step 1: Check whether list is Empty (head == NULL)

Step 2: If it is Empty, then display 'List is Empty!!!

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 1: Check whether list is Empty (head == NULL)

Step 2: If it is Empty, then display 'List is Empty!!!

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

struct node *prev;

int data;

struct node *next;

}*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

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:createdll();

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;

newnode->prev=NULL;

if(head==NULL)

head=newnode;

else

newnode->next=head;

head->prev=newnode;

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;

newnode->prev=NULL;

if(head==NULL)

head=newnode;

else

tail->next=newnode;

newnode->prev=tail;

tail=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;

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);

printf("The node is deleted\n");

void deletelast()

if(tail==NULL)

printf("nothing to delete");

else

temp=tail;
tail=tail->prev;

tail->next=NULL;

free(temp);

printf("The 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++)

temp=temp->next;

temp->next->prev=temp->prev;

temp->prev->next=temp->next;

free(temp);

printf("node deleted\n");
}

void createdll()

int x;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\n enter the data ");

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

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

to stop when the first visited node is visited again.

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...

• Inserting At Beginning of the list

• Inserting At End of the list

• Inserting At Specific location in the list

Inserting At Beginning of the list

Step 1: Create a newNode with given value.

Step 2: Check whether list is Empty (head == NULL)

Step 3: If it is Empty then, set head= newNode and newNode→next = head

Step 4: If it is Not Empty then, newNode->next=head


head=newNode;

tail->next=head;

tail

newnode->next=head;
head=newnode;
tail->next=head;

Inserting At End of the list

Step 1: Create a newNode

Step 2: Check whether list is Empty (head == NULL).

Step 3: If it is Empty then, set head =last= newNode and newNode → next = head.

Step 4: If it is Not Empty then, tail->next=newNode

Step 6: Set tail = newNode and tail → 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 = (struct node *) malloc (sizeof(struct node));

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

Step 1: Check whether list is Empty (head == NULL)


Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible'
Step 3: Assign a Temporary pointer temp=head;
head=head->next;
tail->next=head;
free(temp);

tail

temp

head=head->next;
tail->next=head;
free(temp);

Deleting from End of the list

Step 1: Check whether list is Empty (head == NULL)

Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible'

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;

Step 5:set prevnode->next=head;

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;

struct node *next;

}*head,*temp,*newnode,*

tail,*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.Exit9.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:createcll();

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;

tail->next=head;

void display()

printf("\n The linked list

\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));

printf("\n enter the data at

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));

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=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()

struct node *prevnode;

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\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;
}
}

You might also like