我正在尝试为移动商店数据库创建链接列表数据结构。我在声明struct mobile的全局指针时遇到了问题。在方法view_all()中,我无法获得输入变量的输出。
我在unix操作系统中使用putty帮助将此代码作为mobile.cpp执行。
#include <iostream>
#include <iomanip>
using namespace std;
struct mobile
{
char make[15];
char model[10];
float price;
mobile *link;
}*start;
void add_new ()
{
struct mobile *temp, *traverse;
temp = new struct mobile;
traverse = start;
if (start==NULL)
{
temp=start;
}
else
{
while (traverse->link!=NULL)
{
traverse=traverse->link;
}
traverse->link=temp;
}
temp->make="BlackBerry";
temp->model="BB9320";
temp->price=12000;
}
void view_all ()
{
struct mobile *temp;
temp = new struct mobile;
temp=start;
cout <<"Mobile make:"<<temp->make;
cout <<"Mobile price:"<<temp->price;
}
int main ()
{
add_new ();
view_all ();
return 0;
}