c中的链接列表。如何访问链接列表中的值?

时间:2013-12-27 19:12:59

标签: c++ linked-list

我创建了一个新节点。如何引用node->question->questionnode->question->options[]node->question->correctansnode->question->difficulty_level

struct node {

    node *prev;

    node *next;

    int count; 

    Question question[];

};
struct Question{

    String question;

    String[] options;

    String correctans;

    int difficulty_level;
}

2 个答案:

答案 0 :(得分:0)

' - >'是一个指针解除引用。由于节点中有一个对象数组(而不只是指向它们的指针),访问它们的正确方法是'node.question [x]'。这同样适用于您的Question结构,即node.question [0] .question将产生此节点数组中保存的第一个问题的问题字符串(如果这是有效的C代码......)。

希望这会有所帮助,如果您有任何不清楚之处,请与我联系。

答案 1 :(得分:0)

我不确定你在问什么,但我认为这就是你要找的东西:

#include <string.h>
#include <iostream>
using namespace std;

struct Question{

    string *question;

    string  *options [4];

    string correctans;

    int difficulty_level;
};

struct node {

    node *prev;

    node *next;

    int count; 

    Question *question[4]; 

}n;

int main(){
    Question *q;
    Question qu;

    q->question = new string("This is a question"); // assign some values
    q->options[0]= new string("The first option");
    q->difficulty_level = 4;

    qu.question = new string("another question");
    qu.options[0]= new string("The second option");
    qu.difficulty_level = 5;

    n.question[0] = q;
    n.question[1] = &qu;
    //print the assigned values
    cout << "Question 1 question: " <<  *n.question[0]->question << endl;
    cout << "Question 1 Option 1: "<<*n.question[0]->options[0] << endl;
    cout << "Question 1 dificulty: "<<n.question[0]->difficulty_level << endl;

    cout << "Question 2 question: " <<  *n.question[1]->question << endl;
    cout << "Question 2 Option 1: "<<*n.question[1]->options[0] << endl;
    cout << "Question 2 dificulty: "<<n.question[1]->difficulty_level << endl;

}