关于单链表返回错误的C程序

时间:2014-11-20 11:16:14

标签: c sorting malloc

我试图找出下面代码的问题。在这里,我尝试创建一个字符串的链接列表,按字母顺序对字符串进行排序,但在[在Ubuntu上]编译时,它显示错误。

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

 struct node_type {
   char data[21];
  struct node_type *next;
   };
  typedef struct node_type list;

   void showList ();
   list *sortInsert();
    list *createNode();
   list *find();
       main()
     {
    list *newnode, *start = NULL;
     char c = 'y';
     char word[21];

        while(c != 'n' && c!= 'N')
        {
      printf("\n Enter the word:");
      scanf("%s", word); fflush(stdin);
   newnode = createNode();
   strcpy(newnode->data, word);
    newnode->next = NULL;
      if(!find(start,newnode->data))
       start = sortInsert(start,newnode);
        printf("\nThe list so far:"); 
      showList(start); 
         printf("\n\n");
         printf("\n Do you want to add new data to the list? (y/n): ");
        scanf("%c", &c); getchar(); fflush(stdin);
          }
          printf("\nThe sorted list is:"); showList(start); printf("\n\n");
            }

        list *createNode()
             {
           list *new;
       new = (list *)malloc(sizeof(list));
         return(new);
                 }

             list *sortInsert(list *start, list *newnode)
        {
           list *prev, *curr;
          if(start==NULL)
             {
   return(newnode);
      }

       curr = start;
         prev=curr;

          if(strcmp(newnode->data, curr->data)<0)
          {
          start=newnode;
         newnode->next = curr;
       return(start);
      }

            while(curr!=NULL)
      {
          curr = curr->next;
               if(curr==NULL)
          {
          prev->next = newnode;
         newnode->next = curr;
           return(start);
          }
        else
          {
         if(strcmp(newnode->data, curr->data)<0)
          {
           prev->next = newnode;
            newnode->next = curr;
         return(start);
       }
          prev = prev->next;
        }
        }
       return(start);
          }
        list *find(list *st, int dt)
         {
         while(st)
            if(strcmp(st->data,dt) == 0)
            return (st);
       else
                st = st->next;
            return(st);
        }


          void showList(list *temp)
         {
          while(temp)
          {
            printf("%s", temp->data);
           temp = temp->next;
          }
                 printf("\n");
           }

Linux终端上的错误是

       part4.c: In function ‘find’:
       part4.c:89:3: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
       if(strcmp(st->data,dt) == NULL)
       ^
       In file included from part4.c:3:0:
        /usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of          type ‘int’
       extern int strcmp (const char *__s1, const char *__s2)
                       ^
     part4.c:89:26: warning: comparison between pointer and integer [enabled by default]
     if(strcmp(st->data,dt) == NULL)

请让我知道可能是什么解决方案。

1 个答案:

答案 0 :(得分:2)

问题:

if(strcmp(st->data,dt) == NULL),此处dtint类型的变量...

strcmp()需要const char *作为i / p参数。查看手册页here

解决方案:

在您的代码中,您将find()称为

find(start,newnode->data)

newnode->data的类型为char,因此,find()的函数签名存在问题。将您的find()功能更改为

list *find(list *st, const char * dt)