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