将名称添加到多链表中
Add names into a Multi-linked list
我正在创建一个包含名字、姓氏和 phone 号码的多 link 列表。我有一个为列表分配内存的添加函数,但我不知道如何将 link 名字和姓氏按字母顺序添加到这个列表中。如何添加到多 linked 列表?它与添加到单个 linked 列表有很大不同吗?
结构:
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;
}
编辑:我在这里添加了我的添加功能的更新代码,我修改了添加到我用于单个 linked 列表
的列表
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;
}
我不会为此写出完整的代码,但这是您要使用的算法:
对于姓氏列表,每次插入时,循环访问列表,直到找到位于您要插入的姓名之后的姓氏。在此之前插入您当前的姓氏。如果找不到,请在末尾插入。
对名字列表执行相同的操作。
我正在创建一个包含名字、姓氏和 phone 号码的多 link 列表。我有一个为列表分配内存的添加函数,但我不知道如何将 link 名字和姓氏按字母顺序添加到这个列表中。如何添加到多 linked 列表?它与添加到单个 linked 列表有很大不同吗?
结构:
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;
}
编辑:我在这里添加了我的添加功能的更新代码,我修改了添加到我用于单个 linked 列表
的列表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;
}
我不会为此写出完整的代码,但这是您要使用的算法:
对于姓氏列表,每次插入时,循环访问列表,直到找到位于您要插入的姓名之后的姓氏。在此之前插入您当前的姓氏。如果找不到,请在末尾插入。
对名字列表执行相同的操作。