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

Circular Linled List - Program

This C program implements a circular linked list with functions for insertion, removal, finding, and displaying nodes. It creates a circular linked list of integer nodes from user input, then demonstrates removing and finding a node by value. The insert function adds nodes to the empty or end of the existing list. RemoveNode deletes nodes by value by updating pointers, and find searches the list and reports if the value is found.

Uploaded by

Gowtham
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)
35 views

Circular Linled List - Program

This C program implements a circular linked list with functions for insertion, removal, finding, and displaying nodes. It creates a circular linked list of integer nodes from user input, then demonstrates removing and finding a node by value. The insert function adds nodes to the empty or end of the existing list. RemoveNode deletes nodes by value by updating pointers, and find searches the list and reports if the value is found.

Uploaded by

Gowtham
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/ 3

/* Program for implementation of circular Linked List with insertion, find, delete and display

operations. */
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *insert(node *p, int n)
{
node *temp;
/* if the existing list is empty then insert a new node as the starting node */
if(p==NULL)
{
p=(node *)malloc(sizeof(node)); /* creates new node data value passes as parameter */
p-> data = n;
p-> next = p; /* makes the pointer pointing to itself because it is a circular list*/
}
else
{
temp = p;
/* traverses the existing list to get the pointer to the last node of it */
while (temp-> next != p)
temp = temp-> next;
temp-> next = (node *)malloc(sizeof(node));
/* creates new node using data value passes as parameter and puts
its address in the next field of last node of the existing list*/
temp = temp-> next;
temp-> data = n;
temp-> next = p;
}
return (p);
}
void printlist ( node *p )
{
node *temp;
temp = p;
printf("\nThe data values in the list are\n");
if(p!= NULL)
{
do
{
printf("%d\t",temp->data);
temp = temp->next;
}
while (temp!= p);
}
else
printf("The list is empty\n");
}
node *removenode(node *list, int value)
{
node *first = list, *last = list, *previous = list;
if (list == NULL)
{
printf("The circularly linked list is already empty!\n");
return;
}
//handle the first element of the list
if (list->data == value)
{
//handle only one element in the list
if (list->next == list)
{
free(list);
return NULL;

}
//get the last element of the list
while(last->next != first) last = last->next;
//connect last element with 2nd element
first = first->next;
last->next = first;
free(list);
return first;
}
//handle the middle elements
while (list->next != first)
{
if (list->data == value)
{
previous->next = list->next;
free(list);
return first;
}
previous = list;
list = list->next;
}
//handle the last element
if (list->data == value)
{
previous->next = first;
free(list);
return first;
}
else
{
//if the element is not found
printf("The element %d has not been found in the list!\n", value);
return first;
}
}
void find(node *list, int value)
{
node *first = list, *previous = list;
if (list == NULL)
{
printf("The circularly linked list is empty!\n");
return;
}
//handle the first element
if (list->data == value)
{
printf("The %d is found at the beginning of the list before %d!\n", value,
list->next->data);
return;
}
//handle the middle elements
while (list->next != first)
{
if (list->data == value)
{
printf("\n%d has been found between %d and %d\n", value, previous->data,
list->next->data);
return;
}
previous = list;
list = list->next;
}
//handle the last element
if (list->data == value)
{
printf("The \n%d is found at the end of the list after %d!\n", value, previous->data);
return;
}
printf("\nThe element %d is not found in the list!\n");

return;
}
void main()
{
int n,j;
int x;
node *start = NULL ;
printf("Enter the nodes to be created \n");
scanf("%d",&n);
while (n-- > 0 )
{
printf( "Enter the data values to be placed in a node\n");
scanf("%d",&x);
start = insert ( start, x );
}
printf("The created list is\n");
printlist ( start );
printf ( "\n enter the element to remove from Linked List\n" ) ;
scanf("%d",&j);
start = removenode(start,j);
printlist ( start );
printf ( "\n enter the element to find from Linked List\n" ) ;
scanf("%d",&j);
find(start,j);
}
/*
C:\Users\Administrator\Desktop\stack>csll-final.exe
Enter the nodes to be created
4
Enter the data values to be placed in a node
1
Enter the data values to be placed in a node
2
Enter the data values to be placed in a node
3
Enter the data values to be placed in a node
4
The created list is
The data values in the list are
1
2
3
4
enter the element to remove from Linked List
1
The data values in the list are
2
3
4
enter the element to find from Linked List
3
3 has been found between 2 and 4
*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy