标题:c++链表中的函数问题
只看楼主
fansforever
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-1-1
 问题点数:0 回复次数:0 
c++链表中的函数问题
代码要求删除指定结点,remove函数中形参a存储删除结点值
编译检查时没报错,但有时警告: warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
有时警告变成Skipping... (no relevant changes detected)   不太清楚是什么原因,求解释

#include<iostream>
using namespace std;
struct Node
{
    int content;
    Node *next;
};

bool remove(Node *h,int &a,int pos)
{   
    if(pos<=0)return false;
    else
    {   
        Node *p=h;
        if(pos==1)
        {
            h=h->next;
            delete p;
        }
        else
        {
            int j=1;
            while(j<pos-1)
            {
                if(p->next==NULL)break;
                p=p->next;
                j++;
            }
        }
            if(p->next!=NULL)
            {
                Node *q=p->next;
                p->next=q->next;
                delete q;
                return true;
            }
            else
            return false;
    }
}

int main()
{
    Node *head=NULL,*tail;
    int x;
    cout<<"为链表赋值,输入-1结束"<<endl;
    cin>>x;
    while(x!=-1)
    {
        Node *p=new Node;
        p->content=x;
        p->next=NULL;  
        if(head==NULL)
            head=p;
        else
            tail->next=p;
        tail=p;
        cin>>x;
    }

    int y,position;
    cout<<"输入要删除的数字及其位置"<<endl;
    cin>>y>>position;

    for(Node *p=head;p!=NULL;p=p->next)
    {
        if(p->content==y)break;
    }
    if(p==NULL)cout<<"输入数字不存在!"<<endl;
    else
    {
        Node *q=head;
        bool a=(q,y,position);

        if(a==true)
        {
            cout<<"删除成功,删除后的链表为"<<endl;
            for(Node *p=head;p!=NULL;p=p->next)
            cout<<p->content<<',';
            cout<<endl;
        }
        else
        cout<<"位置不正确或没有第"<<position<<"个结点"<<endl;
    }
    return 0;
}
搜索更多相关主题的帖子: 函数 next relevant content include 
2012-03-19 21:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-363688-1-1.html




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

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