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

标签 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 后,所有变量都是空的。我知道这与我使用或传递指针的方式有关,但是当我查看代码时,我不断得出逻辑结论:我所拥有的应该做我想做的事情。请有人指出我的算法有缺陷。

最佳答案

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

关于c - 在未正确分配的函数中初始化的链表结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26265182/

相关文章:

c - Clock_t 和 time_t 或 time(NULL) 和 Clock() 之间的区别

c - 如何在 linux 用户空间应用程序中获得最精确的时钟?

c - C 中 while 循环中的++i 和 i++

在 minGW-W64 g++ 中编译的 C++ 代码不能用 Ubuntu g++ 编译

linux - 如何使用 cloog 部署可移植 gcc?

c - OpenMP Parallel for 循环显示性能提升很小

c - 我应该使用什么数据结构来计算超过 50 位数字的阶乘?

c++ - 如何使用 fork() 创建进程链?

我可以使用 gcc -march 编译成其他 ISA

c - 它在 GCC 源代码的哪个位置编译成不同的汇编语言?