0% found this document useful (0 votes)
6 views10 pages

circular_singly_linkedlists

A circular linked list is a non-linear data structure organized in a circular manner, eliminating the need for NULL pointers. It can be singly or doubly circular, with operations including insertion, deletion, searching, and sorting. The document also includes code for implementing a circular linked list in C, detailing functions for various operations and a menu-driven interface.
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)
6 views10 pages

circular_singly_linkedlists

A circular linked list is a non-linear data structure organized in a circular manner, eliminating the need for NULL pointers. It can be singly or doubly circular, with operations including insertion, deletion, searching, and sorting. The document also includes code for implementing a circular linked list in C, detailing functions for various operations and a menu-driven interface.
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/ 10

AIM: : CIRCULAR LINKED LISTS

DESCRIPTION
⚫ A circular linked list is a non -linear data structure where data is organised in a
circular manner.
⚫ The part of the last node of a circular data structure contains an adress of a
head such that it makes a circular.
⚫ A circular list eliminates the need of NULL pointers as the link part of last node
points to head
⚫ They are categorised into singly circular and doubly circular linked lists
⚫ In a singly circular ,traversing is done in forward direction .
OPERATIONS DONE IN SINGLY CIRCULAR LINKED LISTS
1) Create newnode()
2) Insert a node at beginning
3) Insert a node at end
4) Insert a node at specified position
5) Delete a node at beginning
6) Delete a node at end
7) Delete a node at specified position
8) Search a node
9) Replcae
10) Display
11) Sort
12) Reverse
CIRCULAR LINKED LIST REPRESENTATIONS
head end
[20][4012]---->[40][4036]-----[60][4000]
4000 4012 4036

CODE FOR CIRCULAR LINKED LIST


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
struct node *head=NULL,*end=NULL;//firts and last nodes pointers
void display();
void create_node();//function prototypes
void insert_beg();
void insert_end();
void insert_anypos();
void remove_beg();
void remove_end();
void remove_anypos();
void reverse();

1
void search();
void replace();
void sort();
int count=0;//variable for storing the number of nodes
void create_nodes(void)
{
int info;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("\nMemory Full......");
exit(1);
}
printf("\u001B[31m \nEnter Data into Node ");
scanf("%d",&info);
newnode->data=info;
newnode->next=NULL;
if(head==NULL)
{
head=end=newnode;
end->next=head;// circular linked list
}
else
{
end->next=newnode;
end=newnode;
end->next=head;// circular linked list
}
++count;
printf("\u001B[32m \nNode created successifully");
}//end of create node function
void display()
{
struct node *temp;
if(head==NULL)
{
printf("\u001B[34m \n Node not created ");
exit(0);
}
temp=head;
printf("\u001B[33m \nThe List Elements are ");
do
{
printf("%d ",temp->data);
temp=temp->next;
}

2
while(temp!=head);
}//end of display function
void insert_beg(void)
{
int ele;
struct node*newnode= (struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("\nMemory Full......");
exit(0);
}
printf("\u001B[35m \nEnter Data into Node ");
scanf("%d",&ele);
newnode->data=ele;
newnode->next=head;
head=newnode;
end->next=head;
++count;
printf("\u001B[32m \nNode added at beginning successifully");
}
void insert_end()

{
int ele;
struct node* newnode= (struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("\nMemory Full......");
exit(1);
}
printf("\u001B[34m \nEnter Data into Node ");
scanf("%d",&ele);
newnode->data=ele;
end->next=newnode;
newnode->next=head;
end=newnode;
++count;
printf("\u001B[32m \nNode added at end successifully");
}
void insert_any()
{
int infor,pos;
struct node* newnode= (struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("\nMemory Full......");
exit(1);

3
}
printf("\u001B[34m \nEnter Data into Node ");
scanf("%d",&infor);
newnode->data=infor;
newnode->next=NULL;
printf("\u001B[33m \nEnter position ");
scanf("%d",&pos);
if(pos<0 || pos>count)
{
printf("\u001B[35m \n Invalid position");
free(newnode);
exit(1);
}
struct node*temp=head;
for(int i=1;i<pos-1;++i)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
++count;
printf("\u001B[35m \nNewnode added at position %d ",pos);
}
void remove_beg()
{
struct node *temp;
if(head==NULL)
{
printf("\u001B[35m \nNo nodes existing....");
free(head);
exit(1);
}
temp=head;
head=temp->next;
end->next=head;
free(temp);
count--;
printf("\u001B[35m \nFirst node removed ");
}
void remove_end()
{
struct node *temp,*prev;
if(head==NULL)
{
printf("\u001B[35m \nNo nodes existing....");
free(head);
exit(1);

4
}
prev=head;
temp=head;
while(temp->next!=head)
{
prev=temp;
temp=temp->next;
}
prev->next=head;
free(temp);
count--;
printf("\u001B[35m \nLast node removed ");
}
void remove_any()
{
struct node *temp,*prev;
int pos;
if(head==NULL)
{
printf("\u001B[35m \n No nodes existing....");
free(head);
exit(1);
}
printf("\u001B[36m\n Enter position of the node..");
scanf("%d",&pos);
if(pos>count || pos<0)
{
printf("\n Invalid position position ");
}
temp=head;
prev=NULL;
if(pos==1)
{
temp=head;
head=head->next;
}
for(int i=1;i<pos;i++)
{
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
free(temp);
--count;
printf("\u001B[35m \nNode removed at position %d ",pos);
}
void reverse(struct node *head)

5
{
if(head == NULL)
{
free(head);
}
else
{
reverse(head->next);
printf("%d ",head->data);
}
}
void search()
{ int key,flag=0;
struct node *temp;
if(head==NULL)
{
printf("\u001B[35m \nNo nodes existing....");
free(head);
exit(1);
}
printf("\u001B[36m \n Enter element to search.");
scanf("%d",&key);
temp=head;
while(temp!=head)
{
if(temp->data==key)
{
flag=1;
printf("\u001B[33m \n Element found...");
}break;
}
temp=temp->next;
if(flag==0)
printf("\u001B[32m \nElement not found ");
free(temp);
}
void replace()
{ int key,flag=0,rep;
struct node *temp;
if(head==NULL)
{
printf("\u001B[35m \nNo nodes existing....");
free(head);
exit(1);
}
printf("\u001B[36m \n Enter element to be replaced.");
scanf("%d",&key);

6
printf("\u001B[35m \n Enter replacement element.");
scanf("%d",&rep);
temp=head;
while(temp!=head)
{
if(temp->data==key)
{
temp->data=rep;
return;
}
else
{
printf("\u001B[32m \nElement not found ");
}
}
free(temp);
}
void sort()
{
int t;
struct node*temp=head;
struct node *i,*j;
if(head==NULL)
{
printf("\u001B[35m \nNo nodes existing....");
exit(0);
}
for(i=temp;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->data>j->data)
{
t=i->data;
i->data=j->data;
j->data=t;
}
}
}
do
{
printf("%d ", temp->data);
temp=temp->next;
}
while(temp!=head);
}
int main()

7
{
int choice;
do
{
printf("\u001B[35m \n*********MENU********************");
printf("\u001B[36m \u001B[3m \n1. Create_circular \n2. Display \n3. Insert at
Beginning \n4. Insert at End ");
printf("\u001B[36m \u001B[3m \n5. Insert at Any position \n6. Remove first
node \n7. Remove Last Node");
printf("\u001B[36m \u001B[3m \n8. Remove node at specified position
\n9.Number of Nodes \n10.Reversed Elements ");
printf("\u001B[36m \u001B[3m \n11.Search any element.. \n12. Replace any
element \n13. Sort elements \n14. Exit ");
printf("\u001B[32m\nEnter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:create_nodes();
break;
case 2:display();
break;
case 3: insert_beg();
break;
case 4: insert_end();
break;
case 5:insert_any();
break;
case 6: remove_beg();
break;
case 7: remove_end();
break;
case 8:remove_any();
break;
case 9: printf("\u001B[3C\nTotal Nosdes created :%d ",count);
break;
case 10: reverse(head);
break;
case 11:search();break;
case 12:replace();break;
case 13:printf("\n Sorted elements are ");sort();break;
case 14:exit(0);
default:printf("\u001B[3CInvalid Option");
}
}
while(1);
return 0;
}

8
Output
*********MENU********************
1. Create_circular
2. Display
3. Insert at Beginning
4. Insert at End
5. Insert at Any position
6. Remove first node
7. Remove Last Node
8. Remove node at specified position
9.Number of Nodes
10.Reversed Elements
11.Search any element..
12. Replace any element
13. Sort elements
14. Exit
Enter Your Choice:
Enter Your Choice: 1

Enter Data into Node 15

Node created successifully


*********MENU********************
1. Create_circular
2. Display
3. Insert at Beginning
4. Insert at End
5. Insert at Any position
6. Remove first node
7. Remove Last Node
8. Remove node at specified position
9.Number of Nodes
10.Reversed Elements
11.Search any element..
12. Replace any element
13. Sort elements
14. Exit
Enter Your Choice: 2

The List Elements are 12 13 15


*********MENU********************
1. Create_circular
2. Display
3. Insert at Beginning
4. Insert at End
5. Insert at Any position
6. Remove first node

9
7. Remove Last Node
8. Remove node at specified position
9.Number of Nodes
10.Reversed Elements
11.Search any element..
12. Replace any elements
13. Exit
Enter Your Choice:

10

You might also like