我正在处理 C 中的 List 示例,其中新节点被推到堆栈的末尾。Bus Error: 10
当我尝试将新节点推到最后时,我一直得到一个。这是我的推送功能:
void push(struct node *tail, struct node *newNode) {
tail->next = newNode; // gdb says the problem is here
tail = tail->next;
}
我称之为使用push(tail, newNode);
如有必要,这也是我的结构:
struct node
{
int hour;
int minute;
char *name;
struct node *next;
};
这是显示导致代码的主要功能push()
int main()
{
char inputString[50];
int timeHour, timeMin;
struct node *head;
struct node *tail;
while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF) {
scanf("%s", inputString);
if (strcmp(inputString, "enqueue") == 0) {
if (head == NULL) {
head = malloc(sizeof(struct node));
head->hour = timeHour;
head->minute = timeMin;
// get name
scanf("%s", inputString);
head->name = malloc(strlen(inputString)+1);
strcpy(head->name, inputString);
tail = head;
printEnqueue(head);
} else {
struct node *newEntry = malloc(sizeof(struct node));
newEntry->hour = timeHour;
newEntry->minute = timeMin;
// get name
scanf("%s", inputString);
newEntry->name = malloc(strlen(inputString)+1);
strcpy(newEntry->name, inputString);
push(tail, newEntry);
printEnqueue(newEntry);
}
} else {
pop(&head, timeHour, timeMin);
}
}
return 0;
}