forked from Romeo-Aryal/C-program-fest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularLL.c
More file actions
125 lines (125 loc) · 2.53 KB
/
circularLL.c
File metadata and controls
125 lines (125 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
}*head=NULL;
void input(){
struct Node *tail=NULL;
int class=1;
while(class!=0){
struct Node *newnode=malloc(sizeof(struct Node));
int num;
printf("Enter the element\n");
scanf("%d",&num);
newnode->data=num;
if(head==NULL){
head=tail=newnode;
head->next=tail;}
else{
tail->next=newnode;
tail=newnode;
tail->next=head;
}
printf("If you want to stop the list press '0' otherwise press any number\n");
scanf("%d",&class);
}
}
void display(struct Node *p){
do{
printf("%d ",p->data);
p=p->next;
}while(p!=head);
}
void insert(struct Node *p){
int pos,n=1;
while(p->next!=head){
p=p->next;
n++;
}
printf("Enter the position where you want to insert\n");
scanf("%d",&pos);
if(pos<1||pos>n){
return;
}
struct Node *newnode=malloc(sizeof(struct Node));
int input;
printf("Enter the input to insert in the list\n");
scanf("%d",&input);
newnode->data=input;
if(pos==1){
if(head==NULL){
head=newnode;
head->next=newnode;
}
else{
while(p->next!=head) p=p->next;
p->next=newnode;
newnode->next=head;
head=newnode;
}
}
else{
for(int i=0;i<pos-1;i++){
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
}
}
int deletionNum(struct Node *p){
struct Node *q=head;
int n=1,x;
while(p->next!=head){
p=p->next;
n++;
}
int pos;
printf("\nEnter the position where you want to delete the number\n");
scanf("%d",&pos);
if(pos<1 || pos>n)
return -1;
if(pos==1){
while(p->next!=head) p=p->next;
x=head->data;
if(head==p){
free(head);
head=NULL;
}
else{
p->next=head->next;
free(head);
head=p->next;
return x;}
}
else{
for(int i=0;i<pos;i++){
q=p;
p=p->next;
}
x=p->data;
q->next=p->next;
free(p);
return x;
}
}
void reverse(struct Node *p){
struct Node *q=NULL,*r=NULL;
do{r=q;
q=p;
p=p->next;
q->next=r;
}while(p!=head);
head=q;
}
int main(){
input();
display(head);
printf("\n");
//insert(head);
// printf("Element which deleted in the list is %d",deletionNum(head));
// printf("\n");
reverse(head);
display(head);
return 0;
}