// Добрый день.Это только часть моего исходного кода.Мое главное затруднение заключается в том, что функция print_list печатает только первый ввод пользователя в связанном списке.Я не могу точно определить проблему, поскольку логика функции кажется правильной.Функция insert_list, похоже, тоже работает нормально, но я не совсем уверен.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// это узел
typedef struct node
{
char name[61];
int month;
int day;
int year;
struct node *next;
}node;
// это список
typedef struct list
{
node *head;
node *tail;
}list;
// это создает узел, принимая входные данные пользователя и помещая их в узел
node *create_node()
{
int x;
node *data = (node*) malloc(sizeof(node));
printf("Name: ");
fgets(data->name, 61, stdin);
printf("Birthdate (mm/dd/yyyy): ");
scanf("%d%*[/]%d%*[/]%d", &data->month, &data->day, &data->year);
getchar();
if ((data->month)==0||(data->month)>=13||(data->day)<=0||(data->day)>=32||(data->year)<=1977||(data->year)>=3001)
{
while ((data->month)==0||(data->month)>=13||(data->day)<=0||(data->day)>=32||(data->year)<=1977||(data->year)>=3001)
{
printf("Invalid Input.\n");
printf("Please Enter a Valid Birthdate(mm/dd/yyyy): \n");
scanf("%d%*[/]%d%*[/]%d", &data->month, &data->day, &data->year);
getchar();
}
}
printf("******************************************************************\n");
for (x=0; x<=strlen(data->name); x++)
{
if (data->name[x]=='\n')
{
data->name[x]='\0';
}
}
printf("Birthday reminder for %s is added.\n", data->name);
return data;
}
list *create_list(list *plist)
{
plist->head = NULL;
plist->tail = NULL;
return plist;
}
// это вставляет узел в список
list *insert_list(list *plist, node *pnode, node *new_node)
{
if(plist->head==NULL)
{
plist->head=new_node;
new_node->next=NULL;
}
else
{
new_node->next = NULL;
pnode->next = new_node;
plist->tail = new_node;
}
return plist;
}
// распечатывает список
list *print_list(list *plist)
{
node *current = plist->head;
int i;
for(i=1;current!=NULL;i++)
{
printf("[%d] %s\n",i ,current->name);
printf("Birth Date: %d/%d/%d\n", current->month, current->day, current->year);
current=current->next;
}
}
// освобождает список
list *free_list(list *List)
{
node *current = List->head;
node *temp = NULL;
while(current != NULL)
{
temp = current;
current = current->next;
free(temp);
}
List->head = NULL;
List->tail = NULL;
}
// это основной
int main(void)
{
list* List = (list*) malloc(sizeof(list));
List = create_list(List);
char x;
node *data = (node *) malloc(sizeof(node));
printf("******************************************************************\n");
printf(" ADD BIRTHDAY REMINDER FORM\n");
printf("******************************************************************\n");
List = insert_list(List, data, create_node(data));
printf("Would you like to add another(y/n)?\n");
scanf("%c", &x);
if (x=='y')
{
while (x=='y')
{
if (x=='y')
{
getchar();
printf("******************************************************************\n");
node *data = (node *) malloc(sizeof(node));
List = insert_list(List, data, create_node(data));
printf("Would you like to add another(y/n)?\n");
scanf("%c", &x);
}
}
}
print_list(List);
free(List);
return 0;
}
// этот код готовдля составления