Skip to content

Commit 418f0f5

Browse files
authored
Merge pull request TheAlgorithms#533 from shubhamsah/master
Circular Linked List
2 parents 0de20fc + a3e1817 commit 418f0f5

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/* Circularly Linked List (Basic Operations) - Program to create a Circularly linked list abstract data type and perform various operations on it (Variable first and last declared globally) */
2+
3+
#include <stdio.h>
4+
#include <conio.h>
5+
#include <stdlib.h>
6+
#define NULL 0
7+
8+
/* Assume that the data portion of each node consists of ONLY an integer.*/
9+
struct node
10+
{
11+
int data ;
12+
struct node *next ;
13+
} ;
14+
15+
struct node *first=NULL ;
16+
struct node *last=NULL ;
17+
/* first and last are global variables and need not be passed to any function. Any changes made to variables first and last by any of the functions in the program will be reflected in the entire program */
18+
19+
/* This function is responsible for creating the Circularly Linked List right from the BEGINING. */
20+
void create()
21+
{
22+
int i , n ;
23+
struct node *pnode , *p ;
24+
25+
printf("Enter the number of nodes required:\n") ;
26+
scanf("%d",&n) ;
27+
28+
printf("Enter the data value of each node:\n") ;
29+
for(i=1 ; i<=n ; i++)
30+
{
31+
pnode=(struct node*)malloc(sizeof(struct node)) ;
32+
if(pnode==NULL)
33+
{
34+
printf("Memory overflow. Unable to create.\n") ;
35+
return ;
36+
}
37+
38+
scanf("%d",&pnode->data) ;
39+
40+
if(first==NULL)
41+
first=last=pnode ;
42+
else
43+
{
44+
last->next=pnode ;
45+
last=pnode ; /* last keeps track of last node */
46+
}
47+
48+
last->next=first ;
49+
}
50+
}
51+
52+
/* This function will delete a node with value k from the Linked List if such a node exists */
53+
void deletenode(int k)
54+
{
55+
struct node *p , *follow ;
56+
57+
/* searching the required node */
58+
p=first ;
59+
follow=NULL ;
60+
while(follow!=last)
61+
{
62+
if(p->data==k)
63+
break ;
64+
follow=p ;
65+
p=p->next ;
66+
}
67+
68+
if(follow==last)
69+
printf("Required node not found.\n") ;
70+
else
71+
{
72+
if(p==first&&p==last) /* deleting the one and the only node */
73+
first=last=NULL ;
74+
else if(p==first) /* deleting the first node */
75+
{
76+
first=first->next ;
77+
last->next=first ;
78+
}
79+
else if(p==last) /* deleting the last node */
80+
{
81+
last=follow ;
82+
last->next=first ;
83+
}
84+
else /* deleting any other node */
85+
follow->next=p->next ;
86+
87+
free(p) ;
88+
}
89+
}
90+
91+
/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */
92+
void traverse()
93+
{
94+
struct node *p , *follow ;
95+
if(first==NULL)
96+
printf("Circularly Linked List Empty") ;
97+
else
98+
{
99+
printf("Circularly Linked List is as shown: \n") ;
100+
101+
p=first ;
102+
follow = NULL ;
103+
while(follow!=last)
104+
{
105+
printf("%d " , p->data) ;
106+
follow=p ;
107+
p=p->next ;
108+
}
109+
110+
printf("\n") ;
111+
}
112+
}
113+
114+
void main()
115+
{
116+
int x , k , ch ;
117+
clrscr() ;
118+
do
119+
{
120+
printf("\n Menu: \n") ;
121+
printf("1:Create Linked List \n") ;
122+
printf("2:Delete Node \n") ;
123+
printf("3:Traverse \n") ;
124+
printf("4:Exit \n") ;
125+
126+
printf("\nEnter your choice: ") ;
127+
scanf("%d",&ch) ;
128+
129+
switch(ch)
130+
{
131+
case 1:
132+
create() ;
133+
break ;
134+
135+
case 2:
136+
printf("Enter the data value of the node to be deleted: ") ;
137+
scanf("%d",&k) ;
138+
deletenode(k) ;
139+
break ;
140+
141+
case 3:
142+
traverse() ;
143+
break ;
144+
145+
case 4:
146+
break ;
147+
}
148+
}
149+
while(ch!=4) ;
150+
151+
getch() ;
152+
}
153+
154+
155+

0 commit comments

Comments
 (0)