【问题标题】:Undeclared identifier linked list in CC中未声明的标识符链表
【发布时间】:2014-05-27 12:01:08
【问题描述】:

我完全是 C 的菜鸟,试图制作一个功能链表。使用 listInsert 函数按字母顺序对列表进行排序。 但是有一个问题,我得到一个 C2059 语法错误和 C2065 错误:'listEntry' : undeclared identifier on the following line in listInsert function:

newNode = (listEntry *)malloc(sizeof(listEntry));

就像我说的我是一个完整的菜鸟,但我在任何地方都没有看到任何丢失的括号,我很确定那是定义 newNode 的行? 我们将不胜感激所有帮助,谢谢。

另外,这里是完整的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SUCCESS 0
#define FAIL    1

char *phonetic[] = { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot",
                     "golf", "hotel", "india", "juliet", "kilo", "lima", "mike",
                     "november", "oscar", "papa", "quebec", "romeo", "sierra",
                     "tango", "uniform", "victor", "whisky", "xray", "yankee", 
                     "zulu" };

unsigned char indexes[] = { 1, 14, 17, 3, 22, 0, 5, 18, 24, 11, 4, 6, 13, 21,
                            2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };                       

// represents an entry in the linked-list
struct listEntry
{
  char *data_p;               // pointer to the entry's string
  struct listEntry *prev_p;   // pointer to previous entry in the linked-list  
  struct listEntry *next_p;   // pointer to next entry in the linked-list
};

// represents the linked-list
struct list
{
  int entryCount;             // number of entries present in the linked-list
  struct listEntry *head_p;   // pointer to the first entry in the list  
  struct listEntry *tail_p;   // pointer to the last entry in the list
};

// Dynamically allocate & initialise an empty linked list
int listCreate(struct list** list_p2)
{
  // allocate struct list from heap 
  *list_p2 = (struct list*) malloc(sizeof(**list_p2));

  if (*list_p2 != NULL)
  {
    // zero-initialize the list structure 
    memset(*list_p2, 0, sizeof(**list_p2));
    return SUCCESS;    
  }

  return FAIL;
}

// Free all entries in the linked-list and the list structure
int listDestroy(struct list *list_p)
{
  if (list_p != NULL)
  {
    struct listEntry *entry_p = list_p->head_p;

    while (entry_p != NULL)
    {
      struct listEntry *next_p = entry_p->next_p;
      // free the current entry
      free(entry_p);
      // move to the next entry
      entry_p = next_p;
    }

    // free list structure
    free(list_p);
  }

  return FAIL;
}

// Traverse the linked-list from head to tail printing out
// the string data from each list entry
int listPrintForward(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->head_p;
    int count = 0;

    printf("FORWARD: %d entries\n", list_p->entryCount);
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->tail_p)
        printf("\n");

      entry_p = entry_p->next_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Traverse the linked-list from tail to head printing out
// the string data from each list entry
int listPrintReverse(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->tail_p;
    int count = 0;

    printf("REVERSE: %d entries\n", list_p->entryCount);   
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->head_p)
        printf("\n");

      entry_p = entry_p->prev_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
void listInsert(struct list *list_p,char *string_p)
{
    struct listEntry *newNode;
    newNode = (listEntry *)malloc(sizeof(listEntry));
  // Please write the listInsert function



    //Special case for the head end.
    if (list_p->head_p == NULL || (list_p->head_p)->data_p >= newNode->data_p)
  {
     newNode->next_p = list_p->head_p;
     list_p->head_p = newNode;
  }
    else
    {
        //Locating the node before which the insertion is to happen.
        struct listEntry* current = list_p->head_p;
        while(current->next_p!= NULL && current->next_p->data_p < newNode->data_p)
        {
            current = current->next_p;
        }
        newNode->next_p = current->next_p;
        current->next_p = newNode;

    }


  //return FAIL;  
}

int main(int argc, char **argv)
{
  struct list *list_p = NULL;
  (void) argc;
  (void) argv;

  if (listCreate(&list_p) == SUCCESS)
  {
    unsigned int count;

    // insert every word in the phonetic alphabet into the
    // linked-list.
    printf("INSERT:\n");
    for (count = 0; count < sizeof(indexes); count++)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", phonetic[indexes[count]]);
      }
      else
      {
        printf("%s ", phonetic[indexes[count]]);
      }
      listInsert(list_p, phonetic[indexes[count]]);
    }
    printf("\n");

    // print out the list in alphabetical order
    listPrintForward(list_p);
    // print out the list in reverse alphabetical order
    listPrintReverse(list_p); 

    // Destroy the linked list and free all associated memory
    listDestroy(list_p);               
  }

  return SUCCESS;
} 

【问题讨论】:

    标签: c sorting linked-list syntax-error undeclared-identifier


    【解决方案1】:

    发生的情况是listEntry 本身并没有描述类型。你需要事先添加struct关键字,因为你没有typedef'd listEntry

    请参阅this

    【讨论】:

      【解决方案2】:

      你可以简单地写

      newNode = malloc(sizeof newNode);
      

      它应该可以编译没有问题,而且更好,因为如果你改变了newNode的类型,你不需要修改它。

      【讨论】:

        【解决方案3】:

        试试这个:

        newNode = (struct listEntry *)malloc(sizeof(struct listEntry));
        

        或者您可以定义新类型并使用它:

        typedef struct listEntry
        {
          char *data_p;               // pointer to the entry's string
          struct listEntry *prev_p;   // pointer to previous entry in the     linked-list
          struct listEntry *next_p;   // pointer to next entry in the linked-list
        } t_listEntry;
        
        
        
        newNode = (t_listEntry *)malloc(sizeof(t_listEntry));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多