-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathcircular_linked_list.c
155 lines (129 loc) · 3.28 KB
/
circular_linked_list.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
/* 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) */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define NULL 0
/* Assume that the data portion of each node consists of ONLY an integer.*/
struct node
{
int data ;
struct node *next ;
} ;
struct node *first=NULL ;
struct node *last=NULL ;
/* 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 */
/* This function is responsible for creating the Circularly Linked List right from the BEGINNING. */
void create()
{
int i , n ;
struct node *pnode , *p ;
printf("Enter the number of nodes required:\n") ;
scanf("%d",&n) ;
printf("Enter the data value of each node:\n") ;
for(i=1 ; i<=n ; i++)
{
pnode=(struct node*)malloc(sizeof(struct node)) ;
if(pnode==NULL)
{
printf("Memory overflow. Unable to create.\n") ;
return ;
}
scanf("%d",&pnode->data) ;
if(first==NULL)
first=last=pnode ;
else
{
last->next=pnode ;
last=pnode ; /* last keeps track of last node */
}
last->next=first ;
}
}
/* This function will delete a node with value k from the Linked List if such a node exists */
void deletenode(int k)
{
struct node *p , *follow ;
/* searching the required node */
p=first ;
follow=NULL ;
while(follow!=last)
{
if(p->data==k)
break ;
follow=p ;
p=p->next ;
}
if(follow==last)
printf("Required node not found.\n") ;
else
{
if(p==first&&p==last) /* deleting the one and the only node */
first=last=NULL ;
else if(p==first) /* deleting the first node */
{
first=first->next ;
last->next=first ;
}
else if(p==last) /* deleting the last node */
{
last=follow ;
last->next=first ;
}
else /* deleting any other node */
follow->next=p->next ;
free(p) ;
}
}
/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */
void traverse()
{
struct node *p , *follow ;
if(first==NULL)
printf("Circularly Linked List Empty") ;
else
{
printf("Circularly Linked List is as shown: \n") ;
p=first ;
follow = NULL ;
while(follow!=last)
{
printf("%d " , p->data) ;
follow=p ;
p=p->next ;
}
printf("\n") ;
}
}
void main()
{
int x , k , ch ;
clrscr() ;
do
{
printf("\n Menu: \n") ;
printf("1:Create Linked List \n") ;
printf("2:Delete Node \n") ;
printf("3:Traverse \n") ;
printf("4:Exit \n") ;
printf("\nEnter your choice: ") ;
scanf("%d",&ch) ;
switch(ch)
{
case 1:
create() ;
break ;
case 2:
printf("Enter the data value of the node to be deleted: ") ;
scanf("%d",&k) ;
deletenode(k) ;
break ;
case 3:
traverse() ;
break ;
case 4:
break ;
}
}
while(ch!=4) ;
getch() ;
}