Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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_Struct_Doubly Linked List - Fatal编程技术网

C 将名称添加到多链接列表中

C 将名称添加到多链接列表中,c,struct,doubly-linked-list,C,Struct,Doubly Linked List,我正在创建一个名、姓和电话号码的多链接列表。我有一个add函数,为列表分配内存,但我不知道如何添加到这个列表中,它将按字母顺序链接名字和姓氏。如何添加到多链接列表中?它与添加到单个链接列表中有很大不同吗 结构: typedef struct node { char *first; char *last; long number; struct node *nextFirst; struct node *nextLast; } Node; typedef

我正在创建一个名、姓和电话号码的多链接列表。我有一个add函数,为列表分配内存,但我不知道如何添加到这个列表中,它将按字母顺序链接名字和姓氏。如何添加到多链接列表中?它与添加到单个链接列表中有很大不同吗

结构:

typedef struct node {
    char *first;
    char *last;
    long number;
    struct node *nextFirst;
    struct node *nextLast;
} Node;

typedef struct mlist {
    Node *headFirstName;
    Node *headLastName;
} MultiLinkedList;
添加功能:

MultiLinkedList *add(MultiLinkedList *list, char *first, char *last, long num) {
    // allocate a new node
    Node *newNode = malloc ( sizeof(Node) );
    newNode->first = malloc ( strlen(first) + 1 );
    strcpy(newNode->first, first);
    newNode->last = malloc ( strlen(last) + 1 );
    strcpy(newNode->last, last);
    newNode->number = num;
    // add this new node at the head of the "byFirst" list
    newNode->nextFirst = list->headFirstName;
    list->headFirstName = newNode;
    // add this new node at the head of the "byLast" list
    newNode->nextLast = list->headLastName;
    list->headLastName = newNode;
    // return the multi-list object with updated head pointers
    return list;
}
编辑:在这里,我添加了我的添加函数的更新代码,通过添加到用于单个链接列表的列表中进行了修改

MultiLinkedList *add(MultiLinkedList *list, char *first, char *last, long num) {

  Node *tempf = list->headFirstName;
  Node *templ = list->headLastName;

  while (tempf && templ) {
    if (strcasecmp(tempf->first, first) == 0 && strcasecmp(templ->last, last) == 0) {
      printf("Error - name %s %s already exists\n", first, last);
      return list;
    }
    tempf = tempf->nextFirst;
    templ = templ->nextLast;
  }

    // allocate a new node
    Node *newNode = malloc ( sizeof(Node) );
    newNode->first = malloc ( strlen(first) + 1 );
    strcpy(newNode->first, first);
    newNode->last = malloc ( strlen(last) + 1 );
    strcpy(newNode->last, last);
    newNode->number = num;
  newNode->nextFirst = NULL;
  newNode->nextLast = NULL;

  if(list == NULL)
    return newNode;
  if(strcasecmp(first, list->headFirstName->first) < 0){
    newNode->nextFirst = list->headFirstName;
    return newNode;
  }
  Node *prevf = list->headFirstName;
  Node *currf = list->headFirstName->nextFirst;
  while(currf && strcasecmp(currf->first, first) < 0){
    prevf = currf;
    currf = currf->nextFirst;
  }
  prevf->nextFirst = newNode;
  newNode->nextFirst = currf;

  if (list == NULL)
    return newNode;
  if (strcasecmp(last, list->headLastName->last) < 0) {
    newNode->nextLast = list->headLastName;
    return newNode;
  }
  Node *prevl = list->headLastName;
  Node *currl = list->headLastName->nextLast;
  while (currl && strcasecmp(currl->last, last) < 0) {
    prevl = currl;
    currl = currl->nextLast;
  }
  prevl->nextLast = newNode;
  newNode->nextLast = currl;

    // add this new node at the head of the "byFirst" list
    newNode->nextFirst = list->headFirstName;
    list->headFirstName = newNode;
  //list->headFirstName = addToFirstNameList(list->headFirstName, newNode);

    // add this new node at the head of the "byLast" list
    newNode->nextLast = list->headLastName;
    list->headLastName = newNode;
  //list->headLastName = addToLastNameList(list->headLastName, newNode);

    // return the multi-list object with updated head pointers
    return list;
}
multilykedlist*添加(multilykedlist*列表,char*first,char*last,long num){
节点*tempf=list->headFirstName;
节点*模板=列表->地头名称;
while(tempf和templ){
如果(strcasecmp(tempf->first,first)==0&&strcasecmp(temp->last,last)==0){
printf(“错误-名称%s%s已存在\n”,第一个,最后一个);
退货清单;
}
tempf=tempf->nextFirst;
temp=temp->nextLast;
}
//分配一个新节点
Node*newNode=malloc(sizeof(Node));
newNode->first=malloc(strlen(first)+1);
strcpy(newNode->first,first);
newNode->last=malloc(strlen(last)+1);
strcpy(newNode->last,last);
newNode->number=num;
newNode->nextFirst=NULL;
newNode->nextLast=NULL;
if(list==NULL)
返回newNode;
如果(strcasecmp(第一个,列表->headFirstName->第一个)<0){
newNode->nextFirst=list->headFirstName;
返回newNode;
}
节点*prevf=list->headFirstName;
节点*currf=list->headFirstName->nextFirst;
while(currf&&stracecmp(currf->first,first)<0){
prevf=currf;
currf=currf->nextFirst;
}
prevf->nextFirst=newNode;
newNode->nextFirst=currf;
if(list==NULL)
返回newNode;
如果(STRCAECMP(最后一个,列表->地头名称->最后一个)<0){
newNode->nextLast=list->headLastName;
返回newNode;
}
节点*prevl=列表->地头名称;
节点*currl=list->headletname->nextLast;
while(currl&&stracecmp(currl->last,last)<0){
prevl=currl;
currl=currl->nextLast;
}
prevl->nextLast=newNode;
newNode->nextLast=currl;
//将此新节点添加到“byFirst”列表的开头
newNode->nextFirst=list->headFirstName;
list->headFirstName=newNode;
//list->headFirstName=addToFirstNameList(list->headFirstName,newNode);
//将此新节点添加到“byLast”列表的开头
newNode->nextLast=list->headLastName;
列表->地头名称=新节点;
//列表->海岬名称=添加海岬名称列表(列表->海岬名称,新节点);
//返回具有更新的头指针的多列表对象
退货清单;
}

我不打算为此编写完整的代码,但以下是您想要使用的算法:

对于“姓氏”列表,每次插入时,都要遍历该列表,直到找到要插入的名字后面的姓氏。将您当前的姓氏插入此姓氏之前。如果找不到,请将其插入末尾


对名字列表也要这样做。

你能确切地说明你想要完成什么吗?你想要这些节点的两个已排序的链接列表?@DanielPaczuskiBak正确,我想要一个按字母顺序链接在一起的名字和姓氏列表,这样我可以按名字或姓氏搜索,它将调出这些人的信息(姓名和号码)我更新了问题中的代码,我相信是你对道说的,你写的代码有什么问题?您是否收到错误,是否有任何示例输入/输出?我收到一条警告:return for uncompatible pointer type for return newNode。输入是add Judy Garland 2058375609,然后输出会有所不同,因为我的代码中还有其他函数应该按名字打印,该函数根据名字按字母顺序打印列表
newnode
类型为Node(您正在返回)但是*add应该返回
multilkedlist
,因此您需要更改返回类型,或者需要将newnode转换为multilkedlist。返回函数不应返回的类型时,会发生“不兼容指针类型”错误。你原来的问题已经回答了,所以如果你把我的答案标记为正确就好了。我把你的答案标记为正确。我还将返回类型更改为list,现在没有任何错误。非常感谢。