simplify this code: #include <stdio.h> #include <stdlib.h> struct Node { int x; int y;…
- User
simplify 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
- User
- Assistant
This code represents a linked list data structure. Each node in the linked list 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 the nodes based on the x values of the integers.
In the main function, an empty linked list is created and populated with nodes containing various integers. The original list is printed, and then all nodes are deleted one by one and printed as they are deleted. The original list is then restored by recreating it, and the restored list is printed.
The code then searches for a specific element in the list and deletes it if found. The modified list is printed.
The list is then sorted based on the x values of the integers, and the sorted list is printed.
Finally, the order of the list is reversed, and the reversed list is printed. Memory is freed and clean up is performed.
Overall, this code demonstrates the creation, manipulation, and deletion of nodes in a linked list, as well as sorting and reversing the list.