specify this code: #include <stdio.h> #include <stdlib.h> struct Node { int x; int y;…
- User
specify this code:
#include <stdio.h> #include <stdlib.h>
struct Node { int x; int y; struct Node* next; };
typedef struct Node Node;
// Function to create a new node with the given data Node* createNode(int x, int y) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->x = x; newNode->y = y; newNode->next = NULL; return newNode; }
// Function to print the linked list void printList(Node* head) { Node* current = head; printf("[ "); while (current != NULL) { printf("(%d,%d) ", current->x, current->y); current = current->next; } printf("]\n"); }
// Function to delete a node with the specified data void deleteNode(Node** head, int x, int y) { Node* current = head; Node prev = NULL;
while (current != NULL) { if (current->x == x && current->y == y) { if (prev == NULL) { *head = current->next; } else { prev->next = current->next; } free(current); return; } prev = current; current = current->next; }}
int main() { // Create an empty linked list Node* originalList = NULL;
// Populate the linked list with integers originalList = createNode(3, 6); originalList->next = createNode(5, 4); originalList->next->next = createNode(4, 11); originalList->next->next->next = createNode(13, 30); originalList->next->next->next->next = createNode(12, 2); originalList->next->next->next->next->next = createNode(10, 1); // Print the original list printf("Original List: "); printList(originalList); // Delete all nodes one by one and print them as they are deleted Node* current = originalList; while (current != NULL) { printf("Deleted value: (%d,%d)\n", current->x, current->y); Node* next = current->next; free(current); current = next; } originalList = NULL; printf("List after deleting all items: "); printList(originalList); // Restore the original list by recreating it originalList = createNode(3, 6); originalList->next = createNode(5, 4); originalList->next->next = createNode(4, 11); originalList->next->next->next = createNode(13, 30); originalList->next->next->next->next = createNode(12, 2); originalList->next->next->next->next->next = createNode(10, 1); printf("Restored List: "); printList(originalList); // Search for and delete a specific element int searchX = 5; int searchY = 4; current = originalList; Node* prev = NULL; while (current != NULL) { if (current->x == searchX && current->y == searchY) { printf("Element found: (%d,%d)\n", current->x, current->y); deleteNode(&originalList, searchX, searchY); printf("List after deleting an item: "); printList(originalList); } prev = current; current = current->next; } if (current == NULL) { printf("Element not found.\n"); } // Sort the list based on the x values of the integers int swapped; Node* lastNode = NULL; do { swapped = 0; current = originalList; while (current->next != lastNode) { if (current->x > current->next->x) { int tempX = current->x; int tempY = current->y; current->x = current->next->x; current->y = current->next->y; current->next->x = tempX; current->next->y = tempY; swapped = 1; } current = current->next; } lastNode = current; } while (swapped); printf("List after sorting the data: "); printList(originalList); // Reverse the order of the list Node* prevNode = NULL; Node* nextNode = NULL; current = originalList; while (current != NULL) { nextNode = current->next; current->next = prevNode; prevNode = current; current = nextNode; } originalList = prevNode; printf("List after reversing the data: "); printList(originalList); // Free memory and clean up while (originalList != NULL) { Node* next = originalList->next; free(originalList); originalList = next; } return 0; - Assistant
The code defines a singly linked list data structure where each node contains two integers, x and y. The code provides functions to create a new node, print the linked list, delete a node with specified data, and sort/reverse the list.
The main function creates an empty linked list and populates it with nodes containing integers. It then prints the original list and proceeds to delete all nodes one by one. After deleting all the nodes, it restores the original list and prints it again. It then searches for a specific element and deletes it if found, printing the list after deletion. Finally, it sorts the list based on the x values of the integers, reverses the list, frees the memory, and cleans up.
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.