-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathVIshruthS_Q24_CircularLinkedList.c
154 lines (139 loc) · 3.18 KB
/
VIshruthS_Q24_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
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
//CSL201 DATA STRUCTURES LAB ----- VISHRUTH S, CS3A, 61
//CYCLE 3 QUESTION 4
//To implement a Circular Linked List and perform the necessary operations
#include <stdio.h>
#include <stdlib.h>
// Node contains data and link to next node
struct Node
{
int data;
struct Node *link;
};
// Last pointer to linked list, pointing to last element
struct Node *last = NULL;
// Function to insert element at the beginning
// Time complexity: O(1)
void insertAtBeginning(int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
if (last == NULL)
last = newNode;
else
newNode->link = last->link;
last->link = newNode;
printf("\nInsertion successful");
}
// Function to insert element at the end
// Time complexity: O(1)
void insertAtEnd(int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
if (last == NULL)
{
last = newNode;
last->link = newNode;
}
else
{
newNode->link = last->link;
last->link = newNode;
last = newNode;
}
printf("\nInsertion successful");
}
// Function to insert element after a particular position
// Time complexity: Worst case O(n), Best case O(1)
void insertAfterPosition(int position, int data)
{
if (last == NULL)
{
printf("\nList empty");
return;
}
struct Node *curr = last->link;
while (--position)
{
curr = curr->link;
if (curr == last->link)
{
printf("\nPosition exceeded list size");
return;
}
}
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->link = curr->link;
curr->link = newNode;
if (curr == last)
last = newNode;
printf("\nInsertion successful");
}
// Function to delete a particular node
// Time complexity: Worst case O(n), Best case O(1)
void deleteElement(int element)
{
if (last == NULL)
{
printf("\nList empty");
return;
}
struct Node *prev = last;
struct Node *curr = last->link;
while (curr->data != element)
{
prev = curr;
curr = curr->link;
if (curr == last->link)
{
printf("\nElement not found");
return;
}
}
prev->link = curr->link;
if (curr == last)
last = prev;
if (curr == prev)
last = NULL;
free(curr);
printf("\nDeletion successful");
}
void display();
// MAIN FUNCTION
int main()
{
insertAtBeginning(10);
insertAtBeginning(20);
display();
insertAfterPosition(1, 25);
insertAfterPosition(2, 15);
insertAfterPosition(3, 45);
display();
deleteElement(10);
display();
deleteElement(15);
display();
deleteElement(45);
display();
insertAfterPosition(5, 13);
deleteElement(13);
display();
return 0;
}
// Function to display elements
void display()
{
if (last == NULL)
{
printf("\nList empty");
return;
}
struct Node *ptr = last->link;
printf("\n");
do
{
printf("%d ", ptr->data);
ptr = ptr->link;
} while (ptr != last->link);
}