简体   繁体   English

如何创建用作队列的节点链表

[英]How to create a linked list of nodes to be used as a queue

Please any advice on How can I create a linked list of nodes to be used as a queue?请就如何创建用作队列的节点链接列表提供任何建议? I finished the node but not sure how to implement it into a queue linked-list .我完成了节点,但不确定如何将它实现到队列linked-list I run the program and it works getting files in order but the program must have a needed linked list structure to implement the queue.我运行该程序,它可以按顺序获取文件,但是该程序必须具有实现队列所需的链表结构。

struct Node {
    int value;
    Node* next;
    // Constructor
    Node(int a, Node* next1 = nullptr) // Constructor function inside (inline in) the struct definition
    {
        value = a;
        next = next1;
    }
};
void drawTriangle(int lines);
void displayList(Node*);
int main()
{
    // Format a DOS system command string
    string lsCmd = "ls ../../Assignment8/Assignment8/triangle*.txt > Assignment9jobQueue.txt"; //Open Assignment9jobQueue.txt to get names of triangle job files (will need to parse file names from this file)
    // Define an input file object
    ifstream triangleFile;
    // Loop looking for triangle job files
    // System call to find files
    int lines = 20;
    Node* texts = nullptr;
    string files;
    system(lsCmd.c_str());
    triangleFile.open("Assignment9jobQueue.txt");
    //getline works well for parsing out file names (store them in a queue)
    while (getline(triangleFile, files)) { // Process files in a while loop (while there are names in the file)
        ifstream temp;
        string strTemp;
        temp.open(files);
        getline(temp, strTemp);
        lines = stoi(strTemp);
        texts = new Node(lines);
        displayList(texts);
        cout << endl;
        cout << "Drawing Triangle: " << endl;
        drawTriangle(lines); // Draw the triangle
        remove(files.c_str());
    }
    if (!getline(triangleFile, files)) {
        cout << "No files to process . . ." << endl;
        sleep(10);
    }

    return 0;
}

A linked list is just nodes pointing to nodes.链表只是指向节点的节点。 You need a head pointer that points to the first node which you use to iterate through your linked list.您需要一个指向第一个节点的head指针,用于遍历您的链表。 A queue is first-in-first-out so you should implement a pop() function that removes and returns the last node of the list as well as a push(node) function that points to head then updates the head pointer to point to the node you just added队列是先进先出的,因此您应该实现pop() function 删除并返回列表的最后一个节点以及指向head然后更新head指针指向的push(node) function您刚刚添加的节点

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

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