Я новичок в программировании на Си. У меня была задача создать список студентов, используя двойной связанный список. Приложение должно иметь три пункта: отобразить список, добавить нового студента и удалить студента по его идентификационному номеру.
Я сделал это, и это работает очень хорошо.
Я хотел бы задать несколько вопросов:
- Что-то используется неуместно?
- Если есть место для сокращения кода, я был бы рад получить любые предложения.
Вот мой код:
struct student
{
char first_name[20];
char last_name[20];
char ID_NO[14];
struct student* previous;
struct student* next;
};
void Menu();
void Show_List(struct student*);
struct student* Add_Student(char [], char [], char [], struct student*);
struct student* Erase_Student(char [], struct student*);
void main(void)
{
char student_first_name[20];
char student_last_name[20];
char personal_code[14];
struct student* first = NULL;
struct student* node0 = NULL;
int x = 0;
int opt;
Menu();
for(; ;)
{
printf("\nEnter the operation you want to do: \n");
scanf("%d", &opt);
switch(opt)
{
case 1:
Show_List(first);
break;
case 2:
printf("\nEnter the student's first name: ");
scanf("%s", &student_first_name);
printf("\nEnter the student's last name: ");
scanf("%s", &student_last_name);
printf("\nEnter the student's personal code: ");
scanf("%s", &personal_code);
node0 = Add_Student(student_first_name, student_last_name, personal_code, first);
first = node0;
break;
case 3:
printf("\nEnter the code of the student you want to erase: ");
scanf("%s", &personal_code);
node0 = Erase_Student(personal_code, first);
first = node0;
break;
default:
printf("\nYou entered an invalid option!!! Please try again.\n");
Menu();
break;
}
}
scanf("%d", &x);
}
void Menu()
{
printf("\nSelect from the Menu the operation you want to execute:\n");
printf("\n1) Show students list;");
printf("\n2) Add a student in the list;");
printf("\n3) Erase a student from the list.");
}
void Show_List(struct student* firstNode)
{
struct student* firstNodeCopy = firstNode;
int number = 0;
if(firstNode == NULL)
printf("\nThe list is empty.\n");
while(firstNodeCopy)
{
printf("\n%d. %s ", ++number, firstNodeCopy->first_name);
printf("%s %s\n", firstNodeCopy->last_name, firstNodeCopy->ID_NO);
firstNodeCopy = firstNodeCopy->next;
}
}
struct student* Add_Student(char name_1[], char name_2[], char ID[], struct student* firstNode)
{
struct student* start = firstNode;
struct student* last = NULL;
struct student* addNode = (struct student*) malloc(sizeof(struct student));
if(firstNode == NULL)
{
firstNode = (struct student*) malloc(sizeof(struct student));
strcpy(firstNode->first_name, name_1);
strcpy(firstNode->last_name, name_2);
strcpy(firstNode->ID_NO, ID);
firstNode->next = NULL;
firstNode->previous = NULL;
return firstNode;
}
else
{
strcpy(addNode->first_name, name_1);
strcpy(addNode->last_name, name_2);
strcpy(addNode->ID_NO, ID);
while(start)
{
if(strcmp(addNode->first_name, start->first_name) > 0)
{
if(start->next == NULL)
{
start->next = addNode;
addNode->previous = start;
addNode->next = NULL;
return firstNode;
}
else
{
last = start;
start = start->next;
}
}
if(strcmp(addNode->first_name, start->first_name) < 0)
{
if(last == NULL)
{
addNode->next = start;
start->previous = addNode;
return addNode;
}
else
{
addNode->next = start;
addNode->previous = last;
last->next = addNode;
start->previous = addNode;
return firstNode;
}
}
if(strcmp(addNode->first_name, start->first_name) == 0)
{
if(strcmp(addNode->last_name, start->last_name) < 0)
{
if(last == NULL)
{
addNode->next = start;
start->previous = addNode;
return addNode;
}
else
{
addNode->next = start;
addNode->previous = last;
last->next = addNode;
start->previous = addNode;
return firstNode;
}
}
if(strcmp(addNode->last_name, start->last_name) > 0)
{
if(start->next == NULL)
{
start->next = addNode;
addNode->previous = start;
addNode->next = NULL;
return firstNode;
}
else
{
last = start;
start = start->next;
}
}
if(strcmp(addNode->last_name, start->last_name) == 0)
{
if(last == NULL)
{
addNode->next = start;
start->previous = addNode;
return addNode;
}
else
{
addNode->next = start;
addNode->previous = last;
last->next = addNode;
start->previous = addNode;
return firstNode;
}
}
}
}
}
}
struct student* Erase_Student(char ID[], struct student* firstNode)
{
struct student* start = firstNode;
struct student* last = NULL;
if(start == NULL)
{
printf("\nThe list is empty.\n");
return NULL;
}
else
{
while(start)
{
if(strcmp(ID, start->ID_NO) == 0)
{
if(last == NULL)
{
start = start->next;
return start;
}
else
{
last->next = start->next;
return firstNode;
}
}
else
{
last = start;
start = start->next;
}
}
printf("\nYou entered a WRONG personal ID number to erase!!! Please try again.\n");
return firstNode;
}
}