C语言单链表实现:插入、删除、取值操作

以下是一个用C语言实现的单链表的完整可运行代码,包含创建、插入、删除和获取节点数据的完整功能:

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

// 定义单链表节点类型
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 创建单链表
Node* createLinkedList(int n) {
    Node* head = NULL;
    Node* tail = NULL;

    for (int i = 0; i < n; i++) {
        int data;
        printf("请输入第%d个节点的值:", i + 1);
        scanf("%d", &data);

        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = data;
        newNode->next = NULL;

        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }    
    }

    return head;
}

// 插入节点
void insertNode(Node** head, int position, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;

    if (position == 0) {
        newNode->next = *head;
        *head = newNode;
    } else {
        Node* current = *head;
        for (int i = 0; i < position - 1; i++) {
            if (current == NULL) {
                printf("插入位置无效!\n");
                return;
            }
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }
}

// 删除节点
void deleteNode(Node** head, int position) {
    if (*head == NULL) {
        printf("链表为空,无法删除节点!\n");
        return;
    }

    Node* current = *head;
    Node* previous = NULL;

    if (position == 0) {
        *head = current->next;
        free(current);
    } else {
        for (int i = 0; i < position; i++) {
            if (current == NULL) {
                printf("删除位置无效!\n");
                return;
            }
            previous = current;
            current = current->next;
        }
        previous->next = current->next;
        free(current);
    }
}

// 取节点数据
int getNodeData(Node* head, int position) {
    Node* current = head;

    for (int i = 0; i < position; i++) {
        if (current == NULL) {
            printf("取节点数据位置无效!\n");
            return -1;
        }
        current = current->next;
    }

    if (current == NULL) {
        printf("取节点数据位置无效!\n");
        return -1;
    }

    return current->data;
}

// 打印链表
void printLinkedList(Node* head) {
    Node* current = head;

    printf("链表的元素为:");
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    int n;
    printf("请输入链表的长度:");
    scanf("%d", &n);

    Node* head = createLinkedList(n);
    printLinkedList(head);

    int insertPosition, insertData;
    printf("请输入要插入的节点位置和值:");
    scanf("%d %d", &insertPosition, &insertData);
    insertNode(&head, insertPosition, insertData);
    printLinkedList(head);

    int deletePosition;
    printf("请输入要删除的节点位置:");
    scanf("%d", &deletePosition);
    deleteNode(&head, deletePosition);
    printLinkedList(head);

    int getPosition;
    printf("请输入要取节点数据的位置:");
    scanf("%d", &getPosition);
    int nodeData = getNodeData(head, getPosition);
    if (nodeData != -1) {
        printf("取到的节点数据为:%d\n", nodeData);
    }

    return 0;
}

代码功能

该代码实现了以下功能:

  1. 定义单链表节点类型: 使用 struct Node 定义了单链表节点的结构,包含数据域 data 和指向下一个节点的指针 next
  2. 动态创建单链表: createLinkedList 函数根据用户输入的长度动态创建单链表,并返回链表的头节点指针。
  3. 插入节点: insertNode 函数在指定位置插入新节点,如果位置无效则提示错误。
  4. 删除节点: deleteNode 函数删除指定位置的节点,如果位置无效或链表为空则提示错误。
  5. 取节点数据: getNodeData 函数获取指定位置的节点数据,如果位置无效则返回 -1。
  6. 打印链表: printLinkedList 函数将链表的所有节点数据输出。

理解体会

通过这个代码的实现,可以体会到链表的以下特点:

  • 灵活: 链表的插入和删除操作不需要移动其他元素,只需要修改指针指向即可,因此更加灵活。
  • 高效: 在插入或删除元素时,链表不需要像数组那样进行大量的元素移动操作,效率更高。
  • 动态分配: 链表节点可以动态地分配内存,可以根据需要增加或减少节点数量。

希望这个代码可以帮助你更好地理解单链表的实现原理和操作方式。

C语言单链表实现:插入、删除、取值操作

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

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