C++ C++;程序已停止工作链接列表

C++ C++;程序已停止工作链接列表,c++,data-structures,codeblocks,C++,Data Structures,Codeblocks,下面的代码在3 4或6次输入时运行良好,然后出现“程序已停止工作”我检查了所有内容,但没有得到使用代码块的线索 struct info{ string name; string phone; }; struct node{ info contact; node *link; }; void SaveToFile(node *start); void AddNode(node *&start, info contact); void display(node *start); int

下面的代码在3 4或6次输入时运行良好,然后出现“程序已停止工作”我检查了所有内容,但没有得到使用代码块的线索

struct info{
string name;
string phone;
};

struct node{
info contact;
node *link;
};

void SaveToFile(node *start);
void AddNode(node *&start, info contact);
void display(node *start);

int main()
{

bool check = true;
int choice;
info Newcontact;
node *start = NULL;

    while(check){
        cout<<" ----------------------------- \n";
        cout<<"|       PHONE DIRECTORY       |\n";
        cout<<" ----------------------------- \n";
        cout<<"|1: Insert a Contact          |\n";
        cout<<"|4: DISPlAY Contacts          |\n";
        cout<<"|6: EXIT                      |\n";
        cout<<" ----------------------------- \n";

        cout<<"Enter your choice: ";
        cin>>choice;

        switch(choice){
        case 1:
            cout<<"Enter Name: ";
            cin>>Newcontact.name;
            cout<<"Enter Contact No: ";
            cin>>Newcontact.phone;
            AddNode(start,Newcontact);
            break;

        case 4:
            display(start);
            break;

        case 6:
            check = false;
            break;
        }
    }

    return 0;
}
结构信息{ 字符串名; 字符串电话; }; 结构节点{ 信息联系; 节点*链接; }; 作废保存文件(节点*开始); void AddNode(节点*&开始,信息联系人); 无效显示(节点*开始); int main() { 布尔检查=真; 智力选择; 信息新联系人; node*start=NULL; while(检查){
好的,简短的回答:如果你在写C++代码,不要使用malloc、
calloc
和其他*alloc。你有漂亮的
new
delete
。使用*alloc你不会得到
std::string
足够的内存,因为
std::string
也是类,你必须为每个字符串分配内存嗯

void AddNode(node *&start, info User_contact){
node *temp, *p;
p=start;
temp = (node *)malloc(sizeof(node));
temp->contact = User_contact;

if(start==NULL){
    start = temp;
    temp ->link=NULL;
}
else{
    while(p->link!=NULL){
        p=p->link;
    }

      p->link=temp;
      temp->link=NULL;
}
}

void display(node *start){
node *p = start;
while(p!=NULL){
    cout<<"NAME: "<<p->contact.name<<"  "<<"PNONE: "<<p->contact.phone<<"\n";
    p=p->link;
}
}