C ++使用多个变量实现Linkedlist

时间:2016-01-09 11:13:58

标签: c++ linked-list

我已经开始了5周的c ++编程。 我正在研究一个程序并使用链表来在程序中存储数据。所以这就是我所做的,而且程序没有编译。如果社区能够解决这些错误,我会很高兴。

// C ++ Assignment.cpp:定义控制台应用程序的入口点。 //

x:Bind

1 个答案:

答案 0 :(得分:0)

首先,名称空间list中已存在std类型。如果您using namespace std;,则无法在名为list的类型中声明。您永远不应该为自己的任何类型list命名,也不要使用名称空间std。 例如,将您的类型list重命名为mylist。像这样调整你的代码:

... 

struct mylist;
mylist *next;

...

struct mylist
{
    ...
};

bool empty(mylist *head);

...

void firstElement(myList *&head, myList *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
    head = last = new myList;
    head->next = NULL;
    head->nic = nic;
    head->contact = contact;
    head->address = address;
    head->customerId = customerId;
    head->name = name;
    head->todaysVehicle = todaysVehicle;
    head->vnum = vnum;
    head->status = status;
    head->serviceType = serviceType;
}

void insertCustomer(myList *&head, myList *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {
        last->next = new myList;
        last = last->next;

        last->next = NULL;
        last->nic = nic;
        last->contact = contact;
        last->address = address;
        last->customerId = customerId;
        last->name = name;
        last->todaysVehicle = todaysVehicle;
        last->vnum = vnum;
        last->status = status;
        last->serviceType = serviceType;
    }
}

除此之外,我建议使用std::list而不是自己动手。