题目
实现单链表的插入操作从键盘输入10个整数,用单链表存储这个数据。每次将数据插入链表的头部。
并在输入完毕后,将这10个数据输出.
#Examples
input
10
0 1 2 3 4 5 6 7 8 9
output
0 1 2 3 4 5 6 7 8 9
Source Program
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* next;
}LNode;
void build_head(LNode* h);
void show(LNode* h);
int main()
{
LNode* head;
head = (LNode*)malloc(sizeof(LNode));
build_head(head);
show(head);
return 0;
}
void build_head(LNode* h)
{
LNode* p = h, * q;
q = (LNode*)malloc(sizeof(LNode));
p->next = q;
p = q;
int n;
printf("请输入数据的个数:");
scanf("%d", &n);
printf("请输入数组中的数:\n");
for (int i = 0; i < n; ++i)
{
scanf("%d", &p->data);
q = (LNode*)malloc(sizeof(LNode));
p->next = q;
p = q;
}
p->next = NULL;
}
void show(LNode* h)
{
h = h->next;
while (h->next != NULL)
{
printf("%d ", h->data);
h = h->next;
}
puts("");
}