头插法建立单链表
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#define OVERFLOW 0
#define OK 1
typedef struct student
{
int num;
struct student *next;
}node,*slist;
slist head_insert(int n)
{
slist h = (slist)malloc(sizeof(node));
h->next = NULL;
slist s,p;
s = NULL;
int a;
printf("Please input integers:\n");
while( n-- )
{
p = (slist)malloc(sizeof(node));
scanf("%d", &a);
p->num = a;
p->next = s;
s = p;
h->next = s;
}
return h;
}
int main()
{
int n;
printf("input the counts:\n");
scanf("%d", &n);
slist hh;
hh = head_insert(n)->next;
while(hh != NULL)
{
printf("%d ", hh->num);
hh = hh->next;
}
return 0;
}