c++实现链表的基本操作

本文详述了使用C++进行链表的基本操作,包括创建链表、在指定位置插入节点、删除特定数值的节点、查询节点及获取链表长度,提供了亲测有效的代码示例。

链表的创建、插入、删除某个数值、查询

本文主要介绍了用C++进行链表的创建,在某个固定的位置上插入节点,删除某个数值的所有节点,在某个位置上删除节点,查询某个位置上节点的数值以及获取链表的长度和显示链表等基本操作,希望对大家有所启发,现放代码如下,本人亲测无误。

#include<iostream>
using namespace std;
class Node {
public:
    int data;
    Node *pnext;
};
class Linklist {
public:
    Linklist() {
        head = new Node;
        head->data = 0;
        head->pnext = NULL;
    }
    ~Linklist() {
        Node *ptemp, *pdel;
        ptemp = head;
        while (ptemp != NULL) {
            pdel = ptemp;
            ptemp = ptemp->pnext;
            delete pdel;
            pdel = NULL;
        }
    }
    void createlist(int n1);
     int getlength();
    void showlist();
    void insertNode(int n2);
    void deleNode(int n3);
    void deleNode1(int value);
    void findNode(int n4);
private:
    Node *head;
};
//创建链表
void Linklist::createlist(int n1) {
    Node *ptemp, *pnew;
    ptemp = head;
    for (int i = 0; i < n1; i++) {
        pnew = new Node;
        pnew->pnext = NULL;
        cout << "请输入第" << i + 1 << "节点的值 = ";
        cin >> pnew->data;
        ptemp->pnext = pnew;
        ptemp = pnew;
    }
}
//显示链表
void Linklist::showlist() {
    Node *ptemp;
    ptemp = head->pnext;
    cout << "该链表的节点 = ";
    while (ptemp != NULL) {
        cout << ptemp->data<<" ";
        ptemp = ptemp->pnext;
    }
    cout << endl;
}
//在某一个位置插入一个节点
void Linklist::insertNode(int n2) {
    Node *ptemp, *pnew;
    ptemp = head;
    pnew = new Node;
    pnew->pnext = NULL;
    cout << "请输入插入节点的数值 = ";
    cin >> pnew->data;
    while (n2-- > 1) {
        ptemp = ptemp->pnext;
    }
    pnew->pnext = ptemp->pnext;
    ptemp->pnext = pnew;
}
//获得链表的长度
int Linklist::getlength() {
    Node *ptemp;
    int n;
    n = 0;
    ptemp = head->pnext;
    while (ptemp != NULL) {             
        n++;
        ptemp = ptemp->pnext;
    }
    //cout << "该链表的节点数 = " << n << endl;
    return n;
}
//删除某个位置上的节点
void Linklist::deleNode(int n3) {
    Node *ptemp, *pdel;
    ptemp = head;
    while (n3-- > 1) {
        ptemp = ptemp->pnext;
    }
    pdel = ptemp->pnext;
    ptemp->pnext = ptemp->pnext->pnext;
    delete pdel;
    pdel = NULL;
}
//删除节点值为某个数的所有的节点
void Linklist::deleNode1(int value) {
    Node *ptemp, *pdel;
    ptemp = head;
    for (int i = 0; i < Linklist::getlength(); i++) {
        if (ptemp->pnext->data == value) {
            pdel = ptemp->pnext;
            ptemp->pnext = ptemp->pnext->pnext;
            delete pdel;
        }
        ptemp=ptemp->pnext;
    }
}
//查询某个位置上的节点的数值
void Linklist::findNode(int n4) {
    Node *ptemp, *ptarget;
    ptemp = head;
    while (n4-- > 1) {
        ptemp = ptemp->pnext;
    }
    cout << ptemp->pnext->data << endl;
}
void main() {
    Linklist list;
    int n1, n2,n3,value,value1;
    cout << "请输入您需要的节点的数量 = ";
    cin >> n1;
    list.createlist(n1);
    list.showlist();
    cout << "请输入插入节点的位置= ";
    cin >> n2;
    list.insertNode(n2);
    list.showlist();
    list.getlength();
    cout << "请输入删除节点的位置= ";
    cin >> n3;
    list.deleNode(n3);
    list.showlist();
    cout << "请输入查询节点的位置 = ";
    cin >> value;
    list.findNode(value);
    list.showlist();
    cout << "请输入需要删除节点的数值 = ";
    cin >> value1;
    list.deleNode1(value1);
    list.showlist();
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值