0% found this document useful (0 votes)
8 views7 pages

Program - 6 (C) Sameer

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)
8 views7 pages

Program - 6 (C) Sameer

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

Program 6(c)

Program Name – Circular linked list Name -Sameer Maurya


Domain – Linked list Roll no- 2300320130215
Problem statement – Write a program for Circular linked list implementation
Code :-

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
};
struct node*start = NULL;
void create();
void traverse();
void insert_beg();
void insert_last();
void insert_after_specific_position();
void delete_first();
void delete_last();
void delete_specific();
void count_nodes();

int main(){
int ch;
char choice;
while(1)
{
printf("Enter the linked list operation\n");
printf("1.create list\n");
printf("2.Display\n");
printf("3.Insert at begin\n");
printf("4.Insert at the last\n");
printf("5.Insert after the specific position\n");
printf("6.Delete at the first\n");
printf("7.Delete at the last\n");
printf("8.Delete at the specific position\n");
printf("9.Exit\n");
printf("10.Count\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
Program 6(c)
Program Name – Circular linked list Name -Sameer Maurya
Domain – Linked list Roll no- 2300320130215
Problem statement – Write a program for Circular linked list implementation
Code :-

case 2: traverse();
break;
case 3: insert_beg();
break;
case 4: insert_last();
break;
case 5: insert_after_specific_position();
break;
case 6: delete_first();
break;
case 7:delete_last();
break;
case 8: delete_specific();
break;
case 10: count_nodes();
break;
case 9: exit(1);
default: printf("Entered invalid choice:-");
}
}
return 0;
}

void create(){
char ch;
struct node*temp = (struct node*)malloc(sizeof (struct node*));

printf("Enter the data");


scanf("%d",&temp->info);
temp->next=temp;
start=temp;
do{
struct node*temp1=(struct node*)malloc(sizeof(struct node*));
printf("Enter the next Data ");
scanf("%d",&temp1->info);
temp->next=temp1;
temp=temp1;
printf("do you want to add more");
scanf("%c",&ch);
}while(ch=='y');
Program 6(c)
Program Name – Circular linked list Name -Sameer Maurya
Domain – Linked list Roll no- 2300320130215
Problem statement – Write a program for Circular linked list implementation
Code :-

temp->next=start;

}
void traverse(){
struct node*temp = start;
if(start==NULL)
{
printf("linklist is empty");
}
while(temp->next!=start){
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d ",temp->info);
}

void insert_beg(){
struct node*q=start;
struct node*temp=(struct node*)malloc(sizeof (struct node*));
printf("enter data");
scanf("%d",&temp->info);
if(start==NULL)
{
temp->next=temp;
start = temp;
}
else{
temp->next = start;
while(q->next!=start)
{
q=q->next;
}
q->next=temp;
start=temp;
}
}
void insert_last(){
struct node*q=start;
struct node*temp=(struct node*)malloc(sizeof (struct node*));
printf("enter data");
Program 6(c)
Program Name – Circular linked list Name -Sameer Maurya
Domain – Linked list Roll no- 2300320130215
Problem statement – Write a program for Circular linked list implementation
Code :-

scanf("%d",&temp->info);
if(start==NULL)
{
temp->next=temp;
start=temp;
}
else{
while(q->next!=start)
{
q=q->next;
}
q->next=temp;
temp->next=start;
}
}

void insert_after_specific_position(){
struct node*q=start;
struct node*temp=(struct node*)malloc(sizeof (struct node*));
int x;
printf("enter node");
scanf("%d",&temp->info);
printf("enter node after which do you want to insert");
scanf("%d",&x);
while(q!=NULL && q->info!=x){
q=q->next;
}
if(q==NULL){
printf("node with value %d not found\n",x);
free(temp);
return;
}
temp->next=q->next;
q->next=temp;
}
void delete_first(){
if(start==NULL){
printf("list is empty");
}
else{
Program 6(c)
Program Name – Circular linked list Name -Sameer Maurya
Domain – Linked list Roll no- 2300320130215
Problem statement – Write a program for Circular linked list implementation
Code :-

struct node*temp=start;
struct node*q=start;
while(q->next!=start)
{
q=q->next;
}
start=temp->next;
q->next=start;
free(temp);
}
}
void delete_last(){
if(start==NULL)
{
printf("list is empty");
}
else{
struct node*q=start;
while(q->next->next!=start)
{
q=q->next;
}
free(q->next);
q->next=start;
}
}

void delete_specific(){
struct node*q=start;
struct node*temp;
int x;
printf("Enter Which do you want to delete");
scanf("%d",&x);
while(q->next->info!=x){
q=q->next;
}
temp=q->next;
q->next=q->next->next;
free(temp);
}
Program 6(c)
Program Name – Circular linked list Name -Sameer Maurya
Domain – Linked list Roll no- 2300320130215
Problem statement – Write a program for Circular linked list implementation
Code :-

void count_nodes()
{
int count=0;
struct node*temp=start;
if(start==NULL)
{
printf("list is empty");
}
else{
do
{
count++;
temp=temp->next;
} while(temp!=start);
}
printf("total nodes=%d",count);
}
Time complexity :- O(n)
Space complexity :- O(n)
Program 6(c)
Program Name – Circular linked list Name -Sameer Maurya
Domain – Linked list Roll no- 2300320130215
Problem statement – Write a program for Circular linked list implementation

Output:-

You might also like