【问题标题】:linked list creation problem at compilation in this way of solving it [closed]以这种方式解决它的编译时的链表创建问题[关闭]
【发布时间】:2018-11-14 09:21:46
【问题描述】:

帮忙看看上面写着 //PLEASE TELL ME PROBLEM IN THIS LINE

包括

#include <stdlib.h>   

// A linked list node 
typedef struct Node 
{ 
int info; 
struct Node *link; 
}node; 

//insert in a linked list

void insert(node* head, int k)
{
 node* temp = (node*) malloc(sizeof(node));
if(temp==NULL)
  {
      printf("malloc was unsuccessfull");
      exit(0);
  }
  else
  {
      temp->info=k;
      temp->link=NULL;
      if(head==NULL)
      head = temp;
      else
      {
      temp->link=head;
      head=temp;
      }
  }

  // This function prints contents of linked list starting from head 
void print(node* a) 
{ 
  while (a != NULL) 
  { 
     printf(" %d ", a->link); //PLEASE TELL ME PROBLEM IN THIS LINE
     a = a->link; 
  } 
}

}
void main() 
{ 
  /* Start with the empty list */
  node* head = NULL;

  insert(head, 7);
  insert(head, 9);

  printf("\n Created Linked list is: "); 
  print(head); //PLEASE TELL ME PROBLEM IN THIS LINE

}  

错误消息

prog.c: In function 'print':

prog.c:40:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'struct Node *' [-Wformat=]
      printf(" %d ", a->link); 
             ^
prog.c: In function 'main':

prog.c:55:3: warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]
   print(head);
   ^


/tmp/cc1HDBBm.o: In function `main':
3192773816853040ef42d0aa4269a062.c:(.text+0xeb): undefined reference to `print'
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 您的意思是打印a-&gt;info(这是一个int,可以用%d 打印)而不是a-&gt;link
  • 看起来“%d”需要“int”类型的参数,但参数 2 的类型为“struct Node *”。您不应该打印info 成员而不是指向下一个节点的指针吗?
  • 正确缩进你的代码,最后一个问题应该很清楚了。
  • 函数'print'的隐式声明意味着1)你忘记了stdio.h和2)你在废话模式下使用gcc,而你应该将它用作C编译器:gcc -std=c11 -pedantic-errors

标签: c struct linked-list


【解决方案1】:
  1. printf(" %d ", a-&gt;link); 行的类型为%d,参数为node。你在这里想要的是打印整数

    printf(" %d ", a->info);
    
  2. insert 函数的括号未对齐。在原始代码中,print 的定义insert 的定义中。您需要正确关闭 insert 函数的括号,并且对 print(head); 的调用将收到正确的定义。并正常工作。

  3. 你使用的是void main(),不带任何命令行参数的main的正确格式是int main(void)

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Node {
    
        int information;
        struct Node* next;
    };
    
    struct Node* Create_Node (void);
    void Insert_Node (struct Node* target, int information);
    void Print_Node (struct Node* head);
    
    int main (int argc, char* argv[]) {
    
        struct Node* head = NULL;
        head = Create_Node();
        head->next = NULL;
    
        Insert_Node (head, 7);
        Insert_Node (head, 9);
    
        printf ("\nCreate Linked list is : ");
        Print_Node (head);
    
        // free head
        if (head != NULL) head = NULL;
        free (head);
    
        return 0;
    }
    
    void Print_Node (struct Node* head) {
    
        struct Node* current = NULL;
        current = head->next;
    
        while (current != NULL) {
    
            printf ("%3d ", current->information);
            current = current->next;
        }
        printf ("\n");
    
    
        // free current
        while (current != NULL) {
    
            struct Node* next = current->next;
            if (current != NULL) current = NULL;
            free (current);
            current = next;
        }
    
        return;
    }
    
    void Insert_Node (struct Node* target, int _information) {
    
        struct Node* New_Node = NULL;
        New_Node = Create_Node();
        New_Node->next = target->next;
    
        // data
        New_Node->information = _information;
    
        target->next = New_Node;
    
        return;
    }
    
    struct Node* Create_Node (void) {
    
        struct Node* Pointer_Return = NULL;
        Pointer_Return = (struct Node*) calloc (1, sizeof (struct Node));
    
        if (Pointer_Return == NULL) {
    
            printf ("Error! Dynamic memory allocation, Create_Node()\n");
            return NULL;
        }
        return Pointer_Return;
    }
    

    【讨论】:

    • 请在你的回答中加入一些解释,而不是仅仅发布一段代码
    猜你喜欢
    • 2021-01-17
    • 2012-08-17
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多