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

三、运行结果

相关推荐
lightqjx1 小时前
【算法】数据结构_单调队列
数据结构·算法·单调队列
小四季豆1 小时前
《数据结构与算法》-顺序表:算法落地的第一个线性结构
c语言·数据结构·算法
Marianne Qiqi2 小时前
非hot100的力扣算法题
数据结构·算法·leetcode
lightqjx2 小时前
【算法】数据结构_单调栈
数据结构·算法·单调栈
计算机安禾3 小时前
【算法分析与设计】第44篇:随机化复杂度类:RP、BPP与去随机化猜想
java·数据结构·数据库·算法·机器学习
枕星而眠3 小时前
【数据结构】树与二叉树基础知识点总结
数据结构·c++·后端·算法·运维开发
fei_sun4 小时前
【SystemVerilog验证】数据类型(待补充)
数据结构·systemverilog
无忧.芙桃4 小时前
数据结构之单链表
c语言·开发语言·数据结构
HZ·湘怡4 小时前
二叉树 1
数据结构·算法·二叉树·