链表的基本操作(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");
        }
    }
}

三、运行结果

相关推荐
兰令水5 小时前
hot100【acm版】【2026.7.14打卡-java版本】
java·数据结构·算法·leetcode·面试
cpp_25015 小时前
P10098 [ROIR 2023] 地铁建设 (Day 2)
数据结构·c++·算法·贪心·二分答案·洛谷题解
米尔的可达鸭8 小时前
深入操作系统 Socket 底层:套接字控制块、FD映射、阻塞IO核心完整实现
arm开发·数据结构·websocket·网络协议·算法·架构·安全架构
tachibana28 小时前
hot100 课程表(207)
java·数据结构·算法·leetcode
Frostnova丶9 小时前
(15)LeetCode 189. 轮转数组
数据结构·算法·leetcode
罗超驿10 小时前
6.Java LinkedList深度解析:从链表原理到源码实践
java·开发语言·链表
来一碗刘肉面11 小时前
顺序表的按值查找
数据结构·c++·算法
延凡科技11 小时前
延凡科技 - 发生器设备管理控制系统:AI 驱动的全生命周期智能管控方案
大数据·数据结构·人工智能·算法
小肝一下12 小时前
2. 顺序表和链表
c语言·数据结构·c++·链表·dijkstra·依蕾娜·香香的依蕾娜
j7~12 小时前
【数据结构初阶】带头双向循环链表 (DSList) --增删查改完整实现详解
c语言·数据结构·链表·带头双向循环链表