C语言链表操作:扩大两倍 & 按素数拆分

本文将介绍两种常见的链表操作,使用C语言实现:

  1. 链表扩大两倍并变成前后对称2. 根据元素是否为素数拆分链表

1. 链表扩大两倍并变成前后对称c#include <stdio.h>#include <stdlib.h>

// 链表节点结构typedef struct Node { int data; struct Node* next;} Node;

// 在链表末尾插入节点void insert(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL;

if (*head == NULL) {        *head = newNode;    } else {        Node* current = *head;        while (current->next != NULL) {            current = current->next;        }        current->next = newNode;    }}

// 链表扩大为2倍大小并变成前后对称链表void doubleLinkedList(Node** head) { if (*head == NULL) { return; }

Node* current = *head;    Node* tail = *head;

// 找到链表尾节点    while (tail->next != NULL) {        tail = tail->next;    }

// 遍历链表并在尾节点后插入原节点的值    while (current != NULL) {        Node* newNode = (Node*)malloc(sizeof(Node));        newNode->data = current->data;        newNode->next = NULL;

    tail->next = newNode;        tail = newNode;

    current = current->next;    }}

// 打印链表节点void printLinkedList(Node* head) { Node* current = head; while (current != NULL) { printf('%d ', current->data); current = current->next; } printf(' ');}

// 释放链表内存void freeLinkedList(Node* head) { Node* current = head; while (current != NULL) { Node* temp = current; current = current->next; free(temp); }}

int main() { // 创建原始链表 Node* head = NULL; insert(&head, 3); insert(&head, 5); insert(&head, 6); insert(&head, 8); insert(&head, 9); insert(&head, 11); insert(&head, 14); insert(&head, 15);

printf('原始链表:');    printLinkedList(head);

// 链表扩大为2倍大小并变成前后对称链表    doubleLinkedList(&head);

printf('扩大后的链表:');    printLinkedList(head);

// 释放链表内存    freeLinkedList(head);

return 0;}

2. 按素数拆分链表c#include <stdio.h>#include <stdlib.h>

// 链表节点结构typedef struct Node { int data; struct Node* next;} Node;

// 在链表末尾插入节点void insert(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL;

if (*head == NULL) {        *head = newNode;    } else {        Node* current = *head;        while (current->next != NULL) {            current = current->next;        }        current->next = newNode;    }}

// 检查一个数是否为素数int isPrime(int num) { if (num <= 1) { return 0; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return 0; } } return 1;}

// 拆分链表void splitLinkedList(Node** head, Node** primeHead, Node** nonPrimeHead) { if (*head == NULL) { return; }

Node* current = *head;    while (current != NULL) {        Node* nextNode = current->next;                if (isPrime(current->data)) {            // 插入素数链表            current->next = *primeHead;            *primeHead = current;        } else {            // 插入非素数链表            current->next = *nonPrimeHead;            *nonPrimeHead = current;        }                current = nextNode;    }

*head = NULL;}

// 打印链表节点void printLinkedList(Node* head) { Node* current = head; while (current != NULL) { printf('%d ', current->data); current = current->next; } printf(' ');}

// 释放链表内存void freeLinkedList(Node* head) { Node* current = head; while (current != NULL) { Node* temp = current; current = current->next; free(temp); }}

int main() { // 创建原始链表 Node* head = NULL; insert(&head, 3); insert(&head, 5); insert(&head, 6); insert(&head, 8); insert(&head, 9); insert(&head, 11); insert(&head, 14); insert(&head, 15);

printf('原始链表:');    printLinkedList(head);

// 拆分链表    Node* primeHead = NULL;  // 素数链表头节点    Node* nonPrimeHead = NULL;  // 非素数链表头节点    splitLinkedList(&head, &primeHead, &nonPrimeHead);

printf('素数链表:');    printLinkedList(primeHead);    printf('非素数链表:');    printLinkedList(nonPrimeHead);

// 释放链表内存    freeLinkedList(primeHead);    freeLinkedList(nonPrimeHead);

return 0;}

希望以上代码能够帮助您理解如何在 C 语言中实现链表的合并和拆分操作。


原文地址: https://www.cveoy.top/t/topic/bsWr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录