| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2036 人关注过本帖
标题:单链表删除结点问题
只看楼主 加入收藏
jioper
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2016-10-21
结帖率:70%
收藏
 问题点数:0 回复次数:2 
单链表删除结点问题
源代码如下:
void Mlist::del(int a)//删除某个结点
{
    find(a);//先找到要删除的结点
    if(current_p==head)//如果要删头结点情况
    {
        head=head->next;
    }
    else//其余结点情况
    {
        previous_p->next=current_p->next;
        delete current_p;
        previous_p=current_p=NULL;
    }
}
但是这样写加入删除不存在的结点,也会把头结点删除于是我加上了一行if(previous_p->next=NULL) return;,但是加上这行后一运行就会不断生成新的结点...
完整代码如下:
#include <iostream>
using namespace std;
class Mlist
{
private:
    struct Node//定义链表结构
    {
        int value;
        Node* next;
    };

    Node* head;//定义头指针
    Node* tail;//定义尾指针
    Node* current_p;//当前值指针
    Node* previous_p;//下一个值指针
    int N;//表长
public:
    Mlist();
    void creat_app(int va);//创建链表
    void showlist();//打印链表
    void find(int);//查找值
    void del(int);//删除一个值
    void intsert_after(int a,int b);//向前插入一个值
    void intsert_before(int a,int b);//向后插入一个值
    void clear();//清空表
    void change(int a,int b);//
};
Mlist::Mlist()//构造函数初始化
{
    head = NULL;//先令头指向空
    N = 0;//长度初始化为0
    tail = NULL;//尾一样指向空
}

void Mlist::creat_app(int va)//创建链表
{
    Node* newp = new Node;//给头结点分配空间
    if(N==0)//长度为0情况
    {
        head = tail = newp;
    }
    tail->next = newp;
    newp->value = va;
    newp->next = NULL;
    tail=newp;
    N++;
}

void Mlist::showlist()//打印链表
{
    Node *p;
    p=head;//另指针指向头结点
    int i=0;
    if(head==NULL) return;//空链表情况
    while(p!=NULL)//循环条件,指针p如果不指向空(即表尾)
    {
        cout<<"\t第"<<++i<<"个节点\t";
        cout<<"首地址\t"<<p<<"\t值\t"<<p->value<<"\t指针域\t"<<p->next<<endl;
        p=p->next;
    }
    cout<<endl;
}

void Mlist::find(int a)//查找某个值
{
    Node* p;
    bool key_ye = false;
    if(head==NULL) return;//空链表情况
    for(p=current_p=previous_p=head;p!=NULL;p=p->next)
    {
        if(p->value == a)//判断,如果找到
        {
            cout<<"\n\t查到了"<<endl;key_ye=true;
            current_p=p;
            cout<<"\n\t首地址是\t"<<current_p<<endl;break;
        }
        previous_p=p;
    }
    if(!key_ye){cout<<"\n\t没有查到! \a"<<endl;}

}

void Mlist::del(int a)//删除某个结点
{
    find(a);//先找到要删除的结点
    if(current_p==head)//如果要删头结点情况
    {
        head=head->next;
    }
    if(previous_p->next=NULL) return;
    else//其余结点情况
    {
        previous_p->next=current_p->next;
        delete current_p;
        previous_p=current_p=NULL;
    }
}

void Mlist::intsert_after(int a,int b)//向后插入
{
    find(a);
    Node* temp=new Node;
    temp->value=b;
    temp->next=current_p->next;
    current_p->next=temp;
}
void Mlist::intsert_before(int a,int b)//向前插入
{
    find(a);
    Node* temp=new Node;//创建一个新结点
    temp->value=b;//赋值
    temp->next=current_p;//把其位置指向插入位置
    previous_p->next=temp;//调换值
}
void Mlist::clear()//删除链表
{
    Node *p=head->next,*q;//定义指针p和q
    while(p)
    {
        q=p->next;//q指向下一个结点
        free(p);//释放p
        p=q;//另p=q
    }//直到p不存在
    cout<<"链表删除成功!"<<endl;
}
int main()
{
    Mlist ml;
    int a;
    for(int i=0;i<5;i++)
    {
        ml.creat_app(i);
    }
    ml.showlist();
    cout<<"\t请输入要查找的数......";
    cin>>a;
    ml.find(a);
    cout<<"\t请输入要删除的数......";
    cin>>a;
    ml.del(a);
    ml.showlist();
    ml.intsert_after(3,5);
    ml.showlist();
    ml.intsert_before(3,6);
    ml.showlist();
    cout<<"是否清空链表?是按1不清空按2"<<endl;
    int n;
    cin>>n;
    if(n==1)
    {
    ml.clear();
    if(n==2)
    return 0;
    }
    return 0;
}
搜索更多相关主题的帖子: 源代码 中国 华夏 经理 
2017-06-14 11:29
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
del块没看到有new语句,怎么不断生成节点的?
2017-06-19 18:06
某一天
Rank: 2
等 级:论坛游民
威 望:1
帖 子:33
专家分:77
注 册:2015-6-15
收藏
得分:0 
槽点太多,感觉到处都是破绽!
previous_p这个是什么东西?不是楼主备注的下一个吧?

if(previous_p->next=NULL) return;这句:
1.我是完全没看懂previous_p->next为空时为什么就能判断没找到指定值.直接判断current_p不行吗?
2.至少应该使用比较运算符==而不是使用赋值运算符=吧!

至于楼主说的会不断生成新节点,我也不知道是个什么情况.生成的节点值是多少?
2017-06-26 16:22
快速回复:单链表删除结点问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018409 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved