C++ C++;线性链表信息。I';I’我不知道出了什么事

C++ C++;线性链表信息。I';I’我不知道出了什么事,c++,class,C++,Class,编辑:我想出来了,谢谢那些回答的人的帮助 有人能帮我写一点代码吗?我是C++新手,我的程序不能工作。它只是运行switch语句,而没有实际执行我编写的任何函数。我相信这是很明显的,但我非常感谢你的帮助 我的目标是能够制作一个链接列表,我可以向其中添加项目并显示它们,如果用户愿意,最终将项目移动到列表的末尾。我真的很感谢你读到这篇文章。这是我的密码: class places // class to hold all of the places to be inserted into the l

编辑:我想出来了,谢谢那些回答的人的帮助

有人能帮我写一点代码吗?我是C++新手,我的程序不能工作。它只是运行switch语句,而没有实际执行我编写的任何函数。我相信这是很明显的,但我非常感谢你的帮助

我的目标是能够制作一个链接列表,我可以向其中添加项目并显示它们,如果用户愿意,最终将项目移动到列表的末尾。我真的很感谢你读到这篇文章。这是我的密码:

class places  // class to hold all of the places to be inserted into the list
{

public:
    places();  // constructor
    void destroy(); // deconstructor
    void addToList(int placement); //adds items to list
    void displayAll(); // displays all items
    void displayFirst(); // displays first item and moves the first to the end       of the LLL

private:
    places * head;
    places * current;
    places * last;
    places * temp;

    char name[100];
    places * next;
}; 

places::places()
{
head = NULL;
last = NULL;
current = NULL;
temp = NULL;
int placement = 0;
}

void places::destroy()
{
delete[]head;
delete[]last;
delete[]current;
}

void places::addToList(int placement)
{


if (!head)
{
    current = new places;
    cout << "What is the name of the location you'd like to enter?" << endl;
    cin.get (name, 100, '\n');
    cin.ignore (200, '\n');
    current -> name;
    current -> next = NULL;
    last -> name; // attachs it to the last node
}
else
{
    head = new places;
    cout << "What is the name of the location you'd like to enter?" << endl;
    cin.get (name, 100, '\n');
    cin.ignore(200, '\n');
    head -> name;
    head -> next = NULL;
    last = head;
}
}
void places::displayAll()
{
if(!head)
{
    cout << "There are no items to display!" << endl;
    return;
}

temp = head;
cout << "Your favorite locations are: " << endl;

while(temp)
{
    cout << temp -> name << endl; // prints the current node
    //below sets the node to be printed to the next one
    if(temp -> next)
    {
        temp = temp -> next;
    }

    else
    {
        cout << "That's all there is!" << endl;
    }
}   
}

void places::displayFirst()
{
if(head)
{
    cout << head -> name << endl;
    while(temp!=NULL)
    {
        temp = temp -> next;
    }
    temp = head;
    delete[]head -> name;
}
 }

bool again()
{
char answer;

cout << "Do you want to enter another place? (Y/N)" << endl;
cin >> answer;

if(toupper(answer) == 'N')
{
    return false;
}

return true;
}
int main()
{
int i = 0;
places favPlaces;
int option;
do
{
    cout << "These are your options! PICK ONE!!!" << endl;
    cout << "\t1. Add to beginning of list\n";
        cout << "\t2. Add to end of list\n";
        cout << "\t3. Display the list\n";
        cout << "\t4. Display first item\n";
        cout << "\t5. DESTROY THE LIST!\n";


        cout << "Enter a choice : ";
        cin >> option;

    switch(option)
    {

        case 1:
            favPlaces.addToList();
            break;

        case 2: 
            favPlaces.addToList();
            break;

        case 3: 
            favPlaces.displayAll();
            break;

        case 4:
            favPlaces.displayFirst();
            break;

        case 5:
            favPlaces.destroy();
            break;

    }

i++;
}while(i < 4);


}   
class places//class保存要插入列表的所有位置
{
公众:
places();//构造函数
void destroy();//解构器
void addToList(int-placement);//将项添加到列表中
void displayAll();//显示所有项目
void displayFirst();//显示第一项并将第一项移动到LLL的末尾
私人:
地点*头部;
地点*当前;
地点*最后;
地点*温度;
字符名[100];
地点*下一个;
}; 
地点::地点()
{
head=NULL;
last=NULL;
电流=零;
温度=零;
整数位置=0;
}
void places::destroy()
{
删除[]标题;
最后删除[…];
删除[]当前版本;
}
空位置::addToList(整数位置)
{
如果(!头)
{
当前=新地点;
cout-next=NULL;
last->name;//将其附加到最后一个节点
}
其他的
{
头=新地点;
cout-next=NULL;
最后=头部;
}
}
void places::displayAll()
{
如果(!头)
{

cout我在代码中看到的主要问题是无法区分关于列表的信息(位置
类型)和属于每个节点的信息

下面是我写的一篇文章,是为了回答另一个初学者的问题


我认为这是我能提供的最好的帮助,深入研究当前代码将不会有成效。因此,请看一看。也许可以尝试通过示例进行操作。:-

好的,如果开关没有按预期工作,那么首先要做的是添加一个
默认值:
字段,该字段将打印一些调试信息…如果您使用
new
,您可以使用
delete
删除它,而不是
delete[]
。问题似乎不是switch语句,而是在我的函数中添加到列表中。它不接收任何信息,它只是输出请求信息的语句,然后就没有其他事情发生。你能解释一下区分位置类型和属于每个节点的信息是什么意思吗?我有一种感觉,我是与一般函数相比,在处理单个对象时遗漏了一些东西,但我不完全清楚what@McPluckingtonJr:对于每个节点来说,了解诸如
头部
当前
最后一个
是没有意义的。相反,对于完整的列表对象来说,了解
下一个
是没有意义的>(下面还有其他列表吗?)。因此,请将这些内容分开。我建议从我的教程开始。因为为了更容易地了解什么是有意义的或没有意义的,需要对主题有所了解。