以下是一个用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. 定义了一个单链表的节点类型,并实现了动态创建单链表的功能。
  2. 实现了向单链表中插入节点的操作,可以在指定位置插入一个节点。
  3. 实现了从单链表中删除节点的操作,可以删除指定位置的节点。
  4. 实现了从单链表中取节点数据的操作,可以获取指定位置节点的数据值。

通过这个代码的实现,可以体会到链表的插入、删除和取值操作相对于数组来说更加灵活和高效。链表的动态创建和节点操作的实现也是链表这种数据结构的特点之一

用c语言编写一个完整可运行代码实现以下功能1、定义单链表类型并动态创建单链表2、实现线性表链式存储结构下元素的插入操作3、实现线性表链式存储结构下元素的删除操作4、实现线性表链式存储结构下取元素操作对以上提取体会心得

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

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