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

circular_ll

The document is a C program that implements a circular linked list with functions to insert and delete nodes at the beginning, end, and specific positions. It includes a display function to print the list's contents and demonstrates the functionality in the main function. The program manages memory allocation and deallocation for the nodes appropriately.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

circular_ll

The document is a C program that implements a circular linked list with functions to insert and delete nodes at the beginning, end, and specific positions. It includes a display function to print the list's contents and demonstrates the functionality in the main function. The program manages memory allocation and deallocation for the nodes appropriately.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head=NULL;
void insert_begin(int item){
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data=item;
if (head==NULL){
head=temp;
temp->next=temp;
return;
}
{
struct node *q=head;
while(q->next!=head){
q=q->next;
}
q->next=temp;
}
temp->next=head;
head=temp;
}
void insert_end(int item){
struct node*temp=(struct node *)malloc(sizeof(struct node));
temp->data=item;
if(head==NULL){
head=temp;
temp->next=head;
return;
}
struct node *q=head;
while(q->next!=head){
q=q->next;
}
q->next=temp;
temp->next=head;
}
void insert_pos(int item,int pos){
struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *q=head;
if(head==NULL && pos>1){
printf("invalid position\n");
return;
}
temp->data=item;
int i=1;
while(i<pos-1){
q=q->next;
i++;
}
temp->next=q->next;
q->next=temp;
}
void delete_begin(){
if (head==NULL){
printf("empty liked list");
return;
}
struct node *temp=head;
struct node *q=head;
while(q->next!=head)
q=q->next;
q->next=head->next;
head=head->next;
printf("%d is deleted\n",temp->data);
free(temp);
}
void delete_end(){
if (head==NULL){
printf("empty liked list");
return;
}
if (head->next == head) {
printf("%d is deleted\n", head->data);
free(head);
head = NULL;
return;
}
struct node *q=head;
while (q->next->next!=head)
q=q->next;
struct node *temp=q->next;
q->next=head;
printf("%d is deleted\n",temp->data);
free(temp);
}
void delete_pos(int pos){
struct node *q=head,*temp;
int i=1;
while(i<pos-1 && q->next!=head){
q=q->next;
i++;
}
temp=q->next;
q->next=q->next->next;
printf("%d is deleted\n",temp->data);
free(temp);

}
void display(){
if (head==NULL)
printf("empty.........");
struct node *q=head->next;
printf("%d ",head->data);
while(q!=head){
printf("%d ",q->data);
q=q->next;
}
printf("\n");
}
int main()
{ insert_begin(3);
display();
insert_end(5);
display();
insert_pos(3,3);
display();
insert_begin(7);
insert_end(9);
display();

delete_begin();
delete_end();
delete_pos(2);
display();
return 0;
}

You might also like