读入n值及n个整数,建立单链表并遍历输出。
输入格式:
读入n及n个整数。
输出格式:
输出n个整数,以空格分隔(最后一个数的后面没有空格)。
输入样例:
在这里给出一组输入。例如:
2
10 5
输出样例:
在这里给出相应的输出。例如:
10 5
以下是ac代码:
#include <iostream>
using namespace std;
typedef struct LNode * List;
struct LNode{
int Data;
struct LNode *Next;
};
List ReadList();
void PrintList(List L);
int main()
{
List L;
L = ReadList();
PrintList(L);
return 0;
}
List ReadList()
{
List L, head, t;
L=(List)malloc(sizeof(struct LNode));
L->Next = NULL;
head = L;
int n, e;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &e);
t = (List)malloc(sizeof(struct LNode));
t->Data = e;
t->Next = NULL;
L->Next = t;
L = L->Next;
}
L = head->Next;
free(head);
return L;
}
void PrintList(List L)
{
if(L == NULL)
{
return;
}
else{
printf("%d", L->Data);
L=L->Next;
while(L){
printf(" %d", L->Data);
L = L->Next;
}
return;
}
}