Circular Linked List
Circular Linked List
}
// Insert at the beginning }
void inB(int val) {
struct node *newnode = (struct node // Insert at a specific location
*)malloc(sizeof(struct node));
void inS(int val, int loc) {
newnode->data = val;
struct node *newnode = (struct node
if (head == NULL) { *)malloc(sizeof(struct node));
newnode->next = newnode; // Point to itself newnode->data = val;
head = newnode;
} else { // Check if list is empty and location is greater than 1
struct node *temp = head; if (head == NULL && loc > 1) {
while (temp->next != head) printf("Invalid location. The position is beyond the
list length.\n");
temp = temp->next; // Traverse to the last node
free(newnode);
newnode->next = head;
return;
temp->next = newnode;
}
head = newnode; // Update head to the new node
}
if (loc == 1) { // Insert at the beginning
}
if (head == NULL) {
newnode->next = newnode; // Point to itself
// Insert at the end
head = newnode;
void inE(int val) {
} else {
struct node *newnode = (struct node
*)malloc(sizeof(struct node)); struct node *temp = head;
newnode->data = val; while (temp->next != head)
if (head == NULL) { temp = temp->next; // Traverse to the last node
newnode->next = newnode; // Point to itself newnode->next = head;
head = newnode; temp->next = newnode;
} else { head = newnode; // Update head to the new node
struct node *temp = head; }
while (temp->next != head) return;
temp = temp->next; // Traverse to the last node }
temp->next = newnode;
newnode->next = head; struct node *temp = head;
free(head); }
head = last->next;
}
return;
}
prev->next = temp->next;
free(temp);
}