Untitled
unknown
plain_text
8 months ago
8.2 kB
5
Indexable
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// LinkedList structure
struct LinkedList {
struct Node* head;
};
// Function prototypes
void addAtBeginning(struct LinkedList* list, int data);
void addAtPosition(struct LinkedList* list, int data, int position);
void addAtEnd(struct LinkedList* list, int data);
void deleteAtBeginning(struct LinkedList* list);
void deleteAtPosition(struct LinkedList* list, int position);
void deleteAtEnd(struct LinkedList* list);
void display(struct LinkedList* list);
int isDuplicate(struct LinkedList* list, int data);
// Function to create a new Node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode; // Points to itself (circular)
return newNode;
}
// Function to initialize an empty LinkedList
void initLinkedList(struct LinkedList* list) {
list->head = NULL;
}
// Function to free the memory of the linked list
void destroyLinkedList(struct LinkedList* list) {
if (list->head == NULL) return;
struct Node* current = list->head;
struct Node* nextNode;
do {
nextNode = current->next;
free(current);
current = nextNode;
} while (current != list->head);
list->head = NULL;
}
// Function to check if the data already exists in the circular linked list
int isDuplicate(struct LinkedList* list, int data) {
if (list->head == NULL) return 0;
struct Node* current = list->head;
do {
if (current->data == data) {
return 1; // Duplicate found
}
current = current->next;
} while (current != list->head);
return 0; // No duplicate found
}
// Function to add a new node at the beginning of the linked list
void addAtBeginning(struct LinkedList* list, int data) {
if (isDuplicate(list, data)) {
printf("Duplicate value %d not inserted.\n", data);
return;
}
struct Node* newNode = createNode(data);
if (list->head == NULL) {
list->head = newNode;
} else {
struct Node* temp = list->head;
while (temp->next != list->head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = list->head;
list->head = newNode;
}
}
// Function to add a new node at a specific position in the linked list
void addAtPosition(struct LinkedList* list, int data, int position) {
if (isDuplicate(list, data)) {
printf("Duplicate value %d not inserted.\n", data);
return;
}
struct Node* newNode = createNode(data);
if (position == 1) {
newNode->next = list->head;
if (list->head != NULL) {
struct Node* temp = list->head;
while (temp->next != list->head) {
temp = temp->next;
}
temp->next = newNode;
}
list->head = newNode;
return;
}
struct Node* current = list->head;
int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL) {
printf("\nInvalid position.\n");
free(newNode);
return;
}
newNode->next = current->next;
current->next = newNode;
}
// Function to add a new node at the end of the linked list
void addAtEnd(struct LinkedList* list, int data) {
if (isDuplicate(list, data)) {
printf("Duplicate value %d not inserted.\n", data);
return;
}
struct Node* newNode = createNode(data);
if (list->head == NULL) {
list->head = newNode;
} else {
struct Node* current = list->head;
while (current->next != list->head) {
current = current->next;
}
current->next = newNode;
newNode->next = list->head;
}
}
// Function to delete the node at the beginning of the linked list
void deleteAtBeginning(struct LinkedList* list) {
if (list->head == NULL) {
printf("\nList is empty.\n");
return;
}
struct Node* temp = list->head;
if (list->head->next == list->head) {
free(temp);
list->head = NULL;
} else {
struct Node* current = list->head;
while (current->next != list->head) {
current = current->next;
}
current->next = list->head->next;
list->head = list->head->next;
free(temp);
}
}
// Function to delete the node at a specific position in the linked list
void deleteAtPosition(struct LinkedList* list, int position) {
if (list->head == NULL) {
printf("\nList is empty.\n");
return;
}
if (position == 1) {
deleteAtBeginning(list);
return;
}
struct Node* current = list->head;
int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL || current->next == list->head) {
printf("\nInvalid position.\n");
return;
}
struct Node* temp = current->next;
current->next = current->next->next;
free(temp);
}
// Function to delete the node at the end of the linked list
void deleteAtEnd(struct LinkedList* list) {
if (list->head == NULL) {
printf("\nList is empty.\n");
return;
}
if (list->head->next == list->head) {
free(list->head);
list->head = NULL;
return;
}
struct Node* current = list->head;
while (current->next != list->head) {
current = current->next;
}
current->next = list->head;
free(current->next);
}
// Function to display the circular linked list
void display(struct LinkedList* list) {
if (list->head == NULL) {
printf("\nList is empty.\n");
return;
}
struct Node* current = list->head;
printf("\nCircular Linked List: ");
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != list->head); // Loop back to the head
printf("%d\n", list->head->data); // Print the head again at the end
}
// Main function
int main() {
struct LinkedList list;
int choice, data, position;
initLinkedList(&list);
do {
printf("\nMenu:\n");
printf("1. Add at beginning\n");
printf("2. Add at any position\n");
printf("3. Add at end\n");
printf("4. Delete at beginning\n");
printf("5. Delete at any position\n");
printf("6. Delete at end\n");
printf("0. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter data: ");
scanf("%d", &data);
addAtBeginning(&list, data);
display(&list);
break;
case 2:
printf("\nEnter data: ");
scanf("%d", &data);
printf("\nEnter position: ");
scanf("%d", &position);
addAtPosition(&list, data, position);
display(&list);
break;
case 3:
printf("\nEnter data: ");
scanf("%d", &data);
addAtEnd(&list, data);
display(&list);
break;
case 4:
deleteAtBeginning(&list);
display(&list);
break;
case 5:
printf("\nEnter position: ");
scanf("%d", &position);
deleteAtPosition(&list, position);
display(&list);
break;
case 6:
deleteAtEnd(&list);
display(&list);
break;
case 0:
printf("\nExiting program.\n");
destroyLinkedList(&list);
break;
default:
printf("\nInvalid choice. Please try again.\n");
}
} while (choice != 0);
return 0;
}
Editor is loading...
Leave a Comment