用C语言写一个万能单向链表

#include <stdio.h>
#include <stdlib.h>

// 结点结构体 
typedef struct Node {
    void *data;      // void指针,可以存储任意类型数据
    struct Node *next; 
} Node;

// 链表结构体
typedef struct List {
    Node *head;
    Node *tail;
} List;

// 初始化链表 
void initList(List *list) {
    list->head = list->tail = NULL;
}

// 向链表尾部添加元素
void addTail(List *list, void *data) {
    Node *newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    
    if (list->tail == NULL) {
        list->head = list->tail = newNode;
    } else {
        list->tail->next = newNode;
        list->tail = newNode;
    }
}

// 向链表头部添加元素
void addHead(List *list, void *data) {
    Node *newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    
    if (list->head == NULL) {
        list->head = list->tail = newNode;
        newNode->next = NULL;
    } else {
        newNode->next = list->head;
        list->head = newNode;
    }
}

// 删除链表头部元素
void deleteHead(List *list) {
    if (list->head == NULL) return;
    
    Node *next = list->head->next;
    free(list->head);
    list->head = next;
}

// 删除链表尾部元素 
void deleteTail(List *list) {
    if (list->head == NULL) return;
    
    Node *cur = list->head;
    Node *pre = cur;
    
    while (cur->next != NULL) {
        pre = cur;
        cur = cur->next;
    }
    
    free(cur);
    pre->next = NULL;
    list->tail = pre;
}

int main() {
    List list;
    initList(&list);
    
    addTail(&list, "a");
    addTail(&list, "b");
    addHead(&list, "c");
    
    deleteHead(&list);
    deleteTail(&list);
}

当然可以。在C语言中,我们可以使用结构体定义链表节点,并编函数来删除特定的元素。这里是一个简单的单向链表删除节点的操作示例: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 创建一个新的链表节点 Node* createNode(int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode != NULL) { newNode->data = value; newNode->next = NULL; } return newNode; } // 删除给定值的节点(如果存在) void deleteNode(Node** head, int value) { // 检查链表是否为空 if (*head == NULL) { printf("链表为空,无法删除。\n"); return; } // 遍历链表 Node* current = *head; Node* previous = NULL; while (current != NULL && current->data != value) { previous = current; current = current->next; } // 如果找到节点,则删除它 if (current != NULL) { if (previous == NULL) { // 头节点 *head = current->next; } else { previous->next = current->next; } free(current); // 释放内存 } else { printf("找不到要删除的节点。\n"); } } // 打印链表(仅用于测试) void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { Node* head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); printf("删除前的链表:\n"); printList(head); deleteNode(&head, 2); printf("删除后的链表:\n"); printList(head); return 0; } ``` 在这个例子中,`deleteNode` 函数接受头指针 `*head` 和一个整数值 `value`,并尝试从链表中删除第一个找到该值的节点。请注意,在实际应用中,你需要处理边界条件以及内存管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值