测试环境VC6.0
#include<stdlib.h>
#include<stdio.h>
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct NODE{ //定义
int a;
struct NODE*next; //这里struct也应该添加.
}Node;
void InitList(Node*la){//头节点创建
la=(Node*)malloc(sizeof(Node));
la->next=NULL;
}
Status ListInsert(Node*a,int i,int e)
{//在i-1,i中插入元素,就是在第I个位置插入。
Node*p,*c;
int j=0;
p=a;
c=NULL;
while(j<i) {p=p->next;j++;} //p与p->next都为指针变量,;不能忘记//
if(j<0||j>i) return ERROR ; //一次执行完的判断
c=(Node*)malloc(sizeof(Node));
c->a=e;
c->next=p->next; //空指针域的继承与修改
p->next=c;
return OK;
}
Status TraverseList(Node a)
{//遍历链表并且输出每个节点
Node*p;
p=a.next;
printf("遍历结果是\n");
while(p!=NULL)
{
printf("%d",p->a);
p=p->next;
}
return OK;
}
void main()
{
Node a;
int b,e,j;
j=1;
InitList(&a);
printf("请插入个数");
scanf("%d",&b);
printf("请输入");
for(;j<=b;j++)
{ scanf("%d",&e);
ListInsert(&a,j,e);
}
TraverseList(a);
}