简体   繁体   English

我想用 c++ 创建一个链表数组

[英]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:除此之外:

  • Also at 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循环将在此列表的末尾添加进程。
  • Removing that line also makes redundant the 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:为了简化事情:

  • I would use, at least, a std::vector<processes*> tab{};我至少会使用std::vector<processes*> tab{}; . .
  • Ideally, I would also use 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.这样,就不需要处理 memory 分配/解除分配。
  • 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:最后,作为编码最佳实践:

  • Always initialize variables.始终初始化变量。
  • Try to declare variables next to their first use.尝试在第一次使用时声明变量。
  • Try not to declare more than one variable per line (eg int i, j = 1; ).尽量不要每行声明一个以上的变量(例如int i, j = 1; )。
  • Try to use descriptive variable names.尝试使用描述性变量名称。
  • Prefer using nullptr to NULL or 0 for modern C++ code (since C++11).更喜欢将nullptr用于NULL0用于现代 C++ 代码(C++11 起)。
  • using namespace std; should better be avoided, as it pollutes the program's namespace (see here ).最好避免,因为它会污染程序的名称空间(请参阅此处)。

[Demo] [演示]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM