-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular linked list1
127 lines (114 loc) · 3.04 KB
/
circular linked list1
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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void create(int data) {
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
tail = newnode;
tail->next = head; // Make it circular
} else {
tail->next = newnode;
tail = tail->next;
tail->next = head; // Make it circular
}
}
void linkedlisttraversal(struct node *ptr) {
struct node *temp = ptr;
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}
struct node *insertatfirst(struct node *head, int data) {
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = data;
ptr->next = head;
head = ptr;
tail->next = head; // Update tail to maintain circularity
return head;
}
struct node *insertatpos(struct node *head, int pos, int data) {
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp = head;
int i = 0;
while (i != pos - 1) {
temp = temp->next;
i++;
}
ptr->data = data;
ptr->next = temp->next;
temp->next = ptr;
return head;
}
struct node *insertatlast(struct node *head, int data) {
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = data;
ptr->next = head; // Make it circular
tail->next = ptr; // Update tail to maintain circularity
tail = ptr;
return head;
}
struct node *deleteatfirst(struct node *head) {
struct node *ptr = head;
head = head->next;
tail->next = head; // Update tail to maintain circularity
free(ptr);
return head;
}
struct node *deleteatpos(struct node *head, int pos) {
struct node *ptr = head;
struct node *q = head->next;
for (int i = 0; i < pos - 1; i++) {
ptr = ptr->next;
q = q->next;
}
ptr->next = q->next;
free(q);
return head;
}
struct node *deleteatlast(struct node *head) {
struct node *ptr = head;
struct node *q = head->next;
while (q->next != head) {
ptr = ptr->next;
q = q->next;
}
ptr->next = head; // Make it circular
tail = ptr; // Update tail to maintain circularity
free(q);
return head;
}
int main() {
create(10);
create(20);
create(30);
linkedlisttraversal(head);
head = insertatfirst(head, 60);
linkedlisttraversal(head);
head = insertatpos(head, 2, 50);
linkedlisttraversal(head);
head = insertatlast(head, 70);
linkedlisttraversal(head);
head = deleteatfirst(head);
linkedlisttraversal(head);
head = deleteatpos(head, 2);
linkedlisttraversal(head);
head = deleteatlast(head);
linkedlisttraversal(head);
// Free memory for circular linked list
struct node *temp = head;
do {
struct node *nextNode = temp->next;
free(temp);
temp = nextNode;
} while (temp != head);
return 0;
}