C 未正确分配函数中初始化的链表结构
我肯定以前有人问过这个问题,但在搜索了一段时间后,我在这个网站或其他网站上找不到任何关于它的信息 我在获取我在函数中创建和修改的结构的值时遇到问题。代码如下所示:C 未正确分配函数中初始化的链表结构,c,gcc,C,Gcc,我肯定以前有人问过这个问题,但在搜索了一段时间后,我在这个网站或其他网站上找不到任何关于它的信息 我在获取我在函数中创建和修改的结构的值时遇到问题。代码如下所示: struct node { char name[35]; int employeeID; struct node *next; } typedef struct node employee; void insertInOrder(employee *head, employee *curr) { if (head
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;
}
}
struct节点{
字符名[35];
国际雇员ID;
结构节点*下一步;
}
typedef结构节点employee;
无效插入器(员工*主管,员工*当前){
if(head==NULL){
*当前->下一步=*头;
*head=*货币;
}否则{
if((head->employeeideemployeeid)&&(curr->employeeID next->employeeID)){
*当前->下一步=*头部->下一步;
*head->next=*当前;
}否则{
插入器(头部->下一步,当前);
}
}
}
void addEmployee(字符名[],员工*主管,员工*当前){
int-id;
scanf(“%d”和&id);
curr=malloc(sizeof(雇员));
strcpy(curr->name,name);
当前->员工id=id;
插入器(头部,当前);
}
内部主(空){
字符名[35];
int-quit=1;
员工*主管,*当前;
head=NULL;
printf(“输入有关书籍的数据:\n”);
while(退出){
scanf(“%[^\n]%*c”,标题);
如果(标题[0]!='#'){
地址簿(名称、标题、货币);
}否则{
退出=0;
}
}
在调试过程中,我的代码会迭代到所有函数中,但在添加所有需要的数据后回到main后,所有变量都是空的。我知道这与我使用或传递指针的方式有关,但当我查看代码时,我会不断得出逻辑结论,即我所拥有的应该做我想要的事情。Ple例如,有人指出我的算法哪里有缺陷。在函数内部不需要使用*head或*curr
谢谢
addBook
接受Book
类型的指针,但您正在传递Employee
类型的指针
编辑:
因此,首先你不需要做像*curr->next=*head
这样的事情。它应该是curr->next=head
。
另外,head->next
可以为空,但不进行检查。最后,head
需要始终指向列表的开头
编辑2:
下面的代码应该可以工作。head
始终指向列表的开头。为此,我们必须传递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;
}
void insertinoder(员工**负责人,员工*当前){
如果(*head==NULL){
//我们正在插入第一个元素
*水头=电流;
}
else if((*head)->next==NULL){
//这是第二个元素。我们可以将其插入头部前面或头部前面。
如果((*head)->employeeIDemployeeID){
(*head)->next=curr;
}
否则{
当前->下一步=*头;
*水头=电流;
(*head)->next=NULL;
}
}
否则{
//我们反复浏览列表,试图找到插入curr的最佳位置。
员工*临时工=*负责人;
while(临时->下一步!=NULL){
如果((临时->员工IDemployeeID)&&(当前->员工IDnext->employeeID)){
当前->下一步=临时->下一步;
温度->下一步=当前;
打破
}
温度=温度->下一步;
}
//curr具有最大的id,因此在末尾插入
如果(临时->下一步==NULL)
温度->下一步=当前;
}
}
void addEmployee(字符名[],员工**负责人){
int-id;
printf(“输入id\n”);
scanf(“%d”和&id);
员工*curr=malloc(sizeof(员工));
strcpy(curr->name,name);
当前->员工id=id;
当前->下一步=空;
插入器(头部,当前);
}
内部主(空){
int-quit=1;
员工*head=NULL;
字符标题[100];
printf(“输入有关员工的数据:\n”);
while(退出){
scanf(“%s”,标题);
如果(标题[0]!='#')
增聘员工(职称和负责人);
否则就断了;
}
返回0;
}
这是旧代码的产物,谢谢你指出。我删减了实际代码,以使其更简单,更易于阅读。抱歉,这是我尝试不同的可能性后留下的。我已将代码更改为实际使用的代码。