为 Link 列表传递指针

passing pointers for Link Lists

我有问题。我正在尝试创建一个 link 列表,但我得到这些 errors.I 不知道我是否正确传递了指针。我什至无法让我的程序 运行 正确。

我正在尝试从文件中获取数据并将其放入没有全局头的 link 列表中(我必须传递它)(这被认为是 head1)然后,使用我制作的 link 列表,我需要制作 16 个 link 列表(创建然后销毁,这是 head2),每个列表大小为六个字母。前任。接受前六个,然后是接下来的六个等等。我已经构建了它,但我不知道从哪里开始。

错误信息:

invalid application of 'sizeof' to incomplete type 'struct node' line 85

request for member 'nextData' in something not a structure or union line 91 ( just an example, there are many of these areas.)

代码:

  struct boggleDataNode {
    //(line 14) This is the data portion of the node? So we are storing 96 arrays? or 96 nodes?
    char data[3];
    // (line 16) Is this the pointer variable? This points to the next node in the link list correct?
    struct boggleDataNode *nextData;
};
struct boggleDieSideNode {

    char dieSideData[3];
    struct boggleDieSideNode *nextSide;
};
//creating the function prototypes.
void input(struct boggleDataNode *head);
void addBoggleData(struct boggleDataNode *head, char data);
void displayBoggleData(struct boggleDataNode *head);

//main function
int main()
{
    //creating a var to use as a counter
    int counter = 0;
    struct boggleDataNode *head1 = NULL;
    struct boggleDieSideNode *head2 = NULL;
    //calls the function that reads data intp a file.
    input(head1);
    //displays the data from rhe file
    displayBoggleData(head1);
    //displays the die sides
    displayDieSide(head2);

    //creates a for loop to create the die sides.
    int side, die;
    for(die =0; die<16; die++){
       //resets the entire link list.
        head2 = NULL;
        for(side = 0; side<6; side++){
                //adds the data to the die side (NOT COMPLETE!)
            addDieSideNode(head2, head1);

        }
    }



}
//this function will handle user imput
void input (struct boggleDataNode *head1)
{
    //create  data size as well as bounds
    char fileData[3];
    int bound = 96;
    //file pointer and file info
     FILE *ips;
     ips = fopen("data.txt", "r");
      if (ips == NULL)
        printf("Please check file!\n");
     else {
            //for loop to scan through file, and retrive the letters
            int i;
            for(i=0; i<bound; i++)
              fscanf(ips, "%s", fileData);
              addBoggleData(head1, fileData);
              }
     //closes the file system
              close(ips);
}
void addBoggleData(struct boggleDataNode *head, char data)
{
    //create a helper and temp
    struct boggleDataNode *temp,*helper;
    //allocate memory (error here)
    temp =(struct boggleDataNode *)malloc(sizeof(struct node));
    //this sets the data for my temp variable
     strcpy(temp.data, data);
    if(head==NULL){
        //sets the first portion of the link list
        head = temp;
        temp.nextData = NULL;
//append function 1
}else
    {
        helper = head;

        //(line 70) I dont even know what I am doing. I dont know why temp points to the head, and why I am setting head = temp.
       while(helper.nextData != NULL){

        helper = helper.nextData;

       }
       helper.nextData = temp;

    }



}
//display function
void displayBoggleData(struct boggleDataNode *head){
    //creates a helper var to loop through the link list.
    struct boggleDataNode *helper;
     helper-head;
     if(helper==NULL){
        return;
     }
     //counter to keep track of the data value
    int counter=0;
 while(helper != NULL){
        //prints data
    printf(" Data Value %d %s", &counter, helper.data);
    counter++;
 //moves on
    helper = helper.nextData;
 }


}
//something that I might never figure out.
void addBoggleSide()
// Incomplete

首先,don't cast the return value of malloc() in C。 然后,

but I dont know where to start.

让我通过提供几个 指针 (双关语).

来帮助您

在您的代码中出现问题,

  1. tempstruct boggleDataNode *类型,所以内存分配语句

     temp =(struct boggleDataNode *)malloc(sizeof(struct node));
    

    应该是

     temp = malloc(sizeof(struct boggleDataNode ));
    

    或者更确切地说,更好更强大的版本

    temp = malloc(sizeof*temp);   //yes, sizeof operator only needs () 
                                  //    when using a datatype as operand
    

    最后一条语句是独立的,不受 temp.

  2. 类型变化的影响
  3. temp是指针类型,

     temp.nextData = NULL;
    

    应该是

    temp->nextData = NULL;
    

现在,请检查并更正代码中的所有 个类似问题(如果有的话)。