【问题标题】:Insert a Node at the Tail of a Linked List在链表的尾部插入一个节点
【发布时间】:2016-01-10 21:50:27
【问题描述】:
Node* Insert(Node *head,int data)

{

  Node *current=head;

   Node *new=(Node *)malloc(size0f(Node));

   new->data=data;

   while(current->next!=NULL)

{

current=current->next;

    }

    current->next=new;

    new->next=NULL;

    return head;

}

错误:

solution.cc: In function 'Node* Insert(Node*, int)': 
solution.cc:23:10: error: expected unqualified-id before 'new' Node *new=(Node *)malloc(size0f(Node)); ^ 
solution.cc:23:10: error: expected initializer before 'new' 
solution.cc:24:7: error: expected type-specifier before '->' token new->data=data; ^ 
solution.cc:29:22: error: expected type-specifier before ';' token current->next=new; ^ 
solution.cc:30:8: error: expected type-specifier before '->' token new->next=NULL; 

【问题讨论】:

  • solution.cc: In function 'Node* Insert(Node*, int)': solution.cc:23:10: error: expected unqualified-id before 'new' Node *new=(Node *)malloc(size0f(节点)); ^ solution.cc:23:10: 错误:'new' solution.cc:24:7 之前的预期初始化程序:错误:'->' token new->data=data 之前的预期类型说明符; ^ solution.cc:29:22: 错误: ';' 之前的预期类型说明符令牌当前->下一个=新; ^ solution.cc:30:8: error: '->' token new->next=NULL 之前的预期类型说明符; ^

标签: data-structures


【解决方案1】:

我认为new 是一个关键字,而您正试图将其用作

中的变量名
`Node *new=(Node *)malloc(size0f(Node));` 

和后续代码。

尝试将其替换为 newNode 之类的内容,如下所示

`Node *newNode=(Node *)malloc(size0f(Node));` 

然后在后续代码中使用newNode代替new

【讨论】:

    【解决方案2】:

    我猜@vmachan 是对的,你不应该使用new 作为变量名。

    此外,我认为您在寻找 sizeof 而不是 size0f

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-15
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      相关资源
      最近更新 更多