Skip to content

Commit c171221

Browse files
authored
Merge pull request ephremdeme#291 from Anuragkar234/cll
Added Circular Linked list in C
2 parents 6711547 + 9975c07 commit c171221

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

linked-list/circular-linked-list.c

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*-----------------------------------------------------
2+
---------------Circular Linked list program-----------
3+
-----------------------------------------------------*/
4+
5+
//Header files
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
//Sturcture for storing node information
10+
struct Clinked
11+
{
12+
int data;
13+
struct Clinked *link;
14+
};
15+
16+
//For declaring the structure pointers for linked list
17+
struct Clinked *ctop = NULL, *init, *temp, *rep;
18+
19+
/*------------------------------------------
20+
----Defination of all neccessary function----
21+
---------------------------------------------*/
22+
23+
//This function si to create a new circular linked list for our execution
24+
void New()
25+
{
26+
int op;
27+
28+
init = (struct Clinked*)malloc(sizeof(struct Clinked)); //dynmically allocating memory
29+
printf("\nPlease enter the integer: ");
30+
scanf("%d", &init->data);
31+
init->link = init;
32+
ctop = init;
33+
printf("\nTo enter another integer press 1 or 0 to discontinue: ");
34+
scanf("%d", &op);
35+
while (op != 0)
36+
{
37+
temp = (struct Clinked*)malloc(sizeof(struct Clinked)); //dynamically allocation memory
38+
printf("\nPlease enter the integer: ");
39+
scanf("%d", &temp->data);
40+
init->link = temp;
41+
temp->link = ctop;
42+
init = temp;
43+
printf("\nTo enter another integer press 1 or 0 to discontinue: ");
44+
scanf("%d", &op);
45+
}
46+
}
47+
48+
//This function is to insert element at beginning of circular linked list
49+
50+
void InsertBeg()
51+
{
52+
init = ctop;
53+
temp = (struct Clinked*)malloc(sizeof(struct Clinked));
54+
printf("\nPlease enter the integer: ");
55+
scanf("%d", &temp->data);
56+
while (init->link != ctop)
57+
{
58+
init = init->link;
59+
}
60+
init->link = temp;
61+
temp->link = ctop;
62+
ctop = temp;
63+
}
64+
65+
//This function is to insert element at any position of circular linked list
66+
67+
void InsertPos()
68+
{
69+
struct Clinked *ptr;
70+
int c = 1, pos, count = 1;
71+
72+
temp = (struct Clinked*)malloc(sizeof(struct Clinked));
73+
if (ctop == NULL)
74+
{
75+
printf(\n"Whoops!, cannot insert the element at this positon...\n");
76+
}
77+
printf("\nPlease enter the integer: ");
78+
scanf("%d", &temp->data);
79+
printf("\nPlease enter a position where you want to insert the above element: ");
80+
scanf("%d", &pos);
81+
init = ctop;
82+
ptr = ctop;
83+
while (ptr->link != ctop)
84+
{
85+
count++;
86+
ptr = ptr->link;
87+
}
88+
count++;
89+
if (pos > count)
90+
{
91+
printf("Warning!! Linked List beyond boundary!!\n");
92+
return;
93+
}
94+
while (c < pos)
95+
{
96+
rep = init;
97+
init = init->link;
98+
c++;
99+
}
100+
temp->link = init;
101+
rep->link = temp;
102+
}
103+
104+
//This function is to delete the beginning element of the circular linked list
105+
106+
void DeleteBeg()
107+
{
108+
if (ctop == NULL)
109+
printf("\nOops!, The list is empty...\n");
110+
else
111+
{
112+
init = ctop;
113+
temp = ctop;
114+
while (init->link != ctop)
115+
{
116+
init = init->link;
117+
}
118+
ctop = temp->link;
119+
init->link = ctop;
120+
free(temp);
121+
}
122+
}
123+
124+
//This function is to delete element at any given postion or end of circular linked list
125+
126+
void DeletePos()
127+
{
128+
if (ctop == NULL)
129+
printf("\nOops!, The list is empty...\n");
130+
else
131+
{
132+
int c = 1, pos;
133+
printf("\nPlease enter the position from where you want to delete a element: ");
134+
scanf("%d", &pos);
135+
init = ctop;
136+
while (c < pos)
137+
{
138+
temp = init;
139+
init = init->link;
140+
c++;
141+
}
142+
temp->link = init->link;
143+
free(init);
144+
}
145+
}
146+
147+
//This function is to dsplay all the elements of linked list
148+
149+
void Display()
150+
{
151+
if (ctop == NULL)
152+
printf("\nOh no!, Nothing to show...\n");
153+
else
154+
{
155+
init = ctop;
156+
while (init->link != ctop)
157+
{
158+
printf("%d->", init->data);
159+
init = init->link;
160+
}
161+
printf("%d", init->data);
162+
}
163+
}
164+
165+
/*-----------------------
166+
------main function------
167+
-------------------------*/
168+
169+
int main()
170+
{
171+
172+
printf("\n\n------------------------------------\n");
173+
printf(" Circular linked list\n");
174+
printf("------------------------------------\n");
175+
176+
int op; //for options selection
177+
178+
printf("\nPlease enter the options you want to execute:\n ");
179+
180+
while (1)
181+
{
182+
//menu
183+
printf("\n(1) Create a circular linked list");
184+
printf("\n(2) Insert an element at beginning");
185+
printf("\n(3) Insert an element at end or any position");
186+
printf("\n(4) Delete an element at beginning");
187+
printf("\n(5) Delete an element from end or any position");
188+
printf("\n(6) Display the list");
189+
printf("\n(7) Exit :(");
190+
191+
printf("\nOption: ");
192+
scanf("%d", &op);
193+
switch (op)
194+
{
195+
case 1:
196+
New();
197+
break;
198+
case 2:
199+
InsertBeg();
200+
break;
201+
case 3:
202+
InsertPos();
203+
break;
204+
case 4:
205+
DeleteBeg();
206+
break;
207+
case 5:
208+
DeletePos();
209+
break;
210+
case 6:
211+
Display();
212+
break;
213+
case 7:
214+
printf("\nBye bye..:(\n----------------\n\n");
215+
exit(0);
216+
break; //for better coding practice
217+
default:
218+
printf("\nHey! you like to enter wrong key, :D...Try again!");
219+
break;
220+
}
221+
222+
printf("\n\nPlease enter a option again: \n");
223+
}
224+
return 0;
225+
}
226+

0 commit comments

Comments
 (0)