c中的编译器错误链表

标签 c linked-list syntax-error insertion-sort

我正在尝试用 C 实现一个链表,我对 C 完全是菜鸟,但擅长 C++。我在使用 malloc() 时遇到此语法错误,不知道为什么。 我收到错误 C2059:语法错误:')'。第 169 行.

该行是

entry_p->next_p = (entry_p *)malloc(sizeof(node));

位于列表插入中。 任何帮助将不胜感激。

编辑:错误已修复,替换为

struct listEntry *entry_p = list_p ->head_p;

但现在我在这条线上发生了程序损坏

while(entry_p->next_p!=NULL)

有什么想法吗?试图在调试器中找出它,但到目前为止还没有运气。

#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
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
};

// represents the linked-list
 typedef 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)
{ 
  // Please write the listInsert function
    struct listEntry *entry_p = list_p ->head_p;
    /* Iterate through the list till we encounter the last node.*/
    while(entry_p->next_p!=NULL)
    {
        entry_p = entry_p ->next_p;
    }
    /* Allocate memory for the new node and put data in it.*/
        entry_p->next_p = (entry_p *)malloc(sizeof(node));
        entry_p = entry_p->next_p;
        entry_p->data_p = string_p;
        entry_p->next_p = NULL;



 // 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;
} 

编辑 2:修复了编译器错误。

这是listInsert的最终状态。

void listInsert(struct list *list_p, char *string_p)
{ 
  // Please write the listInsert function
    //list_p->head_p= NULL;
    struct listEntry *entry_p = list_p ->head_p;
    if (list_p->head_p == NULL)
    {
        struct listEntry *newnode = list_p->head_p;
        list_p->head_p = newnode;

    }
    else
    {

        /* Iterate through the list till we encounter the last node.*/
        while(entry_p->next_p!=NULL)
        {
            entry_p = entry_p ->next_p;
        }
        /* Allocate memory for the new node and put data in it.*/
            entry_p->next_p = (struct listEntry *)malloc(sizeof(struct listEntry));
            entry_p = entry_p->next_p;
            entry_p->data_p = string_p;
            entry_p->next_p = NULL;
    }



 // return FAIL;  
}

最佳答案

常见的错误并不是你头脑中的内容与你在代码中编写的内容不一致。

可能你的头脑中已经将 listEntry 作为链表的节点,并且在你的进度中使用了该术语。这就是你无法找到这个简单错误的原因,即使经过多次检查也是如此。

正确的行是

entry_p->next_p = (struct listEntry  *)malloc(sizeof(struct listEntry));

从 C++ 过渡到 C 的程序员常犯的错误是忘记了 struct 元素之前的 struct 关键字。在 C++ 中,它是可选的,但在 C 中,这是强制性的。

关于c中的编译器错误链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23878232/

相关文章:

c - 在 C 中实现 AES 时的奇怪行为(将 char 转换为 bin)

c - 通过 USART 发送 c char* 时出现问题

java - 为什么在这种情况下使用 LinkedList 迭代器没有 ConcurrentModificationException?

c++ - 将文本文件读入 char 数组,然后读入 char 链表

syntax-error - Protractor 配置文件抛出错误

c - 为什么我的代码使用逻辑表达式作为 case 标签会抛出错误?

c - 通过引用将参数传递给另一个函数是否安全(在 C 中)?

c++ - 如何删除添加到 C++ 链表中的最后一个元素

syntax - 嵌套查询中包含 AS 和 ORDER BY 的 SQL 语法

html - 为什么带有嵌套资源的form_for会出现语法错误?