C语言代码:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
/**尾插法,顺序输入输出**/
struct node *create(int n)
{
struct node *head,*p,*q;//q用来指向新生成的节点。p始终指向L的终端节点。
int i;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
p=head;//p指向了头节点,此时的头节点是终端节点。
printf("输入%d个整数:",n);
for(i=0;i<n;i++)
{
q=(struct node *)malloc(sizeof(struct node));//q指向新申请的节点
scanf("%d",&q->data);
p->next=q;//用p来接纳新节点
p=q;//p指向终端节点