我编写的程序几乎没有问题。 其中之一是,当我输入信息时可以,但是当我想显示数据时,char字段有错误,源字段和目标字段显示为“空”。它应该类似于1234:从x到y。但是相反,它就像1234:从null到null。如何解决?我也有删除某些元素的问题。当我在列表中添加2条日志并尝试删除它们时,它告诉我没有这样的数字。如何解决这两个问题?
#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
#pragma warning(disable:4996)
struct flight {
int value;
char source;
char destination;
struct flight* next;
};
void insert();
void display();
void delete();
int count();
typedef struct flight DATA_NODE;
DATA_NODE* head_node, * first_node, * temp_node = 0, * prev_node, next_node;
int data;
char data2, data3;
struct flight f[];
int main()
{
int decision = 0;
printf("Welcome to flight log editor.\n");
while (decision < 5)
{
printf("\nOptions\n");
printf("1: Add a flight \n");
printf("2: Delete a flight\n");
printf("3: Sort flights\n");
printf("4: Display flights\n");
printf("5: Exit\n");
printf("Enter your option: ");
scanf("%d", &decision);
switch (decision) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
display();
break;
case 5:
return 0;
}
}
return 0;
}
void insert()
{
printf("\nEnter flight number: \n");
scanf("%d", &data);
printf("\nEnter flight source: \n");
scanf("%s", &data2);
printf("\nEnter flight destination: \n");
scanf("%s", &data3);
temp_node = (DATA_NODE*)malloc(sizeof(DATA_NODE));
temp_node->value = data;
temp_node->source = data2;
temp_node->destination = data3;
if (first_node == 0)
{
first_node = temp_node;
}
else
{
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete()
{
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nEnter flight number to delete: \n");
scanf("%d", &pos);
if (pos > 0 && pos <= countvalue) {
if (pos == 1) {
temp_node = temp_node->next;
first_node = temp_node;
printf("\nFlight log deleted.\n");
}
else {
while (temp_node != 0) {
if (i == (pos - 1)) {
prev_node->next = temp_node->next;
if (i == (countvalue - 1))
{
head_node = prev_node;
}
printf("\nFlight deleted successfully.\n");
break;
}
else {
i++;
prev_node = temp_node;
temp_node = temp_node->next;
}
}
}
}
else
printf("\nInvalid flight number.\n");
}
void display()
{
int count = 0;
temp_node = first_node;
printf("\nDisplaying flight list: \n");
while (temp_node != 0)
{
printf(" %d:", temp_node->value);
printf(" from %s ", temp_node->source[temp_node]);
printf(" to %s ", temp_node->destination[temp_node]);
count++;
temp_node = temp_node->next;
}
}
int count()
{
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node->next;
}
return count;
}