链表的高级使用C语言代码示例

以下是一个C语言的高级链表使用示例,演示了如何实现一个双向链表(doubly linked list)并执行一些高级操作,包括在链表中插入、删除和反转元素。这个示例包括以下操作:

  1. 创建双向链表。
  2. 向链表头部和尾部插入元素。
  3. 删除链表中的特定元素。
  4. 遍历链表并打印元素。
  5. 反转链表。
#include <stdio.h>
#include <stdlib.h>

// 定义双向链表节点的结构
struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
};

// 创建新的节点
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode != NULL) {
        newNode->data = data;
        newNode->prev = NULL;
        newNode->next = NULL;
    }
    return newNode;
}

// 在链表头部插入节点
void insertAtHead(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
    } else {
        newNode->next = *head;
        (*head)->prev = newNode;
        *head = newNode;
    }
}

// 在链表尾部插入节点
void insertAtTail(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
        newNode->prev = current;
    }
}

// 从链表中删除特定元素
void deleteNode(struct Node** head, int data) {
    if (*head == NULL) {
        return;
    }
    struct Node* current = *head;
    while (current != NULL) {
        if (current->data == data) {
            if (current->prev != NULL) {
                current->prev->next = current->next;
            } else {
                *head = current->next;
            }
            if (current->next != NULL) {
                current->next->prev = current->prev;
            }
            free(current);
            return;
        }
        current = current->next;
    }
}

// 遍历并打印链表
void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

// 反转链表
void reverseList(struct Node** head) {
    struct Node* current = *head;
    struct Node* prev = NULL;
    struct Node* next = NULL;
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        current->prev = next;
        prev = current;
        current = next;
    }
    *head = prev;
}

int main() {
    struct Node* head = NULL;

    insertAtHead(&head, 1);
    insertAtHead(&head, 2);
    insertAtHead(&head, 3);
    insertAtTail(&head, 4);
    insertAtTail(&head, 5);

    printf("链表内容:\n");
    printList(head);

    deleteNode(&head, 3);

    printf("删除元素3后的链表内容:\n");
    printList(head);

    reverseList(&head);

    printf("反转链表后的链表内容:\n");
    printList(head);

    return 0;
}

这个示例中,我们创建了一个双向链表,然后在链表头部和尾部插入元素,删除特定元素,遍历链表并打印元素,最后反转链表。这展示了链表的高级用法和操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值