-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularLL.c
167 lines (152 loc) · 3.52 KB
/
CircularLL.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<stdio.h>
#include<stdlib.h>
struct node{
int info;
struct node* next;
};
//struct node* LAST=NULL;
struct node* createnode();
struct node* insertnodeatfirst(struct node* LAST , int value);
struct node* insertnodeatempty(struct node* LAST , int value);
struct node* insertnodeatend(struct node* LAST , int value);
struct node* insertnodeatposition(struct node* LAST , int value, int item);
struct node* createlist(struct node* LAST);
void dispaly(struct node* LAST);
struct node* createnode()
{
struct node* n ;
n=malloc(sizeof(struct node));
return n;
}
void dispaly(struct node* LAST) //to access the linklist using last pointer value
{
struct node *t;
if(LAST==NULL)
printf("List is empty!!\n");
else
{
do
{
t=LAST->next;
printf("%d\n",t->info);
t=t->next;
}
while(t!=LAST->next);
}
}
struct node* insertnodeatfirst(struct node* LAST, int value)
{
struct node* n;
n=createnode();
n->info=value;
n->next=LAST->next;
LAST->next=n;
return LAST;
}
struct node* insertnodeatempty(struct node* LAST , int value)
{
struct node* n;
n=createnode();
n->info=value;
LAST=n;
LAST->next=LAST;
return LAST;
}
struct node* insertnodeatend(struct node* LAST , int value)
{
struct node *n ,*t;
n=createnode();
n->info=value;
n->next=LAST->next;
LAST->next=n;
LAST=n;
return LAST;
}
struct node* insertnodeatposition(struct node* LAST , int value, int item)
{
struct node *t, *n;
t=LAST->next;
do
{
if(t->info==item)
{
n=createnode();
n->info=value;
n->next=t->next;
t->next=n;
return LAST;
}
t=t->next;
}while(t!=LAST->next);
printf("%d is not available!!",item);
return LAST;
}
struct node* createlist(struct node* LAST)
{
int n,value,i;
printf("Enter no. of nodes you want to add: ");
scanf("%d",&n);
printf("Enter first data: ");
scanf("%d",&value);
LAST=insertnodeatempty(LAST,value);
for(i=2;i<=n;i++)
{
printf("Enter next data: ");
scanf("%d",&value);
LAST=insertnodeatend(LAST,value);
}
return LAST;
}
void main()
{
struct node* LAST=NULL;
int choice,value,item;
while(1)
{
printf("\t\t\t1. CREATE LIST\n");
printf("\t\t\t2. DISPLAY LIST\n");
printf("\t\t\t3. INSERT AT FIRST\n");
printf("\t\t\t4. INSERT AT EMPTY\n");
printf("\t\t\t5. INSERT AT END\n");
printf("\t\t\t6. INSERT AT AFTER POSITION\n");
printf("\t\t\t7.EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
LAST=createlist(LAST);
break;
case 2:
dispaly(LAST);
break;
case 3:
printf("Enter the value: ");
scanf("%d",&value);
LAST=insertnodeatfirst(LAST,value);
break;
case 4:
printf("Enter the value: ");
scanf("%d",&value);
LAST=insertnodeatempty(LAST,value);
break;
case 5:
printf("Enter the value: ");
scanf("%d",&value);
LAST=insertnodeatend(LAST,value);
break;
case 6:
printf("Enter the item value after which new value to be insert: ");
scanf("%d",&item);
printf("Enter the value: ");
scanf("%d",&value);
LAST=insertnodeatposition(LAST,value,item);
break;
case 7:
exit(0);
break;
default:
printf("Choice not available\n");
}
}
}