-
Notifications
You must be signed in to change notification settings - Fork 1
/
11_1_c_circularlinkedlist.c
102 lines (100 loc) · 2.07 KB
/
11_1_c_circularlinkedlist.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
#include <stdio.h>
#include <malloc.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
void insert(int e) {
struct node *t;
if(head == NULL) {
head = (struct node *)malloc(sizeof(struct node));
head->data = e;
head->next = head; //
}
else {
t = head;
while(t->next != head) {
t = t->next;
}
t->next = (struct node *)malloc(sizeof(struct node));
t->next->data = e;
t->next->next = head;/////
}
}
void disp() {
struct node *t;
if(head == NULL) {
printf("\nCircular Linked List is Empty");
}
else {
t = head;
printf("\n");
while(t->next != head) {
printf("%d\t",t->data);
t = t->next;
}
printf("%d",t->data);
}
}
void disp_2() {
struct node *t;
if(head == NULL) {
printf("\nCircular Linked List is Empty");
}
else {
t = head;
printf("\n");
do {
printf("%d\t",t->data);
t = t->next;
}while(t != head);
}
}
void delete(int e) {
struct node *t;
if(head == NULL) {
printf("\nEmpty Circular Linked List");
}
else if(head->data == e && head->next == head) {
head = NULL;
}
else if(head->data == e)
{
t = head;
while(t->next != head) {
t = t->next;
}
t->next = head->next;
head = head->next;
}
else {
t = head;
while(t->next != head && t->next->data != e) {
t = t->next;
}
if(t->next == head) {
printf("\nElement Not Found");
}
else {
t->next = t->next->next;
}
}
}
int main()
{
insert(10);
insert(20);
insert(30);
disp();
disp_2();
delete(20);
disp();
delete(100);
disp();
delete(10);
disp();
delete(30);
disp();
return 0;
}