| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 602 人关注过本帖
标题:链表报错missing ';' before 'while',求解
只看楼主 加入收藏
naixin
Rank: 2
等 级:论坛游民
帖 子:16
专家分:26
注 册:2013-10-5
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:1 
链表报错missing ';' before 'while',求解
#include <iostream>
#include <string>
using namespace std;
class book
{
public:
    int num;
    float price;
    book *next;    //指向下一个节点
};
book *head=NULL;
bool check(string str)
{
    for(int i=0;i<str.length();i++)
    {
        if ((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
            return false;
    }
    return true;
}
book *creat()//用来建立动态链表
{
    book *p1,*p2;//p2作为下一个节点指针,p1作为本节点指针
    p1=new book;
    head=p1;
    p2=p1;
    cout<<"请输入图书的编号,以0结束"<<endl;
    string str;
    cin>>str;
    while(!check(str))
    {
        cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
        cin>>str;
    }
    p1->num=atoi(str.c_str());
    if (p1->num!=0)
    {
        cout<<"请输入图书的价格"<<endl;
        cin>>str;
    while(!check(str))
    {
        cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
        cin>>str;
    }
    p1->price=atof(str.c_str());
    }
    else
    {
        delete p1;
        p2=NULL;p2->next=NULL;
        head=NULL;
        return head;
    }
    while (p1->num!=0)
    {
        p2=p1;
        p1=new book;
        cout<<"请输入图书的编号,以0结束"<<endl;
        cin>>str;
    while(!check(str))
    {
        cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
        cin>>str;
    }
        p1->num=atoi(str.c_str());
        if (p1->num!=0)
        {
            cout<<"请输入图书的价格"<<endl;
            cin>>str;
    while(!check(str))
    {
        cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
        cin>>str;
    }
        p1->price=atof(str.c_str());
        }
        p2->next=p1;
    }
    delete p1;
    p2->next=NULL;
    return head;
}
void showbook(book* head)
{
    cout<<endl;
    cout<<"图书信息如下:"<<endl;
    while (head)
    {
        cout<<"图书编号:"<<head->num<<"\t";
        cout<<"价格:"<<head->price<<endl;
        head=head->next;
    }
}
void Delete(book*head,int num)
{
    book*l;
    if (head->num==num)
    {
        l=head;
        head=head->next;
        ::head=head;
        delete l;
        cout<<"操作成功"<<endl;
        return;
    }
    while(head)
    {
        if (head->next==NULL)
        {
            cout<<"找不到要删除的编号。"<<endl;
            return;
        }
        if (head->next->num==num)
        {
            l=head->next;
            head->next=l->next;
            delete l;
            cout<<"操作成功"<<endl;
            return;
        }
        head=head->next;
    }
    cout<<"找不到要删除的编号"<<endl;
}
void insert(book*head,int num,float price)
{
    book*list=new book;
    list->num=num;
    list->price=price;
    list->next=head;
    ::head=list;
}
book*temp=NULL;
////////////////////////////////////////////////////////////////////////////////////////////////
while((num>head->num)&&(head->next!=NULL))//此处报错missing ';' before 'while',该怎么改呀/
    temp=head;                                                                        /
    head=head->next;/
}                                                                                    
if (num>head->num)
{
    head->next=list;
}
else
{
    temp->next=list;
    list->next=head;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
int getBookNum(book*head)
{
    int num=0;
    while(head)
    {
        num++;
        head=head->next;
    }
    return num;
}
int main()
{
    //book *head=NULL;
    head=creat();
    showbook(head);
    cout<<"请输入您要删除的图书编号:";
    int BookNum;
    cin>>BookNum;
    Delete(head,BookNum);
    showbook(head);
    cout<<"请输入您要插入图书的编号:";
    cin>>BookNum;
    cout<<"请输入您要插入图书的价格:";
    float BookPrice;
    cin>>BookPrice;
    insert(head,BookNum,BookPrice);
    showbook(head);
    cout<<"图书数目是:"<<getBookNum(head)<<endl;
    return 0;
}
去掉方块中的代码,其余正确运行,方框中的代码是插入节点
搜索更多相关主题的帖子: include before public return false 
2014-04-06 11:55
xiayizhanzyf
Rank: 2
等 级:论坛游民
帖 子:80
专家分:78
注 册:2010-7-21
收藏
得分:7 
head=head->next;/这里不是多了个“/”啊
2014-04-07 13:57
快速回复:链表报错missing ';' before 'while',求解
数据加载中...
 
   



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

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