第二章-单链表

下面是单链表(Singly Linked List)的各个基本操作的代码实现:

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

typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 初始化单链表
void initList(Node** head) {
    *head = NULL;
}

// 判断单链表是否为空
int isEmpty(Node* head) {
    return head == NULL;
}

// 在链表头部插入元素
void insertAtHead(Node** head, int element) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = element;
    newNode->next = *head;
    *head = newNode;
}

// 在链表尾部插入元素
void insertAtTail(Node** head, int element) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = element;
    newNode->next = NULL;
    
    if (*head == NULL) {
        *head = newNode;
    } else {
        Node* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

// 在指定位置插入元素
void insertAtPosition(Node** head, int position, int element) {
    if (position <= 0) {
        printf("Error: Invalid position\n");
        return;
    }
    
    if (position == 1) {
        insertAtHead(head, element);
    } else {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = element;
        
        Node* current = *head;
        int count = 1;
        while (current != NULL && count < position - 1) {
            current = current->next;
            count++;
        }
        
        if (current == NULL) {
            printf("Error: Invalid position\n");
            return;
        }
        
        newNode->next = current->next;
        current->next = newNode;
    }
}

// 删除链表中第一个出现的指定元素
void deleteElement(Node** head, int element) {
    Node* previous = NULL;
    Node* current = *head;
    
    while (current != NULL && current->data != element) {
        previous = current;
        current = current->next;
    }
    
    if (current == NULL) {
        printf("Error: Element not found\n");
        return;
    }
    
    if (previous == NULL) {
        *head = current->next;
    } else {
        previous->next = current->next;
    }
    
    free(current);
}

// 获取链表长度
int getLength(Node* head) {
    int length = 0;
    Node* current = head;
    
    while (current != NULL) {
        length++;
        current = current->next;
    }
    
    return length;
}

// 打印链表中的元素
void printList(Node* head) {
    printf("Linked List: ");
    Node* current = head;
    
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    
    printf("\n");
}

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

int main() {
    Node* head;
    initList(&head);
    
    insertAtHead(&head, 10);
    insertAtHead(&head, 20);
    insertAtTail(&head, 30);
    insertAtPosition(&head, 2, 40);
    
    printList(head);
    
    deleteElement(&head, 20);
    
    printList(head);
    
    int length = getLength(head);
    printf("Length of the linked list: %d\n", length);
    
    destroyList(&head);
    
    return 0;
}
```

这段代码实现了单链表的基本操作,包括初始化单链表 `initList`,判断单链表是否为空 `isEmpty`,在链表头部插入元素 `insertAtHead`,在链表尾部插入元素 `insertAtTail`,在指定位置插入元素 `insertAtPosition`,删除链表中第一个出现的指定元素 `deleteElement`,获取链表长度 `getLength`,打印链表中的元素 `printList`,以及销毁链表,释放内存 `destroyList`。

在 `main` 函数中,我们演示了如何使用这些基本操作来创建链表、插入元素、删除元素、获取链表长度,并打印链表的内容。

需要注意的是,这里使用了指针作为参数,通过传递指向指针的指针来修改头指针的值,以便在插入和删除操作中对链表进行修改。此外,这只是一个简单的示例,实际应用中可能需要根据具体需求进行修改和扩展。

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

typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 初始化单链表
void initList(Node** head) {
    *head = NULL;
}

// 判断单链表是否为空
int isEmpty(Node* head) {
    return head == NULL;
}

// 在链表头部插入元素
void insertAtHead(Node** head, int element) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = element;
    newNode->next = *head;
    *head = newNode;
}

// 在链表尾部插入元素
void insertAtTail(Node** head, int element) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = element;
    newNode->next = NULL;
    
    if (*head == NULL) {
        *head = newNode;
    } else {
        Node* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

// 在指定位置插入元素
void insertAtPosition(Node** head, int position, int element) {
    if (position <= 0) {
        printf("Error: Invalid position\n");
        return;
    }
    
    if (position == 1) {
        insertAtHead(head, element);
    } else {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = element;
        
        Node* current = *head;
        int count = 1;
        while (current != NULL && count < position - 1) {
            current = current->next;
            count++;
        }
        
        if (current == NULL) {
            printf("Error: Invalid position\n");
            return;
        }
        
        newNode->next = current->next;
        current->next = newNode;
    }
}

// 删除链表中第一个出现的指定元素
void deleteElement(Node** head, int element) {
    Node* previous = NULL;
    Node* current = *head;
    
    while (current != NULL && current->data != element) {
        previous = current;
        current = current->next;
    }
    
    if (current == NULL) {
        printf("Error: Element not found\n");
        return;
    }
    
    if (previous == NULL) {
        *head = current->next;
    } else {
        previous->next = current->next;
    }
    
    free(current);
}

// 获取链表长度
int getLength(Node* head) {
    int length = 0;
    Node* current = head;
    
    while (current != NULL) {
        length++;
        current = current->next;
    }
    
    return length;
}

// 打印链表中的元素
void printList(Node* head) {
    printf("Linked List: ");
    Node* current = head;
    
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    
    printf("\n");
}

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

int main() {
    Node* head;
    initList(&head);
    
    insertAtHead(&head, 10);
    insertAtHead(&head, 20);
    insertAtTail(&head, 30);
    insertAtPosition(&head, 2, 40);
    
    printList(head);
    
    deleteElement(&head, 20);
    
    printList(head);
    
    int length = getLength(head);
    printf("Length of the linked list: %d\n", length);
    
    destroyList(&head);
    
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要什么私信我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值