C ++链接列表数据输入

时间:2015-04-03 19:03:49

标签: c++ linked-list

我正在尝试运行while循环将数据注入我已经创建的链表。当用户输入-1作为值时,程序将停止将数据放入链表。出于某种原因,我所拥有的东西不起作用,我不确定我是否理解它背后的逻辑。

class House
{
    public:
        struct node{
            double width;
            double length;
            node *next;
        };

        int houseSize;

        House();
        double calculateTax(double);
};

House::House(){

    //Temporary variables for location and data injecting.
    int temp = 0;
    houseSize = 0;

    //Nodes for both the first location and current location in the linked list.
    node *root; 
    node *conductor;  

    root = new node;
    root->next = 0;

    //Point to the first location.
    conductor = root;

    if (conductor != 0) {
        while (conductor->next != 0){
            houseSize++;

            if(temp != -1){

                //Ask for user input about the rooms.
                cout << "What is the width of room " << houseSize << ": ";
                cin >> temp;    

                if(temp != -1){
                    //If the list isn't ending, input data.
                    conductor->width=temp;  
                }

                //Ask for user input about the rooms.
                cout << "What is the length of room " << houseSize << ": ";
                cin >> temp;    

                if(temp != -1){
                    //If the list isn't ending, input data.
                    conductor->length=temp; 

                    //Point to the next node.
                    conductor->next = new node;
                    conductor = conductor->next;

                }

            }
        }

        //End the linked list to prevent new data from being added.
        conductor->next = new node;  
        conductor = conductor->next;
        conductor->next = 0;       

    }

}

2 个答案:

答案 0 :(得分:0)

node *root; 
node *conductor;  

root = new node;
root->next = 0;

//Point to the first location.
conductor = root;

if (conductor != 0) {
    while (conductor->next != 0){
        ...
    }//End while

    //End the linked list to prevent new data from being added.
    conductor->next = new node;  
    conductor = conductor->next;
    conductor->next = 0;       

}//End if

看起来导体是root,root-&gt; next是零,所以它将完全跳过你的while循环,然后在底部,在新节点旁边分配导体并将其切换为根。

所以,我猜你的while循环应该是:

while (conductor->next == 0) {

进入内部位后,在有效长度输入上,不要将导体 - >设置为零。它现在是一个小的循环缓冲区。更新如下:

                //Point to the next node.
                conductor->next = new node;
                conductor = conductor->next;
                conductor->next = 0;   // ADD THIS!

编辑: 基于所有评论,我认为这是您正在寻找的最终解决方案:

// Helper method to create a node, set internal pointers and data members to zero.  Should really be node CTOR...
node* allocateNewNode()
{
     node * tmp = new node;
     if (tmp == 0) return 0;
     tmp->width = 0;
     tmp->length = 0;
     tmp->next = 0;
     return tmp;
}

House::House() :
    houseSize(1) 
{

//Temporary variables for location and data injecting.
int temp = 0;

//Nodes for both the first location and current location in the linked list.
node *root = allocateNewNode(); 
node *conductor = root;  

bool done = false;

if (conductor != 0) 
{
    // Conductor points to a new unused node at top of loop.
    while (!done) 
    {

       //Ask for user input about the rooms.
       cout << "What is the width of room " << houseSize+1 << ": ";
       cin >> temp;    

       if(temp != -1) 
       {
            //If the list isn't ending, input data.
            conductor->width = temp;  

            //Ask for user input about the rooms.
            cout << "What is the length of room " << houseSize+1 << ": ";
            cin >> temp;    

            if(temp != -1) 
            {
                //If the list isn't ending, input data.
                conductor->length = temp; 

                //Point to the next node and make things identical to top of loop.
                conductor->next = allocateNewNode();
                conductor = conductor->next;
                ++houseSize;
            }
            else
            {
                done = true;
            }
       }
       else
       {
          done = true;
       }
   }
   // Fell off bottom of list, so done is true and conductor points to an invalid node
   // width MIGHT be set from above, so zero it back out.
   conductor->width = 0;

   // conductor already points to an empty unused node, so there is nothing to do to terminate the linked list
}

}

答案 1 :(得分:0)

你的逻辑由以下几行代表:

if (conductor != 0) {
    while (conductor->next != 0){

但是,在此之上,您将conductor设置为等于root,并将root设置为新分配的节点,因此它保证为非null,所以if条件是一个常数。您还将root->next设置为0conductor->next是相同的,因此while条件是常量false。换句话说,您写的所有代码都保证永远不会被执行。

我不知道异常的来源。您不会抛出任何异常,也不能“运行对象”。但请注意,root是构造函数中的局部变量,因此您永远不会实际保存房屋布局。此外,您泄漏了所分配的所有内存,以及在实际执行I / O代码时分配的内存。

最后,你确定你的教授想要“在构造函数中完成所有输入”,而不是“构造函数应该把房间列表作为输入”吗?