0

大家好,我已经查看了其他类似问题的问题,但找不到类似的东西。我在代码的第 69 行遇到错误(根据标题),我不确定如何修复它。我的代码无法编译 atm。该程序旨在获取键值对并形成排序的字典链表。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
  int key;
  char value[256];
  struct node *next;
} node;

void findloc(int key, node*headnode);
void insert(node*newinserte, node*previous, node*after);

int main()
{
  makedic();
}

int makedic()
{
  int keydata, once;
  int compareval = 0;
  int i = 0;
  char valuedata[256];
  node * root, *head, *tmp;
  head = NULL;
  while (scanf("%d %s", &keydata, &valuedata) != EOF)
  {
    root = (node*) malloc(sizeof(node));
    root->key = keydata;
    strcpy(root->value, value data);
    root->next = head;
    head = root;
    if (head != NULL && once == 0)
    {
      tmp = head;
      once++;
    }
    findloc(root->key, tmp);
  }
  /*for(; p1->next!=NULL;p1 = p1->next)
   {
   for (p2 = p1->next;p2!=NULL;p2=p2->next)
   {
   if(p1->key>p2->key)
   {
   int temp = p1 ->key;
   p1 ->key = p2->key;
   p2 ->key =temp;
   compareval = compareval +1;
   }
   }
   }*/
  //root = root -> next;  
  while (root)
  {
    printf("%d %s\n", root->key, root->value);
    root = root->next;
  }
  printf("%d\n", compareval);
}

void findloc(int keysearch, node*headnode)
{
  int i;
  node*head, *root;
  head = headnode;
  while (headnode->next != NULL )
  {
/*line 69*/    if (keysearch < headnode->next->key) //error is here
    {
      if (keysearch > headnode->key)
      {
        //insert(headnode ,headnode->next,headnode->next->next );
      }
    }
  }
}

void insert(node*newinserte, node*previous, node*after)
{
  int tmp = after;
  previous->next = newinserte;
  newinserte->next = tmp;
}
4

1 回答 1

1

这个

typedef struct
{
  int key;
  char value[256];
  struct node *next;
} node;

应该是这样的:

typedef struct node
{
  int key;
  char value[256];
  struct node * next;
} node;

否则,该成员struct node * next将指向未知类型,即struct node.


注意:虽然前者是完全有效的,但为了减少可能的混淆,我会用以下方式声明它:

typedef struct node
{
  int key;
  char value[256];
  struct node * next;
} Node;
于 2013-08-31T07:27:22.227 回答