C++ 链表 1-- 结构体链表

#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
    15-02结构体链表
---------------------------------*/
struct book
{
int     num;
float price;
struct book *next;
};
int main()
{
book x,y,z,*p,*head;
x.num =1000;
x.price =14.5f;
y.num =2000;
y.price =23.4F; //小数在vc编译器里默认是double
z.num =3000;
z.price =34.3f; //双精度浮点数类型,不能隐式转换为float型,后缀f或F即可提示为float型
head =&x;
x.next =&y;
y.next =&z;
z.next =NULL;


p=head;
while(p!=NULL) //访问到最后一个指针为空,即遍历完所有元素
{
cout<<p->num<<"\t"<<p->price<<endl;
p =p->next;
}
return 0;

}

运行结果:

1000    14.5
2000    23.4
3000    34.3
Press any key to continue

猜你喜欢

转载自blog.csdn.net/paulliam/article/details/80046243