Write a menu driven C program to implement of circular linked list with following…
- User
Write a menu driven C program to implement of circular linked list with following operations: • Create the list • Insert a node in the beginning, in the end, at given position • Delete a node in the beginning, in the end, at given position • Search a node • Display the list
- Assistant
#include<stdio.h> #include<stdlib.h>
// Define structure for Node struct node { int data; struct node* next; };
// Function to create a Node struct node* createNode(int data) { struct node* newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; newNode->next = NULL; return newNode; }
// Function to display the Linked List void display(struct node* head) { if(head == NULL) { printf("\nCircular Linked List is empty!"); return; } struct node* current = head; printf("\nCircular Linked List: "); do { printf("%d -> ", current->data); current = current->next; } while(current != head); }
// Function to insert a node at the beginning of the Circular Linked List struct node* insertAtBeginning(struct node* head, int data) { struct node* newNode = createNode(data); if(head == NULL) { newNode->next = newNode; head = newNode; } else { struct node* current = head; while(current->next != head) current = current->next; newNode->next = head; current->next = newNode; head = newNode; } return head; }
// Function to insert a node at the end of the Circular Linked List struct node* insertAtEnd(struct node* head, int data) { struct node* newNode = createNode(data); if(head == NULL) { newNode->next = newNode; head = newNode; } else { struct node* current = head; while(current->next != head) current = current->next; newNode->next = head; current->next = newNode; } return head; }
// Function to insert a node at given position in the Circular Linked List struct node* insertAtPosition(struct node* head, int data, int position) { if(position < 1) { printf("\nInvalid position!"); return head; } else if(position == 1) return insertAtBeginning(head, data); else { struct node* newNode = createNode(data); struct node* current = head; int i = 1; while(i < position-1 && current->next != head) { current = current->next; i++; } if(i < position-1) { printf("\nPosition exceeds the length of Circular Linked List!"); return head; } newNode->next = current->next; current->next = newNode; return head; } }
// Function to delete a node from the beginning of the Circular Linked List struct node* deleteFromBeginning(struct node* head) { if(head == NULL) { printf("\nCircular Linked List is empty!"); return head; } else if(head->next == head) { free(head); return NULL; } else { struct node* current = head; while(current->next != head) current = current->next; current->next = head->next; struct node* deleteNode = head; head = head->next; free(deleteNode); return head; } }
// Function to delete a node from the end of the Circular Linked List struct node* deleteFromEnd(struct node* head) { if(head == NULL) { printf("\nCircular Linked List is empty!"); return head; } else if(head->next == head) { free(head); return NULL; } else { struct node* current = head; while(current->next->next != head) current = current->next; struct node* deleteNode = current->next; current->next = head; free(deleteNode); return head; } }
// Function to delete a node from a given position in the Circular Linked List struct node* deleteFromPosition(struct node* head, int position) { if(position < 1) { printf("\nInvalid position!"); return head; } else if(position == 1) return deleteFromBeginning(head); else { struct node* current = head; int i = 1; while(i < position-1 && current->next != head) { current = current->next; i++; } if(i < position-1) { printf("\nPosition exceeds the length of Circular Linked List!"); return head; } struct node* deleteNode = current->next; current->next = current->next->next; free(deleteNode); return head; } }
// Function to Search a node in the Circular Linked List int search(struct node* head, int key) { if(head == NULL) { printf("\nCircular Linked List is empty!"); return 0; } struct node* current = head; int position = 1; do { if(current->data == key) return position; current = current->next; position++; } while(current != head); printf("\n%d not found in the Circular Linked List!", key); return 0; }
// Main function of the program int main() { struct node* head = NULL; int choice, data, position, key;
// Menu-driven program do { printf("\n\n--------------- MENU ---------------"); printf("\n1. Create Circular Linked List"); printf("\n2. Insert at Beginning"); printf("\n3. Insert at End"); printf("\n4. Insert at Given Position"); printf("\n5. Delete from Beginning"); printf("\n6. Delete from End"); printf("\n7. Delete from Given Position"); printf("\n8. Search a Node"); printf("\n9. Display Circular Linked List"); printf("\n10. Exit"); printf("\n------------------------------------"); printf("\nEnter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("\nEnter the data: "); scanf("%d", &data); head = insertAtEnd(head, data); break; case 2: printf("\nEnter the data: "); scanf("%d", &data); head = insertAtBeginning(head, data); break; case 3: printf("\nEnter the data: "); scanf("%d", &data); head = insertAtEnd(head, data); break; case 4: printf("\nEnter the data: "); scanf("%d", &data); printf("Enter the position: "); scanf("%d", &position); head = insertAtPosition(head, data, position); break; case 5: head = deleteFromBeginning(head); break; case 6: head = deleteFromEnd(head); break; case 7: printf("\nEnter the position: "); scanf("%d", &position); head = deleteFromPosition(head, position); break; case 8: printf("\nEnter the key to search: "); scanf("%d", &key); int pos = search(head, key); if(pos == 0) printf("\n%d not found in the Circular Linked List!", key); else printf("\n%d found at position %d in Circular Linked List", key, pos); break; case 9: display(head); break; case 10: printf("\nExiting the Program..."); exit(0); default: printf("\nInvalid choice! Please try again..."); } } while(choice != 10); return 0;}
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.