链表的使用

本文详细介绍了如何在C++中实现单链表的基本操作,包括初始化、遍历、插入、删除和清空等,以及辅助函数如链表长度计算和节点值查找。适合初学者理解链表数据结构和常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对链表的操作与其他存储结构类似,无非也就是“增删改查”。
结构体指针变成结构体变量,需要经过动态内存申请,如下面所示:

struct List* node = (struct List*)malloc(sizeof(struct List));

在写程序时,每个功能的程序模块化,更合适。
创建链表时:

void Init(struct List* L) {
    int cur;
    cin >> cur;
    while (cur != -1) {
        struct List* node = (struct List*)malloc(sizeof(struct List));
        node->data = cur;
        node->next = NULL;
        L->next = node;
        L = L->next;
        cin >> cur;
    }
}

遍历链表时:

void Show(struct List* L) { //遍历链表值
    cout << "链表遍历:";
    while (L->next) {
        cout << L->next->data << " ";
        L = L->next;
    }
    cout << endl;
}

单链表的插入,在特定位置插入节点时:

//在第K个位置前插入data元素,最后链表的第K个位置就是data
void InsertList(struct List* L, int k, int data) {
    struct List* pre = NULL; //存储第K-1个元素的值
    struct List* node = (struct List*)malloc(sizeof(struct List));   //申请空间
    node->data = data;
    node->next = NULL;
    while (k && L->next) {  //通过K的不断减小,来移动pre指针,找到data数据的插入点
        pre = L;
        L = L->next;
        k--;
    }
    if (k > 0) { //如果K > 0,说明K的插入点位置超过链表长度,故直接插到链表的表尾
        L->next = node;
        L = L->next;
    }
    else { //K=0说明找到了插入点的位置
        pre->next = node;    //链接链表
        node->next = L;
    }
}

单链表中,当链表中没有重复值时,删除值为X的某个节点时:

void DeleteList(struct List* L, int x) {   //删除值为x的结点
    if (lengthList(L) <= 0) { //先判空
        cout << "表空,没有元素可删除" << endl;
        return;
    }
    struct List* node = L->next;
    struct List* pre = L; 
    while (node) {
        if (node->data == x) {
            pre->next = node->next; 
            free(node); 
            return;
        }
        pre = node;
        node = pre->next;
    }
}

单链表中,删除第K个位置上的节点时:

void DeleteList_Position(struct List* L, int k) { 
    if (lengthList(L) <= 0) {
        cout << "表空,没有元素可删除" << endl;
        return;
    }
    struct List* node = L->next;
    struct List* pre = L;  
    k = k - 1;  //因为如果k = 1,直接用pre->next = node->next就把node删掉了,所以要减1
    while (k-- && node) {
        pre = node;
        node = node->next;
    }
    if (node == NULL || k > 0) {
        cout << "要删除的位置不存在" << endl;
    }
    else {
        pre->next = node->next; 
        free(node);
    }
}

清空链表时:

void ClearList(struct List* L) {  
    struct List* node = L;
    if (lengthList(L) > 0) {
        while (node->next) {
            struct List* temp = node->next;
            node->next = node->next->next;
            free(temp); 
        }
    }
}

求链表长度时:

int lengthList(struct List* L) {
    int len = 0;
    while (L->next) {
        len++;
        L = L->next;
    }
    return len;
}

链表判空时:

bool IsEmptyList(struct List* L) { 
    if (L->next == NULL) {
        return true;
    }
    else {
        return false;
    }
}

返回链表的第i个位置上的值时:

int GetElemList(struct List* L, int i) {
    struct List* node = L;
    int k = i;  //标记i的值,以防不存在输出显示
    while (i > 0 && node->next) {
        node = node->next;
        i--;
    }
    if (i == 0 && node != NULL) { //当i == 0 和 node 不为空代表找到了第i个位置的元素
        return node->data;
    }
    else {
        cout << "第" << k << "个位置不存在" << endl;
        return -1;
    }
}

单链表的主函数如下:

#include <iostream>
using namespace std;

struct List {
    int data;
    struct List* next;
};

int main() {
    struct List* head = (struct List*)malloc(sizeof(struct List));  //头结点(不存值)
    head->next = NULL;
    Init(head); //初始化链表
    Show(head);  //遍历链表

    int i, data;
    cout << "请输入要插入的位置和值:";
    cin >> i;
    cin >> data;
    InsertList(head, i, data); //在第i个位置前插入data
    Show(head); //遍历链表

    int x;
    cout << "请输入要删除的值: ";
    cin >> x;
    DeleteList(head, x); //删除链表中值为x的结点(链表值无重复)
    Show(head); //遍历链表

    int position;
    cout << "请输入要删除的位置: ";
    cin >> position;
    DeleteList_Position(head, position);
    Show(head);

    if (IsEmptyList(head))
        cout << "链表是空链表!" << endl;
    else
        cout << "链表不空!" << endl;
    cout << "链表的长度为: " << lengthList(head) << endl;

    int n;
    cout << "请输入要查找的位置: ";
    cin >> n;
    if (GetElemList(head, n) != -1)
        cout << "第" << n << "个位置的值为: " << GetElemList(head, n) << endl;

    cout << "没有清空链表前链表长度 : " << lengthList(head) << endl;
    ClearList(head);
    cout << "清空链表后链表长度 : " << lengthList(head) << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值