Untitled
unknown
plain_text
7 months ago
2.5 kB
1
Indexable
Never
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node { int data; struct Node* next; } Node; Node* create_node(int data) { Node* new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->next = NULL; return new_node; } void insert_node(Node** head, int data) { Node* new_node = create_node(data); if (*head == NULL) { *head = new_node; new_node->next = *head; } else { Node* temp = *head; while (temp->next != *head) { temp = temp->next; } temp->next = new_node; new_node->next = *head; } } char* convert_to_binary(int n) { char* binary = (char*)malloc(33 * sizeof(char)); int index = 0; for (int i = 31; i >= 0; i--) { int k = n >> i; if (k & 1) { binary[index++] = '1'; } else { binary[index++] = '0'; } } binary[index] = '\0'; return binary; } void delete_nth_elements(Node** head, int n) { if (*head == NULL || n <= 0) { return; } Node* current = *head; int count = 1; while (count < n && current->next != *head) { current = current->next; count++; } if (count < n) { return; } while (current->next != *head) { Node* temp = current->next; current->next = temp->next; free(temp); for (int i = 0; i < n - 1 && current->next != *head; i++) { current = current->next; } } } void print_list(Node* head) { if (head == NULL) { printf("List is empty.\n"); return; } Node* temp = head; do { printf("%s -> ", convert_to_binary(temp->data)); temp = temp->next; } while (temp != head); printf("\n"); } int main() { Node* head = NULL; int num_elements, element, n; printf("Introdu numarul de elemente din lista: "); scanf("%d", &num_elements); printf("Introdu elementele listei:\n"); for (int i = 0; i < num_elements; i++) { scanf("%d", &element); insert_node(&head, element); } printf("Initial list:\n"); print_list(head); printf("Introdu valoarea lui n: "); scanf("%d", &n); delete_nth_elements(&head, n); printf("List after deleting every %dth element:\n", n); print_list(head); return 0; }