C<stdlib.h>
C++< cstdlib>
【向前插入法构造链表】
#include <stdio.h>
#include <stdlib.h>
typedef struct linkednode
{
int data;
struct linkednode *next;
}snode,*ptr;
ptr creatlinkA() // 向前插入法
{
ptr head,p;
head = NULL;
int x;
scanf("%d",&x);
while(x!=1000)
{
p = (ptr)malloc(sizeof(snode)); // 创建节点
p->data = x; // 节点数据
p->next = head; // 插入结点
head = p;
scanf("%d",&x);
}
return (head);
}
void main()
{
ptr ha,hb,hc;
ha = creatlinkA();
printf("%d ",ha->data);
hc = ha;
while(hc!=NULL) // 输出结点 检验
{
printf("%d ",hc->data);
hc = hc->next;
}
}