C++ 为可变大小的链表动态创建节点

C++ 为可变大小的链表动态创建节点,c++,linked-list,C++,Linked List,对于作业,我的任务是根据文件输入创建多维链表。每个节点都包含一个座位对象,其中包含文件的行、列(作为字符)和字符数据。例如,如果文件是 ABC DEF GHI 然后链表将是(其中-和|表示连接的指针) 我目前正试图根据文件中的元素数量,首先创建所有节点。我已经创建了head节点,但是我不确定如何分配剩余的可变数量的节点。到目前为止,我有两个for循环遍历文件的每个字符,以收集行、列和字符,如下所示: scanner.open(fileName); if(scanner) {

对于作业,我的任务是根据文件输入创建多维链表。每个节点都包含一个座位对象,其中包含文件的行、列(作为字符)和字符数据。例如,如果文件是

ABC
DEF
GHI
然后链表将是(其中-和|表示连接的指针)

我目前正试图根据文件中的元素数量,首先创建所有节点。我已经创建了head节点,但是我不确定如何分配剩余的可变数量的节点。到目前为止,我有两个for循环遍历文件的每个字符,以收集行、列和字符,如下所示:

scanner.open(fileName);
   if(scanner)
      {
         for(int i = 0; i < cols; i++)
            {
               for(int j = 0; j < rows; j++)
                  {
                     if(i == 0 && j == 0)
                        {
                           Node<Seat> *headNode = new Node<Seat>(Seat(j, letter, c));
                           headNode->up = NULL;
                           headNode->left = NULL;
                           setFirst(headNode);
                           continue;
                        }
                     scanner >> c;
                     letter = ('A' + i);
                     Node<Seat> *curNode = new Node<Seat>(Seat(j, letter, c));
                     std::cout << "Row, seat, ticket: " << curNode->getPayload().getRow() << curNode->getPayload().getSeat() << curNode->getPayload().getTicketType() << std::endl;
                  }

            }
      }
}
scanner.open(文件名);
if(扫描仪)
{
for(int i=0;iup=NULL;
headNode->left=NULL;
setFirst(头节点);
继续;
}
扫描仪>>c;
字母=('A'+i);
节点*curNode=新节点(座位(j,字母,c));

std::coutIMHO,每个节点都需要四个链接。想象一个棋盘

struct Node
{
    Node * up;
    Node * down;
    Node * left;
    Node * right;
};
对于
up
字段,顶行将有一个空ptr值。对于其他边也是如此


添加节点需要调整适当的链接。

我要做的是保留两组指针:

  • “最后一行”和“此行”指针,用于跟踪行的开头
  • “最后一列节点”、“最后一行节点”和“此节点”指针,用于跟踪同级节点
然后重复这些步骤:

  • 从文件中读取名称
  • 创建一个节点
  • 勾搭兄弟姐妹
  • 更新指针,使“last…”指向“this…”
  • 以下是一个示例:

    #include <fstream>
    #include <sstream>
    #include <iostream>
    #include <string>
    
    struct Node {
        std::string name;
        Node *up, *down, *left, *right;
        Node(std::string name) : name(name), up(NULL), down(NULL), left(NULL), right(NULL) {}
    };
    
    void dump(Node *this_node) {
        std::cout
            << " name="  << this_node->name
            << " up="    << (this_node->up?this_node->up->name:" ")
            << " down="  << (this_node->down?this_node->down->name:" ")
            << " left="  << (this_node->left?this_node->left->name:" ")
            << " right=" << (this_node->right?this_node->right->name:" ")
            << std::endl;
    }
    
    Node *print(Node *head) {
        Node *this_row = head;
        while (this_row) {
            Node *this_node = this_row;
            while (this_node) {
                dump(this_node);
                this_node = this_node->right;
            }
            this_row = this_row->down;
        }
        return head;
    }
    
    Node *read() {
        Node *head = NULL;
        std::fstream file("input.txt");
        if (file) {
            std::string line;
            Node *last_row = NULL;
            Node *this_row = NULL;
            while (std::getline(file,line)) {
                std::string name;
                std::stringstream ss(line);
                Node *last_row_node = NULL;
                Node *last_col_node = last_row;
                Node *this_node = NULL;
                while (ss >> name) {
                    this_node = new Node(name);
                    // if we haven't set these yet then do it now
                    if (!head) head = this_node;
                    if (!this_row) this_row = this_node;
                    // if there was a last column then use it
                    if (last_col_node) {
                        last_col_node->down = this_node;
                        this_node->up = last_col_node;
                    }
                    // if there was a last row then use it
                    if (last_row_node) {
                        last_row_node->right = this_node;
                        this_node->left = last_row_node; 
                    }
                    // move right one column
                    last_row_node = this_node;
                    if (last_col_node)
                        last_col_node = last_col_node->right;
                }
                // move down one row
                last_row = this_row;
                this_row = NULL;
            }
        }
        return head;
    }
    
    int main() {
        Node *head = read();
        print(head);
    }
    

    错误的方法。您将有多个头部节点,每个列表的头部各有一个(例如,
    A
    B
    C
    。然后创建一个
    add_node()
    函数,当从文件中读取每个节点时,该函数将分配并初始化每个节点,并将其添加到正确的列表中(基于列号)。尝试首先分配所有内容就像尝试在圆孔中塞进一个方钉——这不是您真正想要做的。
    add()
    示例可以在这里找到,如果您的节点最多限制为四个链接(如您的示例所示),则为模板示例我建议不要让它们变大,只需为每个节点分配四个链接,并适当地保留一些空链接。我建议从一维列表开始,然后扩展到第二维。列需要是列表吗?如果它只是一个行列表,这会更简单。@Siroos我要提醒您,这不是你的学校。我们不会给你的家庭作业评分。你可以在这里修改(一份)你的代码,为你的问题创建一个更简单的答案。事实上,这样的修改是受鼓励的!忘了提到我有那些链接,哎呀。
    模板类节点{public:Node*up;Node*down;Node*left;Node*right;
    不要在注释中放置代码。用代码编辑你的问题。这很好,我一直在尝试调整它-我如何编辑它,使它可以使用没有空格的字符?我忘了提到我的文件看起来更像ABC,没有空格。
    #include <fstream>
    #include <sstream>
    #include <iostream>
    #include <string>
    
    struct Node {
        std::string name;
        Node *up, *down, *left, *right;
        Node(std::string name) : name(name), up(NULL), down(NULL), left(NULL), right(NULL) {}
    };
    
    void dump(Node *this_node) {
        std::cout
            << " name="  << this_node->name
            << " up="    << (this_node->up?this_node->up->name:" ")
            << " down="  << (this_node->down?this_node->down->name:" ")
            << " left="  << (this_node->left?this_node->left->name:" ")
            << " right=" << (this_node->right?this_node->right->name:" ")
            << std::endl;
    }
    
    Node *print(Node *head) {
        Node *this_row = head;
        while (this_row) {
            Node *this_node = this_row;
            while (this_node) {
                dump(this_node);
                this_node = this_node->right;
            }
            this_row = this_row->down;
        }
        return head;
    }
    
    Node *read() {
        Node *head = NULL;
        std::fstream file("input.txt");
        if (file) {
            std::string line;
            Node *last_row = NULL;
            Node *this_row = NULL;
            while (std::getline(file,line)) {
                std::string name;
                std::stringstream ss(line);
                Node *last_row_node = NULL;
                Node *last_col_node = last_row;
                Node *this_node = NULL;
                while (ss >> name) {
                    this_node = new Node(name);
                    // if we haven't set these yet then do it now
                    if (!head) head = this_node;
                    if (!this_row) this_row = this_node;
                    // if there was a last column then use it
                    if (last_col_node) {
                        last_col_node->down = this_node;
                        this_node->up = last_col_node;
                    }
                    // if there was a last row then use it
                    if (last_row_node) {
                        last_row_node->right = this_node;
                        this_node->left = last_row_node; 
                    }
                    // move right one column
                    last_row_node = this_node;
                    if (last_col_node)
                        last_col_node = last_col_node->right;
                }
                // move down one row
                last_row = this_row;
                this_row = NULL;
            }
        }
        return head;
    }
    
    int main() {
        Node *head = read();
        print(head);
    }
    
    name=A up=  down=D left=  right=B
    name=B up=  down=E left=A right=C
    name=C up=  down=F left=B right= 
    name=D up=A down=G left=  right=E
    name=E up=B down=H left=D right=F
    name=F up=C down=I left=E right= 
    name=G up=D down=  left=  right=H
    name=H up=E down=  left=G right=I
    name=I up=F down=  left=H right=