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

三、运行结果

相关推荐
Darling噜啦啦7 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠8 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾8 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres8218 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q8 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒8 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
疯狂成瘾者8 天前
Java 集合 LinkedList 详解:链表结构、常用方法和队列使用
java·开发语言·链表
WL学习笔记8 天前
单项不带头不循环链表
数据结构·链表
小糯米6018 天前
JS 数组
数据结构·算法·排序算法
小欣加油8 天前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展