Write a c program to Create a Circular Linked list and perform Following Operations A. Insertion At Beginning B. Insertion At End C. Insertion After a particular node Insertion Before a particular node E. Insertion at specific position F. Search a particular node G. Return a particular node H. Deletion at the beginning I. Deletion at the end J. Deletion after a particular node K. Deletion before a particular node L. Delete a particular node M. Deletion at a specific position
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node with given data
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
// Function to insert a node at the end of the linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Function to insert a node after a particular node
void insertAfterNode(struct Node* prevNode, int data) {
if (prevNode == NULL) {
printf("The given previous node cannot be NULL\n");
return;
}
struct Node* newNode = createNode(data);
newNode->next = prevNode->next;
prevNode->next = newNode;
}
// Function to insert a node before a particular node
void insertBeforeNode(struct Node** head, int beforeData, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
printf("List is empty\n");
return;
}
if ((*head)->data == beforeData) {
newNode->next = *head;
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL && temp->next->data != beforeData) {
temp = temp->next;
}
if (temp->next == NULL) {
printf("Node with data %d not found\n", beforeData);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Function to insert a node at a specific position
void insertAtPosition(struct Node** head, int position, int data) {
if (position < 1) {
printf("Position should be >= 1\n");
return;
}
if (position == 1) {
insertAtBeginning(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* temp = *head;
for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Function to search for a particular node
int searchNode(struct Node* head, int data) {
struct Node* temp = head;
int position = 1;
while (temp != NULL) {
if (temp->data == data) {
return position;
}
temp = temp->next;
position++;
}
return -1; // Not found
}
// Function to return a particular node by its position
struct Node* getNodeByPosition(struct Node* head, int position) {
if (position < 1) {
printf("Position should be >= 1\n");
return NULL;
}
struct Node* temp = head;
for (int i = 1; temp != NULL && i < position; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return NULL;
}
return temp;
}
// Function to delete a node at the beginning
void deleteAtBeginning(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}
// Function to delete a node at the end
void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
struct Node* prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
}
// Function to delete a node after a particular node
void deleteAfterNode(struct Node* prevNode) {
if (prevNode == NULL || prevNode->next == NULL) {
printf("The given node is invalid or it has no next node\n");
return;
}
struct Node* temp = prevNode->next;
prevNode->next = temp->next;
free(temp);
}
// Function to delete a node before a particular node
void deleteBeforeNode(struct Node** head, int beforeData) {
if (*head == NULL || (*head)->next == NULL) {
printf("List is empty or has only one node\n");
return;
}
if ((*head)->data == beforeData) {
printf("No node exists before the first node\n");
return;
}
struct Node* temp = *head;
struct Node* prev = NULL;
while (temp->next != NULL && temp->next->data != beforeData) {
prev = temp;
temp = temp->next;
}
if (temp->next == NULL) {
printf("Node with data %d not found\n", beforeData);
return;
}
if (prev == NULL) {
*head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
}
// Function to delete a particular node by its data
void deleteNode(struct Node** head, int data) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
struct Node* prev = NULL;
while (temp != NULL && temp->data != data) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Node with data %d not found\n", data);
return;
}
if (prev == NULL) {
*head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
}
// Function to delete a node at a specific position
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL || position < 1) {
printf("List is empty or invalid position\n");
return;
}
struct Node* temp = *head;
if (position == 1) {
*head = temp->next;
free(temp);
return;
}
struct Node* prev = NULL;
for (int i = 1; temp != NULL && i < position; i++) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
prev->next = temp->next;
free(temp);
}
// Function to print the linked list
void printList(struct Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
// Sample operations
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 4);
insertAtEnd(&head, 5);
insertAtPosition(&head, 3, 3); // Insert 3 at position 3
insertAtBeginning(&head, 0); // Insert 0 at the beginning
printf("Initial list: ");
printList(head);
printf("After inserting 6 at the end: ");
insertAtEnd(&head, 6);
printList(head);
printf("After inserting 7 at position 4: ");
insertAtPosition(&head, 4, 7);
printList(head);
printf("After inserting 8 before node with data 3: ");
insertBeforeNode(&head, 3, 8);
printList(head);
printf("After deleting node at position 2: ");
deleteAtPosition(&head, 2);
printList(head);
printf("After deleting node with data 5: ");
deleteNode(&head, 5);
printList(head);
printf("After deleting node at the beginning: ");
deleteAtBeginning(&head);
printList(head);
printf("After deleting node at the end: ");
deleteAtEnd(&head);
printList(head);
printf("Searching for node with data 4: ");
int position = searchNode(head, 4);
if (position != -1) {
printf("Node found at position %d\n", position);
} else {
printf("Node not found\n");
}
struct Node* node = getNodeByPosition(head, 3);
if (node != NULL) {
printf("Node at position 3 has data: %d\n", node->data);
}
return 0;
}
Comments
Post a Comment