要求:输入一组题目存入链表中,(用结构体)
输入形式:数组的大小n,题目信息
输出形式:打印所有题目
测试用例:
输入:2
1 0 3+2
1 1 10+15
输出:1 0 3+2
1 0 10+15
#include <stdio.h>
#include <string.h>
struct node
{
char s[50];
struct node *pNext;
};
void main()
{
struct node *pHead = NULL, *pEnd = NULL, *pNode = NULL;
int i = 1,n;
char str[50],ch;
scanf("%d",&n);
for(i=0; i<=n; i++)
{
pNode = (struct node *)malloc(sizeof(struct node));
if(pNode != NULL)
{
gets(str);
strcpy(pNode -> s,str);
//puts(pNode -> s);
pNode -> pNext = NULL;
if(pHead == NULL)
{
pHead = pNode;
pEnd = pNode;
}
else
{
pEnd -> pNext = pNode;
pEnd = pNode;
}//end of if(pHead == NULL)
}//end of if(pNode != NULL)
}
pNode = pHead;
while(pNode != NULL)
{
printf("%s\n", pNode -> s);
pHead = pNode;
pNode = pNode -> pNext;
free(pHead);
}
printf("\n");
}