C 程序在单向链表重新排序错误



我正在尝试找出以下代码的问题。在这里,我尝试创建一个字符串链表,按字母顺序对字符串进行排序,但是在编译它时[在 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("nn");
         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("nn");
            }
        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)

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

问题 :

if(strcmp(st->data,dt) == NULL),这里dt是一个类型为int的变量...

strcmp()需要const char *为 I/P 参数。 在此处查看手册页。

溶液:

在代码中,将find()调用为

find(start,newnode->data)

newnode->data属于 char 型,所以,这是你的函数签名的问题 find() .将find()函数更改为

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

相关内容

  • 没有找到相关文章

最新更新