链表的基本操作(C语言版)

如何初始化链表、插入元素、删除元素、查找元素?(C语言)

一、实验内容:

1.编写链表基本操作函数。

(1)InitList(LIST *L,int ms)::初始化链表。

(2)InsertListl(LIST *L,int item,int rc):向链表指定位置插入元素。

(3)InsertList2(LIST *L,int item,int rc):向有序链表指定位置插入元素。

(4)DeleteList(LIST *L,int item):删除指定元素值的链表记录。

(5)FindList(LIST *L,int item):查找链表中的元素。

(6)OutputList(LIST *L):输出链表元素。

二、代码如下:

c 复制代码
#include <stdio.h>
#include <stdlib.h>

// 定义链表结点结构
typedef struct Node {
    int data;           // 数据域
    struct Node *next;  // 指针域
} Node;

// 定义链表类型(指向头结点)
typedef Node* LIST;

// ==========================================
// (1) 初始化链表(创建头结点)
// ==========================================
void InitList(LIST *L, int ms) {
    *L = (Node*)malloc(sizeof(Node));
    if(*L == NULL){
        printf("内存分配失败!\n");
        exit(0);
    }
    (*L)->next = NULL;
    printf("====链表初始化完成====\n");
}

// ==========================================
// (2) 指定位置插入元素 InsertList1
// rc:插入位置(从 1 开始)
// ==========================================
int InsertList1(LIST *L, int item, int rc) {
    Node *p = *L;
    int i = 0;
    while (p != NULL && i < rc - 1) {
        p = p->next;
        i++;
    }
    if (p == NULL || i > rc - 1) {
        printf("插入位置非法!\n");
        return 0;
    }
    Node *s = (Node*)malloc(sizeof(Node));
    s->data = item;
    s->next = p->next;
    p->next = s;
    printf("元素 %d 在位置 %d 插入成功!\n", item, rc);
    return 1;
}

// ==========================================
// (3) 有序链表插入 InsertList2(保持升序)
// ==========================================
int InsertList2(LIST *L, int item) {
    Node *p = *L;
    while (p->next != NULL && p->next->data < item) {
        p = p->next;
    }
    Node *s = (Node*)malloc(sizeof(Node));
    s->data = item;
    s->next = p->next;
    p->next = s;
    printf("有序插入元素 %d 成功!\n", item);
    return 1;
}

// ==========================================
// (4) 删除指定值的结点 DeleteList
// ==========================================
int DeleteList(LIST *L, int item) {
    Node *p = *L;
    while (p->next != NULL && p->next->data != item) {
        p = p->next;
    }
    if (p->next == NULL) {
        printf("未找到元素 %d,删除失败!\n", item);
        return 0;
    }
    Node *q = p->next;
    p->next = q->next;
    free(q);
    printf("元素 %d 删除成功!\n", item);
    return 1;
}

// ==========================================
// (5) 查找元素 FindList,找到返回位置,失败返回 0
// ==========================================
int FindList(LIST *L, int item) {
    Node *p = (*L)->next;
    int pos = 1;
    while (p != NULL) {
        if (p->data == item) {
            printf("找到元素 %d,位置是 %d\n", item, pos);
            return pos;
        }
        p = p->next;
        pos++;
    }
    printf("未找到元素 %d\n", item);
    return 0;
}

// ==========================================
// (6) 输出链表所有元素 OutputList
// ==========================================
void OutputList(LIST *L) {
    Node *p = (*L)->next;
    if (p == NULL) {
        printf("链表为空!\n");
        return;
    }
    printf("链表元素:");
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

//销毁链表
void DestroyList(LIST *L)
{
    Node *p = *L,*q;
    while(p != NULL)
    {
        q = p->next;
        free(p);
        p = q;
    }
    *L = NULL;
}

//菜单打印
void Menu()
{
    printf("\n========单链表操作菜单========\n");
    printf("1. 指定位置插入数据\n");
    printf("2. 有序升序插入数据\n");
    printf("3. 按数值删除结点\n");
    printf("4. 按数值查找元素\n");
    printf("5. 遍历输出链表\n");
    printf("0. 退出程序\n");
    printf("=============================\n");
    printf("请输入功能序号:");
}

// ===================== 交互主函数 =====================
int main() {
    LIST L;
    InitList(&L,100);
    int op,val,pos;

    while(1)
    {
        Menu();
        scanf("%d",&op);
        switch(op)
        {
            case 1:
                printf("输入要插入的数据:");
                scanf("%d",&val);
                printf("输入插入位置(从1开始):");
                scanf("%d",&pos);
                InsertList1(&L,val,pos);
                break;
            case 2:
                printf("输入有序插入的数据:");
                scanf("%d",&val);
                InsertList2(&L,val);
                break;
            case 3:
                printf("输入要删除的数值:");
                scanf("%d",&val);
                DeleteList(&L,val);
                break;
            case 4:
                printf("输入要查找的数值:");
                scanf("%d",&val);
                FindList(&L,val);
                break;
            case 5:
                OutputList(&L);
                break;
            case 0:
                DestroyList(&L);
                printf("程序退出,内存释放完毕!\n");
                return 0;
            default:
                printf("输入错误,请输入0~5之间数字!\n");
        }
    }
}

三、运行结果

相关推荐
灵枢时代16 小时前
小程序在生鲜配送行业,如何设计夜间预约和次日达的订单处理流程?
数据结构·人工智能·小程序
一条大祥脚16 小时前
ABC468 扫描线|贡献法|二阶差分|线段树优化DP
数据结构·算法
普通攻击往后拉1 天前
Leetcode 206. 反转链表
算法·leetcode·链表
mifengxing1 天前
LeetCode 189 轮转数组|3种解法拆解,从暴力到O(1)原地最优解
数据结构·算法·leetcode
MIngYaaa5201 天前
2026暑期牛客多校3 2026-7-24
数据结构·算法·000
2501_937860941 天前
Java 集合底层深度剖析:Map 与 Set、二叉搜索树、哈希表全解
java·数据结构·散列表
Hommy882 天前
【剪映小助手】字符串列表转对象接口(Str List To Obds)
数据结构·list·剪映小助手·视频剪辑自动化·剪映api·开源剪映小助手
noipp2 天前
推荐题目:洛谷 P3726 [AHOI2017/HNOI2017] 抛硬币
c语言·数据结构·c++·算法·编程·洛谷·luogu
wabs6662 天前
关于链表【力扣19.删除链表的倒数第N个节点的思考】
数据结构·leetcode·链表
Rabitebla2 天前
C++ STL 之 set 详解:从底层红黑树到实际应用
开发语言·数据结构·c++·算法·leetcode