Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “我的链表”节点中没有数据_C++_Linked List - Fatal编程技术网

C++ “我的链表”节点中没有数据

C++ “我的链表”节点中没有数据,c++,linked-list,C++,Linked List,我正在将文本文件中的每条记录添加到链表中的每个节点 这意味着第一行记录在第一个节点中,第n条记录在第n个节点中 我使用PrintNth(n)函数检查第n个节点中是否有任何值。 当我运行程序时,它显示0 结果: 1001 3 2.5 15 1.2 1001 2 26.7 5.3 1001 5 40 3.5 2.9 71.2 5.3 1001 1 3.86 1001 4 232.3 39.29 9.3 94.32 0 #include <iostream> #include <f

我正在将文本文件中的每条记录添加到链表中的每个节点

这意味着第一行记录在第一个节点中,第n条记录在第n个节点中

我使用
PrintNth(n)
函数检查第n个节点中是否有任何值。 当我运行程序时,它显示0

结果:

1001
3 2.5 15 1.2
1001
2 26.7 5.3
1001
5 40 3.5 2.9 71.2 5.3
1001
1 3.86
1001
4 232.3 39.29 9.3 94.32
0
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Customer{

    int custId;
    double *arItemPrices;

};

struct Node { 

    Customer cust; 
    struct Node *next; 

}*front = NULL, *rear, *temp;

/**Function Declaration***/
void insert(Node**,ifstream &in, int);
//void delete();
int PrintNth(int);  
/**Function Declaration***/

int main(){

    ifstream in;
    in.open("Price Records.txt");
    Customer c;
    c.custId = 1001;
    insert(&front, in,c.custId); //ifstream object and customer id
    cout <<endl;
    cout << PrintNth(1);

    return 0;
}

/***Function Definition***/
void insert(struct Node** temp, ifstream&in, int a){

    struct Node *newNode = new Node();

    newNode->cust.custId = a;

    int item; 
    double prices;

    while(in >> item){

    cout << endl;
    cout << newNode->cust.custId << endl;
    cout << item << " ";
    double items[item];
    newNode->cust.arItemPrices = items;

    for (int i = 0; i < item; i++)
    {
        in >> newNode->cust.arItemPrices[i];
        cout << newNode->cust.arItemPrices[i] << " "; 
    }

}
}

int PrintNth(int index)
{
    struct Node* current = front;
    int count = 0; 

    //Start travesing from head.
    while (current != NULL)
    {
       if(count == index)
       {
          return (current->cust.custId);
       }
       count++;
       current = current->next;
    }
    return 0;              
} 
代码:

1001
3 2.5 15 1.2
1001
2 26.7 5.3
1001
5 40 3.5 2.9 71.2 5.3
1001
1 3.86
1001
4 232.3 39.29 9.3 94.32
0
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Customer{

    int custId;
    double *arItemPrices;

};

struct Node { 

    Customer cust; 
    struct Node *next; 

}*front = NULL, *rear, *temp;

/**Function Declaration***/
void insert(Node**,ifstream &in, int);
//void delete();
int PrintNth(int);  
/**Function Declaration***/

int main(){

    ifstream in;
    in.open("Price Records.txt");
    Customer c;
    c.custId = 1001;
    insert(&front, in,c.custId); //ifstream object and customer id
    cout <<endl;
    cout << PrintNth(1);

    return 0;
}

/***Function Definition***/
void insert(struct Node** temp, ifstream&in, int a){

    struct Node *newNode = new Node();

    newNode->cust.custId = a;

    int item; 
    double prices;

    while(in >> item){

    cout << endl;
    cout << newNode->cust.custId << endl;
    cout << item << " ";
    double items[item];
    newNode->cust.arItemPrices = items;

    for (int i = 0; i < item; i++)
    {
        in >> newNode->cust.arItemPrices[i];
        cout << newNode->cust.arItemPrices[i] << " "; 
    }

}
}

int PrintNth(int index)
{
    struct Node* current = front;
    int count = 0; 

    //Start travesing from head.
    while (current != NULL)
    {
       if(count == index)
       {
          return (current->cust.custId);
       }
       count++;
       current = current->next;
    }
    return 0;              
} 
#包括
#包括
#包括
使用名称空间std;
结构客户{
int custId;
双*算术;
};
结构节点{
客户客户;
结构节点*下一步;
}*前=零,*后,*温度;
/**函数声明***/
无效插入(节点**,ifstream&in,int);
//作废删除();
int PrintNth(int);
/**函数声明***/
int main(){
如果输入;
in.open(“Price Records.txt”);
客户c;
c、 custId=1001;
插入(&front,in,c.custId);//ifstream对象和客户id
cout(项目){
计算算术价格[i];
客户价格(cust.custId);
}
计数++;
当前=当前->下一步;
}
返回0;
} 
上面的代码有什么问题

这些线

double items[item];
newNode->cust.arItemPrices = items;
您正在获取本地数组的地址。退出
Insert
函数时,数组将被销毁,但仍有指向它的指针。这是行不通的。您需要动态分配阵列


另外,您的
Insert
函数分配一个节点,但不尝试将其链接到列表中。在功能中任何时候都不能修改
temp
front
reast
。所以这也不行。

现在是学习和开始使用调试器的最佳时机。在调试器中检查你的代码并观察程序是如何运行的。首先,它不是一个程序,因为你不能仅仅用你提供的信息来复制它。首先删除对输入文件的依赖关系。此外,它的格式不一致。请,作为一个新的用户,拿着这本书读一读,因为你甚至没有说出你所期望的@UlrichEckhardt,我将阅读常见问题解答。别担心。如果您阅读了我问题的第一句话,我确实提到要将文本文件中的每条记录添加到链表中的每个节点。@S.M.@Visual Studio调试器的文档有用吗?它是否提供了对每个步骤的全面解释?如何在结构中动态分配数组?它是通过像double*items[item]那样指定星号来实现的吗?我再次检查代码..是的,我忘了链接节点。我现在这样做了,而且奏效了。谢谢,这样分配一个数组
newNode->cust.arItemPrice=newdouble[item]