Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将新工作进程添加到列表中,但按名称排序_C_List - Fatal编程技术网

C 将新工作进程添加到列表中,但按名称排序

C 将新工作进程添加到列表中,但按名称排序,c,list,C,List,我必须在员工列表中添加一名新员工,但我必须按字母顺序将其放在正确的位置 我为新名称提供了几个选项: 列表是空的,所以我只是把新名字放在列表中 列表中有1个工作进程 新名称名称,名称); 新建->城市=城市; 新建->id=id; 新建->薪资=薪资; if(head==NULL)//检查列表是否为空// { 新建->下一步=空; 头=新的; 回流头; } if(length(head)==1)//检查列表中是否只有1个人// { 如果(标题->名称名称) { head->next=新建; 新建-

我必须在员工列表中添加一名新员工,但我必须按字母顺序将其放在正确的位置

我为新名称提供了几个选项:

  • 列表是空的,所以我只是把新名字放在列表中
  • 列表中有1个工作进程
  • 新名称<现有名称
  • 新名字比前一个大,但比下一个小
  • 新名字比姓大
  • 我哪里出错了

    //first function-add a new worker to the list//
    Worker * addWorker(Worker *head, char name[], char *city, int id, float 
    salary)
    {
    Worker *new = (Worker *)malloc(sizeof(Worker));
    strcpy(new->name, name);
    new->city = city;
    new->id = id;
    new->salary = salary;
    if (head == NULL)  //checks if the list is empty//
    {
        new->next = NULL;
        head = new;
        return head;
    }
    if (length(head) == 1) //checks if the list has 1 person only //
    {
        if (head->name < new->name)
        {
            head->next = new;
            new->next = NULL;
            return head;
        }
        if (head->name > new->name)
        {
            new->next = head;
            return new;
        }
    }
    Worker *x = head;
    Worker *y = head->next;
    while (x != NULL)
    {
        if (x->name > new->name) //checks if the name needs to be first in 
    the list//
        {
            new->next = x;
            return new;
        }
        if ((x->name < new->name) && (y->name > new->name)) // checks if the 
    name needs to be somewhere in the middle of the list//
        {
            x->next = new;
            new->next = y;
            return head;
        }
    
        if ((x->name < new->name) && (y==NULL)) //checks if the name needs to 
    be last in the list//
        {
            x->next = new;
            new->next = NULL;
            return head;
        }
        x = x->next;
        y = y->next;
    }
    
    return head;
    
    }
    
    //第一个函数向列表中添加新工作进程//
    Worker*addWorker(Worker*head,char name[],char*city,int-id,float
    薪金)
    {
    Worker*new=(Worker*)malloc(sizeof(Worker));
    strcpy(新建->名称,名称);
    新建->城市=城市;
    新建->id=id;
    新建->薪资=薪资;
    if(head==NULL)//检查列表是否为空//
    {
    新建->下一步=空;
    头=新的;
    回流头;
    }
    if(length(head)==1)//检查列表中是否只有1个人//
    {
    如果(标题->名称<新建->名称)
    {
    head->next=新建;
    新建->下一步=空;
    回流头;
    }
    如果(标题->名称->新建->名称)
    {
    新建->下一步=头部;
    归还新的;
    }
    }
    工人*x=人头;
    工人*y=头部->下一步;
    while(x!=NULL)
    {
    if(x->name>new->name)//检查名称是否需要排在第一位
    名单//
    {
    新建->下一步=x;
    归还新的;
    }
    if((x->namename)&&&(y->name>new->name))//检查
    名字需要在列表中间的某个地方。
    {
    x->next=新建;
    新建->下一步=y;
    回流头;
    }
    if((x->namename)&&&(y==NULL))//检查名称是否需要
    排在最后//
    {
    x->next=新建;
    新建->下一步=空;
    回流头;
    }
    x=x->next;
    y=y->next;
    }
    回流头;
    }
    
    对不起,您的代码太复杂了,我不想试着去理解它

    如果addWorker返回新的worker(而不是列表的新头),则第一个参数必须是存储头的位置,因此
    worker**head
    。之后,您只需在列表上迭代到正确的位置即可插入新的工作进程

    要比较字符串,请使用strcmp

    解决办法是:

    //first function-add a new worker to the list//
    Worker * addWorker(Worker ** head, char name[], char *city, int id, float salary)
    {
      Worker * new = malloc(sizeof(Worker));
    
      strcpy(new->name, name); /* hope the name is not too long to be saved in Worker */
      new->city = city; /* hope the city is always a valid string */
      new->id = id;
      new->salary = salary;
    
      while ((*head) && (strcmp(name, (*head)->name) > 0))
        head = &(*head)->next;
    
      new->next = *head;
      *head = new;
    
      return new;
    }
    
    请注意,对于城市和名字来说,没有相同的行为是很奇怪的,但我尊重这一点


    如果我有一些定义,有一个完整的程序,包括打印和删除:

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    typedef struct Worker {
      char name[10];
      const char * city;
      int id;
      float salary;
      struct Worker * next;
    } Worker;
    
    //first function-add a new worker to the list//
    Worker * addWorker(Worker ** head, char name[], char *city, int id, float salary)
    {
      Worker * new = (Worker *)malloc(sizeof(Worker));
    
      strcpy(new->name, name); /* hope the name is not too long to be saved in Worker */
      new->city =  city; /* hope the city is always a valid string */
      new->id = id;
      new->salary = salary;
    
      while ((*head) && (strcmp(name, (*head)->name) > 0))
        head = &(*head)->next;
    
      new->next = *head;
      *head = new;
    
      return new;
    }
    
    void pr(Worker * l)
    {
      while (l != NULL) {
        printf("%s %s %d %f\n", l->name, l->city, l->id, l->salary);
        l = l->next;
      }
    }
    
    void del(Worker * l)
    {
    
      while (l != NULL) {
        Worker * w = l;
    
        l = l->next;
        free(w);
      }
    }
    
    int main()
    {
      Worker * l = NULL;
    
      addWorker(&l, "aze", "c1", 1, 1);
      addWorker(&l, "qsd", "c2", 2, 2);
      addWorker(&l, "aaa", "c3", 3, 3);
      addWorker(&l, "wxc", "c4", 4, 4);
    
      pr(l);
    
      del(l);
    }
    
    在valgrind下执行:


    第二个建议,如果addWorker返回列表的新标题,而不是新的worker,则返回addWorker和main的新定义


    您无法将字符串与
    进行比较,谢谢您这样的评论-if(strcmp(x->name,new->name)>0)//检查名称是否需要位于列表的第一位?如果是这样,则
    x->name
    按字母顺序排列比
    new->name
    晚,抱歉代码太长了,很遗憾,我不得不使用上面给出的函数签名,所以我不能真正使用你的代码。还有其他提示吗?@Gimi是必须遵守的签名,这意味着返回值是新列表,好的,我编辑我的答案…它必须完全像这样:Worker*addWorker(Worker*head,char name[],char*city,int id,float salary)注意它是Worker*head,而不是Worker**head,就像你的code@Gimi是的,我看过,看看新版本
    pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall w.c
    pi@raspberrypi:/tmp $ ./a.out
    aaa c3 3 3.000000
    aze c1 1 1.000000
    qsd c2 2 2.000000
    wxc c4 4 4.000000
    
    pi@raspberrypi:/tmp $ valgrind ./a.out
    ==17053== Memcheck, a memory error detector
    ==17053== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==17053== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
    ==17053== Command: ./a.out
    ==17053== 
    aaa c3 3 3.000000
    aze c1 1 1.000000
    qsd c2 2 2.000000
    wxc c4 4 4.000000
    ==17053== 
    ==17053== HEAP SUMMARY:
    ==17053==     in use at exit: 0 bytes in 0 blocks
    ==17053==   total heap usage: 5 allocs, 5 frees, 1,136 bytes allocated
    ==17053== 
    ==17053== All heap blocks were freed -- no leaks are possible
    ==17053== 
    ==17053== For counts of detected and suppressed errors, rerun with: -v
    ==17053== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
    
    Worker * addWorker(Worker * head, char name[], char *city, int id, float salary)
    {
      Worker ** phead = &head;
      Worker * new = (Worker *)malloc(sizeof(Worker));
    
      strcpy(new->name, name); /* hope the name is not too long to be saved in Worker */
      new->city =  city; /* hope the city is always a valid string */
      new->id = id;
      new->salary = salary;
    
      while ((*phead) && (strcmp(name, (*phead)->name) > 0))
        phead = &(*phead)->next;
    
      new->next = *phead;
      *phead = new;
    
      return (new->next == head) ? new : head;
    }
    
    int main()
    {
      Worker * l = NULL;
    
      l = addWorker(l, "aze", "c1", 1, 1);
      l = addWorker(l, "qsd", "c2", 2, 2);
      l = addWorker(l, "aaa", "c3", 3, 3);
      l = addWorker(l, "wxc", "c4", 4, 4);
    
      pr(l);
    
      del(l);
    }