简体   繁体   English

链接的指针列表C ++

[英]Linked list of pointers C++

I have a list, but now I have to link it. 我有一个列表,但是现在我必须链接它。
Here is my program ( I deleted code inside functions, to make my program more easy to read ). 这是我的程序(为了使程序更易于阅读,我删除了函数内部的代码)。

#include <iostream>

using namespace std;

struct Student
{
    char ime[16];
    char priimek[16];
    char vpisna[10];
    char ocenaRV[10];
    char ocenaDN[10];
    char ocenaKV[10];
    char ocenaVI[10];
    Student *next;
};

void clean(Student* pointer,int x) // Delete random data
{

}

void dodajanje(int x,Student* s) // Add information about student
{

}

void brisi(Student* pointer,int x) // Delete information about student
{   

}

int main()
{
    int student,mesto, brisanje, ali = 0;
    cout << "Number of students?." << endl; 
    cin  >> student;

    Student* s = new Student[student];

    clean(s,student);

    cout << endl;
    cout << "Add student to i place in array." << endl;
    cin >> mesto;
    dodajanje( mesto, s );

    for(int i=0;i<(student*2);i++)
    {
        cout << "add student = 1, delete student = 2, cout information = 3"<<endl;
        cin>>ali;
        if (ali == 1)
        {
            cout << endl;
            cout << "Add student to i place in array." << endl;
            cin >> mesto;
            dodajanje( mesto, s );  
        }
        else if (ali == 2)
        {
            cout << "delete student on i place ?" << endl;
            cin >> brisanje;
            brisi(s,brisanje);
        }
        else
        {
            break;
        }
    }
    delete[] s;  

    return 0;
}

Can someone explain me how to link my list, because code in all tutorials I came across was similar to this: 有人可以解释一下如何链接我的列表,因为我遇到的所有教程中的代码都与此类似:

Node* temp = Node();

But in my program my code is: 但是在我的程序中,我的代码是:

Student* s = new Student[student];

And now I'm lost; 现在我迷路了;

Note: I have to create dynamically linked list. 注意:我必须创建动态链接列表。

Linked list is a node based data structure. 链表是基于节点的数据结构。 What you are trying to do is creating a dynamic array of students not a linked list. 您尝试做的是创建一个动态的学生数组,而不是一个链表。

If you really need to create a linked list, in place of following line 如果您确实需要创建链接列表,请代替以下行

Student* s = new Student[student];

you should create a nodes as follows in number of time of students in a for loop and link each other by updating student()-> next= next_student (Psuedo code) 您应在for循环中按以下方式创建一个节点,以增加学生的数量,并通过更新student()-> next= next_student (Psuedo code)

Student* s = new Student;

And at end, you have to call delete s within a for loop to deallocate the memory. 最后,您必须在for循环中调用delete s来释放内存。

Node* temp = Node(); Node * temp = Node();

This creates a single Node instance. 这将创建一个Node实例。 Though it should be this instead: 虽然应该是这样:

Node* temp = new Node;

Student* s = new Student[student]; 学生* s =新学生[学生];

This creates an array of student number of Student instances. 这将创建数组student数量Student的情况。 This defeats the purpose of a linked list , as you won't be able to add/remove Student instances from the array efficiently. 这违反了链接列表的目的,因为您将无法有效地从数组中添加/删除Student实例。 But, just for the sake of argument, lets say you really need an array. 但是,仅出于争论的目的,可以说您确实需要一个数组。 You can "link" the Student instances together like this: 您可以像这样将“ Student实例“链接”在一起:

for (int i = 0; i < (student-1); i++)
    s[i].next = &s[i+1];
s[student-1].next = NULL;

If you actually need a linked list then you need something more like this instead: 如果您实际上需要一个链表,那么您需要的是这样的东西:

Student *studentList = NULL;

Student *lastStudent = NULL;
for (int i = 0; i < student; ++i)
{
    Student* s = new Student;
    s->next = NULL;
    if (lastStudent) lastStudent->next = s;
    if (!studentList) studentList = s;
    lastStudent = s;
}

// use studentList as needed...

Student *s = studentList;
while (s)
{
    Student *next = s->next;
    delete s;
    s = next;
}

After fixing that, consider using the STL std::list class instead, or even std::forward_list in C++11. 解决此问题后,请考虑改用STL std::list类,甚至在C ++ 11中使用std::forward_list

That being said, you also need to rethink your code design. 话虽如此,您还需要重新考虑代码设计。 A linked list grows and shrinks dynamically, so there is no point in asking the user for the number of students up front, or pre-allocating the list with garbage that has to be cleaned before it can be used. 链接列表会动态增长和收缩,因此毫无疑问地要求用户预先提供学生人数,或者为列表预先分配必须清除的垃圾才能使用。 Change your loop to run forever (or at least until the user says to stop). 将循环更改为永久运行(或至少直到用户说要停止运行)。 On each iteration, ask the user what to do. 在每次迭代中,询问用户该怎么做。 If Add , add a new Student to the list at that time. 如果Add ,则此时将一个新的Student添加到列表中。 If Delete , ask the user which student to delete, find that Student , unlink it, and delete it. 如果为Delete ,则询问用户要删除的学生,找到该Student ,取消链接,然后deletedelete If Display , ask the user which student to display, find that Student , and display it. 如果是Display ,则询问用户要显示哪个学生,找到该Student并显示它。 And so on. 等等。

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

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