Add names into a Multi-linked list

时间:2018-04-20 00:54:44

标签: c struct doubly-linked-list

I am creating a multi-linked list of first name, last name, and phone number. I have an add function that allocates memory for the list but I don't know how to add into this list that will link first and last name in alphabetical order. How do I add to a multi-linked list? Is it much different from adding into a single linked list?

structures:

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

typedef struct mlist {
    Node *headFirstName;
    Node *headLastName;
} MultiLinkedList;

add function:

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;
}

Edit: Here I have added in an updated code of my add function that I modified with adding to a list that I have used for a single linked 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;
}

1 个答案:

答案 0 :(得分:1)

I'm not going to write out the entire code for this, but here's the algorithm you want to use:

For the last names list, every time you insert, iterate through the list until you reach a last name that comes after the name you're trying to insert. Insert your current last name before this one. If you don't find one, insert it at the end.

Do the same for the first names list.