在C++链接列表中帮助.show记录功能启动无限循环

In C++ Link list help ..show Record function started infinite loop

本文关键字:记录 功能 启动 无限循环 show 帮助 C++ 链接 列表      更新时间:2023-10-16

我编写了一个程序,在其中实现了linklist(structure),有两个函数setRecord用于获取用户的ID,第二个函数是显示ShowRecord()但是当我输入 ID 然后想显示记录时..它只显示第一个和最后一个节点或...现在进行了一些更改,它以最后一个ID开始无限循环...代码如下...

// linklist practice.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
struct info
{
    int data;
    info *next;
};
void setRecord(info *temp);
void showRecord(info *temp);
int main()
{
    char a;
    info *base = new info;
    info *temp;
    info *last;
    temp = base;
    last = base;
    setRecord(temp);
    //do
//  {
        do
        {
            info *std = new info;
            std = last;
            temp->next = std;
            //std->next = NULL;
            setRecord(temp);
            temp->next = temp;
            cout << "Do you wanna Enter another....?(y or n):n";
            cin >> a;
        } while (a =='y');
    //  break;
    //} while (1);
        last = NULL;
        showRecord(temp);
    system("pause");
    system("pause");
    return 0;
}
void setRecord(info *temp)
{
    cout << "Enter your ID:->";
    cin >> temp->data;
}
void showRecord(info *temp)
{
    while (temp != NULL)
    {

            cout << "ID number "<< temp->data;
        cout << endl;
        temp = temp->next;
    }
}

更改main()方法。

int main()
    {
        char a;
        info *base = new info;
        info *temp;
        info *first;
        temp = base;
        first=temp;
        setRecord(temp);
        //do
    //  {
            do
            {
                info *std = new info;
                temp->next = std;
                //std->next = NULL;
                temp=std;
                setRecord(temp);
                temp->next = null;
                cout << "Do you wanna Enter another....?(y or n):n";
                cin >> a;
            } while (a =='y');
        //  break;
        //} while (1);
            showRecord(first);
        system("pause");
        system("pause");
        return 0;
    }