forked from DGIIIT/Hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_linked_list
137 lines (132 loc) · 2.42 KB
/
Circular_linked_list
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
// Write a C Program to implement CIRCULAR LINKED LIST(one way).This Program must include following functions in it.
// a.Write a function to CONSTRUCT a circular linked list.
// b.Write a function to INSERT an element at the beginning of the created linked list(step a).
// c.Write a function to INSERT an element at the end of the circular linked list.
// d.Write a function to DELETE an element from the beginning of the circular linked list.
// e.Write a function to DELETE an element from the end of the circular linkrd list.
// f.Write a function for 'DISPLAY' operation which prints the content of the circular linked list.
#include<stdio.h>
#include<stdlib.h>
struct cr
{
int data;
struct cr *next;
}
s1,*node,*start,*end,*temp;
int count=0;
void construct()
{
start=NULL;node=NULL;
}
void insertb()
{
int x;
printf("Enter the element:"); scanf("%d",&x);
count++;
node=(struct cr *)malloc(sizeof(s1));
node->data=x;
if(start==NULL)
{
start=node;
start->next=start;
end=node;
}
else
{
node->next=start;
end->next=node;
start=node;
}
}
void inserte(){
int x;
printf("Enter the element:"); scanf("%d",&x);
count++;
node=(struct cr *)malloc(sizeof(s1));
node->data=x;
if(start==NULL)
{
start=node;
start->next=start;
end=node;
}
else
{
node->next=start;
end->next=node;
end=node;
}
}
void deletee()
{
if(start==NULL)
printf("Queue Underflow\n");
else if(start==end)
{
temp=end;
free(temp);
start=NULL;
end=NULL;
}
else
{
temp=end;
count--;
for(node=start;node->next->next!=start;node=node->next);
end=node;
end->next=start;
free(temp);
}
}
void deleteb()
{
if(start==NULL)
printf("Queue Underflow\n");
else if(start==end)
{
temp=end;
free(temp);
start=NULL;
end=NULL;
}
else
{
temp=start;
count--;
start=start->next;
end->next=start;
free(temp);
}
}
void display()
{
int i;
if(start==NULL)
printf("Queue underflow!\n");
else
{
printf("\nThe elements are:\n");
for(node=start,i=0;i<count;i++,node=node->next)
printf("%d ",node->data);
}
}
void main()
{
int ch;
construct();
while(1)
{
printf("\n\n***Circular Queue***\n1.Insert at beginning\n2.Insert at end\n3.Delete from begining\n4.Delete from end\n5.Display\n6.Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insertb(); break;
case 2: inserte(); break;
case 3: deleteb(); break;
case 4: deletee(); break;
case 5: display(); break;
case 6: exit(0); break;
default: printf("\nWrong Choice!!\n");
}
}
}