0% found this document useful (0 votes)
5 views4 pages

Circular Linked List

Uploaded by

Thanos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Circular Linked List

Uploaded by

Thanos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Circular Linked List CE Kalloopp ara

#include <stdio.h> case 2:


#include <stdlib.h> printf("Enter value: ");
scanf("%d", &val);
// Define the node structure inE(val);
struct node { break;
int data; case 3:
struct node *next; printf("Enter value: ");
} *head = NULL; scanf("%d", &val);
printf("Enter location: ");
// Function declarations scanf("%d", &loc);
void inB(int val); inS(val, loc);
void inE(int val); break;
void inS(int val, int loc); case 4:
void deB(); deB();
void deE(); break;
void deS(int loc); case 5:
void dis(); deE();
break;
int main() { case 6:
int ch, val, loc; printf("Enter location: ");
for (;;) { scanf("%d", &loc);
printf("\nA.Insert:\t1.At Beginning\t2.At End\t3.At deS(loc);
Specific Location");
break;
printf("\nB.Delete:\t4.At Beginning\t5.At End\t6.At
case 7:
Specific Location");
dis();
printf("\n7.Display\t8.Exit\tChoice: ");
break;
scanf("%d", &ch);
case 8:
switch (ch) {
return 0;
case 1:
default:
printf("Enter value: ");
printf("Invalid input\n");
scanf("%d", &val);
}
inB(val);
}
break;
}

SHILPA RADHAKRISHNAN, AP IN CSE 1


Circular Linked List CE Kalloopp ara

}
// 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;

SHILPA RADHAKRISHNAN, AP IN CSE 2


Circular Linked List CE Kalloopp ara

for (int i = 1; i < loc - 1; i++) { printf("Empty List\n");


temp = temp->next; return;
if (temp == head) { }
printf("Invalid location. The position is beyond struct node *temp = head, *prev = NULL;
the list length.\n");
if (head->next == head) { // Only one node in the list
free(newnode);
free(head);
return;
head = NULL;
}
} else {
}
while (temp->next != head) {
newnode->next = temp->next;
prev = temp;
temp->next = newnode;
temp = temp->next;
}
}
// Delete from the beginning
prev->next = head;
void deB() {
free(temp);
if (head == NULL) {
}
printf("Empty List\n");
}
return;
// Delete from a specific location
}
void deS(int loc) {
struct node *temp = head, *last = head;
if (head == NULL) {
if (head->next == head) { // Only one node in the list
printf("Empty List\n");
free(head);
return;
head = NULL;
}
} else {
struct node *temp = head, *prev = NULL;
while (last->next != head)
if (loc == 1) { // Delete the head node
last = last->next; // Find the last node
if (head->next == head) { // Only one node in the
head = head->next; list
last->next = head; free(head);
free(temp); head = NULL;
} } else {
} struct node *last = head;
// Delete from the end while (last->next != head)
void deE() { last = last->next; // Find the last node
if (head == NULL) { last->next = head->next;

SHILPA RADHAKRISHNAN, AP IN CSE 3


Circular Linked List CE Kalloopp ara

free(head); }
head = last->next;
}
return;
}

for (int i = 1; i < loc; i++) {


prev = temp;
temp = temp->next;
if (temp == head) {
printf("Invalid location. The position is beyond
the list length.\n");
return;
}
}

prev->next = temp->next;
free(temp);
}

// Display the list


void dis() {
if (head == NULL) {
printf("Empty List\n");
} else {
struct node *temp = head;
printf("List: ");
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(head)\n");
}

SHILPA RADHAKRISHNAN, AP IN CSE 4

You might also like