C ++线性链表信息。我很遗憾出了什么问题

时间:2013-12-11 03:38:00

标签: c++ class

编辑:我想通了,感谢那些回答者的帮助。

有人可以用我的一些代码帮助我吗?我是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);


}   

1 个答案:

答案 0 :(得分:1)

我在代码中看到的主要内容是无法区分关于列表(places类型)的信息和属于每个节点的信息。

这是我在回答另一个这样的初学者问题时所写的short tutorial on linked lists

我认为这是我能给予的最好的帮助,深入研究当前的代码并不会有成效。所以,看一看吧。也许试着通过这些例子。 : - )