1.编写一个链表的构建函数输入任意多条的长度不等的字符串,用链表和动态
申请空间方式保存各个字符串,结束的条件是直接回车
2.编写一个显示函数,调用即显示前面所有输入过的字符串
VS2019这两题怎么编写,求求大神们帮帮忙
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char *string;
struct node *next;
};
struct node *create()
{
char str[256];
struct node *head = NULL;
struct node *tail = NULL;
while (scanf("%s", str) == 1)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->string = (char *)malloc(strlen(str) + 1);
strcpy(p->string, str);
p->next = NULL;
if (!head)
head = p;
if (tail)
tail->next = p;
tail = p;
if (getchar() == '\n')
break;
}
return head;
}
void destroy(struct node *head)
{
struct node *p = head;
while (p)
{
struct node *q = p;
p = p->next;
free(q->string);
free(q);
}
}
void print(struct node *head)
{
struct node *p = head;
while (p)
{
printf("%s\n", p->string);
p = p->next;
}
}
int main()
{
struct node *head = create();
print(head);
destroy(head);
}