在未正确分配的函数中初始化的链接列表结构

时间:2014-10-08 19:47:37

标签: c gcc

我确定以前曾经问过这个问题,但在搜索了很长一段时间之后,我在这个网站或其他任何网站上找不到任何相关信息。

我无法获取我在函数中创建和修改的结构值。代码看起来像:

struct node {
  char name[35];
  int employeeID;
  struct node *next;
}

typedef struct node employee;

void insertInOrder(employee *head, employee *curr) {
  if (head == NULL) {
    *curr->next = *head;
    *head = *curr;
  } else {
    if ((head->employeeID<curr->employeeID)&&(curr->employeeID <head->next->employeeID)) {
      *curr->next = *head->next;
      *head->next = *curr;
    } else {
      insertInOrder(head->next, curr);
    }
  }
}

void addEmployee(char name[], employee *head, employee *curr) {
  int id;
  scanf("%d", &id);
  curr = malloc(sizeof(employee));
  strcpy(curr->name, name);
  curr->employeeID = id;
  insertInOrder(head, curr);
}

int main(void) {
  char name[35];
  int quit = 1;
  employee *head, *curr;
  head = NULL;
  printf("Enter data about the books: \n");
  while (quit) {
    scanf("%[^\n]%*c", title);
    if (title[0] != '#') {
        addBook(name, head, curr);
    } else {
        quit = 0;
    }
}

在我的调试过程中,我的代码迭代到我的所有函数中,但是一旦我在添加了我想要的所有数据后回到main,所有变量都是空的。我知道它与我使用或传递指针的方式有关,但是当我查看代码时,我不断得出合乎逻辑的结论,即我所拥有的应该做我想要的。请有人指出我的算法存在缺陷。

2 个答案:

答案 0 :(得分:1)

addBook获取类型为Book的指针,但您传递的类型为Employee

编辑:

所以,首先你不需要做*curr->next = *head之类的事情。它应该是curr->next = head。 此外,head->next可以为null,但未进行检查。最后,head需要始终指向列表的开头。

编辑2:

以下代码应该可行。 head始终指向列表的开头。为此,我们必须传递头指针的地址。我们需要这样做,因为我们将修改head的地址。

我还清理了一些东西。

void insertInOrder(employee **head, employee *curr) {

  if (*head == NULL) {
    // We are inserting the first element
    *head = curr;
  }
  else if ((*head)->next == NULL) {
    // This is the second element. We either insert it in front of head or before head.
    if ((*head)->employeeID < curr->employeeID) {
      (*head)->next = curr;
    }
    else {
      curr->next = *head;
      *head = curr;
      (*head)->next = NULL;
    }
  }

  else {
    // We iterate through the list trying to find the best spot to insert curr.
    employee *temp = *head;
    while (temp->next != NULL) {
        if ((temp->employeeID < curr->employeeID) && (curr->employeeID < temp->next->employeeID))     {
            curr->next = temp->next;
            temp->next = curr;
            break;
        }
        temp = temp->next;
    }
    // curr has the greatest id so it is inserted at the end
    if (temp->next == NULL)
      temp->next = curr;  
  }
}

void addEmployee(char name[], employee **head) {
  int id;
  printf("Enter id\n");
  scanf("%d", &id);
  employee *curr = malloc(sizeof(employee));
  strcpy(curr->name, name);
  curr->employeeID = id;
  curr->next = NULL;
  insertInOrder(head, curr);
}

int main(void) {
  int quit = 1;
  employee *head = NULL;
  char title[100];
  printf("Enter data about the employees: \n");
  while (quit) {
    scanf("%s", title);
    if (title[0] != '#') 
        addEmployee(title, &head);
    else break;
  }
  return 0;
}

答案 1 :(得分:0)

函数内部无需使用* head或* curr ..因为 - &gt;仅用于指针而是直接使用head-&gt; left&amp; curr-&gt;接着

感谢