[英]I want to make an array of linked lists with c++
I am trying to make a simple array of linked lists,every case in this array containing the head of a linked list,the code contains lot of functions,creating the lists,reading data in the lists,printing each node,and deleting the node.我正在尝试制作一个简单的链表数组,这个数组中的每个案例都包含链表的头部,代码包含很多功能,创建列表,读取列表中的数据,打印每个节点,并删除节点.
#include <iostream>
using namespace std;
struct processus {
int id;
int prio;
int etat;
processus* suiv;
};
void inserD(processus*& D, int x, int y, int z)
{
processus* nouv = new processus;
nouv->id = x;
nouv->prio = y;
nouv->etat = z;
nouv->suiv = D;
D = nouv;
}
void afficher(processus** tab, int M)
{
int i, j = 1;
for (i = 0; i < M; i++) {
processus* c = tab[i];
while (c != NULL) {
cout << j << "-->\t";
cout << c->id << " ";
cout << c->prio << " ";
cout << c->etat << endl;
c = c->suiv;
j++;
}
cout << "\n";
}
}
void supprimer(processus*& D)
{
processus* supp;
while (D != 0) {
supp = D;
D = D->suiv;
delete supp;
}
}
void insertF(processus* tab, int x, int y, int z)
{
processus* last = new processus;
last->id = x;
last->prio = y;
last->etat = z;
if (tab == 0) {
tab = last;
last->suiv = 0;
}
else {
processus* c = tab;
while (c->suiv != 0) {
c = c->suiv;
}
c->suiv = last;
last->suiv = 0;
}
}
void Creation(processus** tab, int l, int m)
{
int x, y, z, i, j;
for (i = 0; i < l; i++) {
tab[i] = new processus[m];
for (j = 0; j < m; j++) {
cout << "donner id:";
cin >> x;
cout << "donner prio:";
cin >> y;
cout << "donner etat:";
cin >> z;
insertF(tab[i], x, y, z);
}
}
}
int main()
{
int l, m;
cout << "entrer le nombre des listes:";
cin >> l;
cout << "entrer le nombre de procesuss dans chaque liste";
cin >> m;
processus** tab = new processus*[l];
for (int i = 0; i < l; i++) {
*(tab + i) = NULL;
}
Creation(tab, l, m);
afficher(tab, l);
for (int i = 0; i < l; i++) {
delete[] tab[i];
}
delete[] tab;
}
the problem is that when i run the code it always add a node at the first of each case in this array and output a random data in it,and i cant find the problem in the code.问题是,当我运行代码时,它总是在此数组中每个案例的第一个添加一个节点,并在其中添加一个随机数据 output,我无法在代码中找到问题。
There are a couple of issues with your code that could be causing the behavior you described.您的代码有几个问题可能会导致您描述的行为。
First, in the inserD
function, you are using the reference of the head pointer of the linked list to insert a new node at the beginning of the list.首先,在
inserD
function 中,您使用链表头指针的引用在链表的开头插入一个新节点。 However, in the insertF
function, you are passing the head pointer as a regular parameter, which means that any changes you make to the pointer within the function will not be reflected outside the function. To fix this, you should pass the head pointer as a reference in the insertF function as well.但是,在
insertF
function 中,您将头指针作为常规参数传递,这意味着您对 function 中的指针所做的任何更改都不会反映在 function 之外。要解决此问题,您应该将头指针传递为insertF function 中的引用也是如此。
Second, in the Creation
function, you are creating an array of processus
structures, but you are not assigning the head pointers of the linked lists to the corresponding element in the array.其次,在
Creation
function 中,您正在创建一个processus
结构数组,但您没有将链表的头指针分配给数组中的相应元素。 Instead, you are creating a new linked list and inserting nodes into it, but this linked list is not being saved in the array.相反,您正在创建一个新的链表并将节点插入其中,但该链表并未保存在数组中。 To fix this, you should assign the head pointers of the linked lists to the corresponding elements in the array.
要解决此问题,您应该将链表的头指针分配给数组中的相应元素。
Here is how you could modify the Creation
function to fix these issues:您可以通过以下方式修改
Creation
function 来解决这些问题:
void Creation(processus** tab, int l, int m)
{
int x, y, z, i, j;
for (i = 0; i < l; i++) {
for (j = 0; j < m; j++) {
cout << "donner id:";
cin >> x;
cout << "donner prio:";
cin >> y;
cout << "donner etat:";
cin >> z;
inserD(tab[i], x, y, z); // Use inserD to insert at the beginning of the list
}
}
}
tab[i] = new processus[m]
creates a new processus
instance but all of its members are uninitilised, in insertF
you then iterate through the linked nodes of tab[i]
but as suiv
is uninitialised this has undefined behaviour. tab[i] = new processus[m]
创建一个新的processus
实例,但它的所有成员都未初始化,然后在insertF
中迭代tab[i]
的链接节点,但由于suiv
未初始化,这具有未定义的行为。
The simplest fix is to remove the line tab[i] = new processus[m]
and then change insertF
to take tab
by reference so it can correctly initialise it with a new node.最简单的修复是删除行
tab[i] = new processus[m]
,然后将insertF
更改为通过引用获取tab
,以便它可以使用新节点正确初始化它。
You should also help avoid these problems by creating a constructor for processus
which initilises all the members or more simply just give each member an inline initialiser:您还应该通过为
processus
创建一个构造函数来帮助避免这些问题,该构造函数初始化所有成员,或者更简单地为每个成员提供一个内联初始化程序:
struct processus {
int id = 0;
int prio = 0;
int etat = 0;
processus* suiv = 0;
};
The only problem I've seen is that modifications to tab
within insertF
are only local.我看到的唯一问题是
insertF
中对tab
的修改只是本地的。 That can be fixed passing tab
by reference as done in the other methods, ie processus*& tab
.这可以像在其他方法(即
processus*& tab
)中所做的那样通过引用来修复传递tab
卡。
Apart from that:除此之外:
Creation
, tab[i] = new processus[m]
is not needed.Creation
中,不需要tab[i] = new processus[m]
。 tab[i]
is the head pointer to a list of processes. tab[i]
是指向进程列表的头指针。 The inner for
loop will be adding processes at the end of this list.for
循环将在此列表的末尾添加进程。delete[] tab[i];
delete[] tab[i];
at the end of main
.main
的末尾。 What should actually be called there is supprimer(tab[i]);
supprimer(tab[i]);
. In order to simplify things:为了简化事情:
std::vector<processes*> tab{};
std::vector<processes*> tab{};
. std::vector<std::unique_ptr<process>>
.std::vector<std::unique_ptr<process>>
。 This way, there would be no need to deal with memory allocation/deallocation.processus
instances could be created by using aggregate initialization , eg processus{x, y, z, D}
or processus{x, y, z}
. processus
实例可以通过使用聚合初始化创建,例如processus{x, y, z, D}
或processus{x, y, z}
。 Finally, as coding best practices:最后,作为编码最佳实践:
int i, j = 1;
).int i, j = 1;
)。nullptr
to NULL
or 0
for modern C++ code (since C++11).nullptr
用于NULL
或0
用于现代 C++ 代码(C++11 起)。using namespace std;
should better be avoided, as it pollutes the program's namespace (see here ).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.