C错误答案中的链表的简单实现

时间:2018-09-20 09:12:06

标签: c pointers linked-list

我是C语言中的数据结构的新手。我想通过链表实现字典数据结构。

我期望以下输出:

  

Pankaj = 10

     

Pankaj = 20

     

Kumar = 30

     

Kumar = 30

     

Kumar = 30

但是相反,我收到了一些我无法纠正的警告和错误:

justprint.c: In function ‘push’:
justprint.c:19:15: warning: assignment to ‘int’ from ‘void *’ makes integer from pointer without a cast [-Wint-conversion]
   node->phone = malloc(sizeof(int));
               ^
justprint.c: In function ‘main’:
justprint.c:50:12: warning: passing argument 1 of ‘push’ from incompatible pointer type [-Wincompatible-pointer-types]
       push(&start, &phone[i], *name[i]);
            ^~~~~~
justprint.c:13:28: note: expected ‘struct nodetype *’ but argument is of type ‘struct nodetype **’
 void push(struct nodetype *head, int *phone, char *name[]) {
           ~~~~~~~~~~~~~~~~~^~~~
justprint.c:50:31: warning: passing argument 3 of ‘push’ makes pointer from integer without a cast [-Wint-conversion]
       push(&start, &phone[i], *name[i]);
                               ^~~~~~~~
justprint.c:13:52: note: expected ‘char **’ but argument is of type ‘char’
 void push(struct nodetype *head, int *phone, char *name[]) {
                                              ~~~~~~^~~~~~
Segmentation fault

这是我用C语言编写的完整代码:

请注意,我正在将新数据添加到链接列表的头部。我在打印字符串时遇到问题,并且摆脱了错误和警告。

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

struct nodetype{
    int phone;
    char **name;
    struct nodetype * next;
};

struct nodetype *head = NULL;

void push(struct nodetype *head, int *phone, char *name[]) {

  struct nodetype* node = malloc(sizeof(struct nodetype));

  size_t size=strlen(*name);

  node->name = malloc(size);

  node->next = head;

  int i; 
  for (i=0; i<size; i++) 
    *(node->name + i) = *(name + i); 
  (node->phone)=*phone;

  *head = *node;

}

void print(struct nodetype *node) 
{ 
    while (node != NULL) 
    { 
      printf("%s=%d\n", *node->name, node->phone);
      node = node->next; 
    } 
} 

int main() 
{ 
    struct nodetype *start = NULL; 

    int phone[] = {10, 20, 30, 40, 50}, i;
    char *name[] = {"Pankaj","Pankaj","Kumar","Kumar","Kumar"};
    for (i=0; i<5; i++) 
      push(start, &phone[i], &name[i]);
    print(start); 

    return 0; 
} 

我已经阅读了很多有关链表,字典,指针等的在线文章,每种文章都有自己的实现方法,这让我感到困惑。我已经读了很多。

1 个答案:

答案 0 :(得分:1)

您的代码中有很多错误,导致未定义的行为。

以下是少数。

  1. void push(struct nodetype *head, int *phone, char *name[])应该是

    void push(struct nodetype **head, int phone, char name[])

    如果您打算更改head

  2. node->phone = malloc(sizeof(int));,因为phone的类型为int,因此不分配内存。

您的代码应如下所示,

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

struct nodetype{
    int phone;
    char *name;
    struct nodetype * next;
};

struct nodetype *head = NULL;

void push(struct nodetype **head, int phone, char name[]) {

  struct nodetype* node = malloc(sizeof(struct nodetype));

  unsigned size=strlen(name);
  node->name = malloc(size+1);

  node->next = *head;

  int i; 
  for (i=0; i<=size; i++) 
    node->name[i] = name[i];
  node->phone=phone;

  *head = node;

}

void print(struct nodetype *node) 
{ 
    while (node != NULL)
    {
      printf("%s=%d\n", node->name, node->phone);
      node = node->next;
    }
} 

int main() 
{  
    int phone[] = {10, 20, 30, 40, 50}, i;
    char *name[] = {"Pankaj","Pankaj","Kumar","Kumar","Kumar"};
    for (i=0; i<5; i++) 
      push(&head, phone[i], name[i]);
    print(head); 

    return 0; 
}