运行时检查失败 #2 - 变量“编号”周围的堆栈已损坏。我不明白错误可能发生在哪里:我正在用 C 编写一个列表。结果,指针将 NULL 返回给 print 函数,因此不会打印列表。提前致谢
#include <iostream>
#include <string.h>
struct avia
{
int number;
float time;
char destination;
avia* next;
};
void instructions();
void createlist(avia* pbegin);
void insert(avia** elem, int number, float time, char destination);
int deleteelem(avia** elem, int number);
void printlist(avia* current);
void deletelist(avia* current);
int isempty(avia* elem);
int main()
{
avia* head=NULL;
int number;
int choice;
float time;
char destination;
instructions();
scanf_s("%d", &choice);
while (choice != 6)
{
switch (choice)
{
case 1:
createlist(head);
break;
case 2:
printf("Enter a number: \n");
scanf_s("%d", &number);
printf("Enter time:\n");
scanf_s("%f", &time);
printf("Enter destination:\n");
scanf_s("%s", &destination, 30);
insert(&head, number, time, destination);
printlist(head);
break;
case 3:
if (!isempty(head))
{
printf("Enter a number you want to delete\n");
scanf_s("%d", &number);
if (deleteelem(&head, number))
{
printf("Object deleted");
printlist(head);
}
else
{
printf("Number not found");
}
}
else
{
printf("The list is empty\n");
}
break;
case 4:
if (!isempty(head))
{
deletelist(head);
}
else
{
printf("The list is empty\n");
}
break;
case 5:
if (!isempty(head))
{
printlist(head);
}
else
{
printf("The list is empty\n");
}
break;
default:
printf("Invalid choice\n");
instructions();
break;
}
printf("?\n");
scanf_s("%d", &choice);
}
printf("End of run\n");
return 0;
}
void instructions()
{
printf("Enter choice:\n1 create an empty list\n 2 insert an element into the list.\n3 delete an element from the list. \n 4 delete the whole list\n 5 print the list\n 6 end program. \n");
}
void createlist(avia* elem)
{
elem = NULL;
}
void insert(avia** elem, int number, float time, char destination)
{
avia* newelem;
newelem = (avia*)malloc(sizeof(avia));
if (newelem != NULL)
{
newelem->number = number;
newelem->time = time;
newelem->destination = destination;
newelem->next = NULL;
}
else printf("List not inserted. No memory available\n");
}
int deleteelem(avia** elem, int number)
{
avia* previous;
avia* current;
avia* temp;
if (number == (*elem)->number)
{
temp = *elem;
*elem = (*elem)->next;
free(temp);
return(number);
}
else
{
previous = *elem;
current = (*elem)->next;
while (current != NULL && current->number != number)
{
previous = current;
current = current->next;
}
if (current != NULL)
{
temp = current;
previous = current->next;
free(temp);
return(number);
}
}
}
void printlist(avia* current)
{
if (current == NULL)
{
printf("The list is empty\n");
}
else
{
printf("The list is: ");
while (current != NULL)
{
printf("%d, %f, %s ->", current->number, current->time, current->destination);
current = current->next;
}
printf("NULL");
}
}
void deletelist(avia* current)
{
avia* temp;
if (current == NULL)
{
printf("The list is empty\n");
}
else
{
while (current != NULL)
{
temp = current;
current = current->next;
free(temp);
}
}
}
int isempty(avia* elem)
{
return elem == NULL;
}
1 个回答