设为首页 加入收藏

TOP

C++建立动态链表
2014-11-24 07:29:21 来源: 作者: 【 】 浏览:1
Tags:建立 动态

#include
#include
using namespace std;
class book
{
public:
int num;
double price;
book* next;
};
bool check(string str)
{
for(int i=0;i{
if((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
return false;
}
return true;
}


book* create()
{
book *p1,*p2=NULL,*head=NULL;
p1=new book;
head=p1;
p2=p1;
cout<<"please enter number of book,then 0 to end:\n";
string str_num,str_price;
cin>>str_num;
while(!check(str_num))
{cout<<"the enter of your is error,please enter true number:\n";cin>>str_num;}
p1->num=atoi(str_num.c_str());
if(p1->num==0)
{
delete p1;p2->next=NULL;p2=NULL;head=NULL;cout<<"p1->num=0\n";return head;
}
while(p1->num!=0)
{
cout<<"please enter price of the book:\n";
cin>>str_price;
while(!check(str_price))
{cout<<"the enter of your is error;please enter true number:\n";cin>>str_price;}
p1->price=atof(str_price.c_str());
p2=p1;
p1=new book;
p2->next=p1;
cout<<"please enter number of book,then 0 to end:\n";
cin>>str_num;
while(!check(str_num))
{cout<<"the enter of your is error;please enter true number:\n";cin>>str_num;}
p1->num=atoi(str_num.c_str());
cout<num;
}
delete p1;
p2->next=NULL;
return head;
}


void print(book* head)
{
book* p;
p=head;
cout<<"num"<<"\t"<<"price"<<"\n";
while(p)
{
cout<num<<"\t"<price<<"\n";
p=p->next;
}
delete p;
}



int main()
{
book* head=NULL;
head=create();
print(head);
delete head;
return 0;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 软件的状态栏图标通知处理 下一篇Linux Epoll的使用方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)