Circular Linked ListCode CrazeMar 14, 20231 min readA circular linked list is a type of linked list where the last node in the list points back to the first node, forming a circle. #include <stdio.h>#include <stdlib.h>struct Node { int data; struct Node* next;};struct CircularLinkedList { struct Node* head; int count;};struct Node* create_node(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; return node;}struct CircularLinkedList* create_list() { struct CircularLinkedList* list = (struct CircularLinkedList*)malloc(sizeof(struct CircularLinkedList)); list->head = NULL; list->count = 0; return list;}void append(struct CircularLinkedList* list, int data) { struct Node* node = create_node(data); if (list->head == NULL) { list->head = node; node->next = node; // make the node point to itself } else { node->next = list->head->next; list->head->next = node; list->head = node; } list->count++;}int remove_node(struct CircularLinkedList* list, int data) { if (list->head == NULL) { return 0; } struct Node* current = list->head; struct Node* prev = NULL; do { if (current->data == data) { if (current == list->head) { list->head = prev; } prev->next = current->next; free(current); list->count--; return 1; } prev = current; current = current->next; } while (current != list->head->next); return 0;}void print_list(struct CircularLinkedList* list) { if (list->head == NULL) { return; } struct Node* current = list->head->next; do { printf("%d ", current->data); current = current->next; } while (current != list->head->next); printf("\n");}int main() { struct CircularLinkedList* list = create_list(); append(list, 1); append(list, 2); append(list, 3); print_list(list); // output: 1 2 3 remove_node(list, 2); print_list(list); // output: 1 3 return 0;}
A circular linked list is a type of linked list where the last node in the list points back to the first node, forming a circle. #include <stdio.h>#include <stdlib.h>struct Node { int data; struct Node* next;};struct CircularLinkedList { struct Node* head; int count;};struct Node* create_node(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; return node;}struct CircularLinkedList* create_list() { struct CircularLinkedList* list = (struct CircularLinkedList*)malloc(sizeof(struct CircularLinkedList)); list->head = NULL; list->count = 0; return list;}void append(struct CircularLinkedList* list, int data) { struct Node* node = create_node(data); if (list->head == NULL) { list->head = node; node->next = node; // make the node point to itself } else { node->next = list->head->next; list->head->next = node; list->head = node; } list->count++;}int remove_node(struct CircularLinkedList* list, int data) { if (list->head == NULL) { return 0; } struct Node* current = list->head; struct Node* prev = NULL; do { if (current->data == data) { if (current == list->head) { list->head = prev; } prev->next = current->next; free(current); list->count--; return 1; } prev = current; current = current->next; } while (current != list->head->next); return 0;}void print_list(struct CircularLinkedList* list) { if (list->head == NULL) { return; } struct Node* current = list->head->next; do { printf("%d ", current->data); current = current->next; } while (current != list->head->next); printf("\n");}int main() { struct CircularLinkedList* list = create_list(); append(list, 1); append(list, 2); append(list, 3); print_list(list); // output: 1 2 3 remove_node(list, 2); print_list(list); // output: 1 3 return 0;}
Stack Using QueueA stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue...
コメント