#ifndef _INSERT_H
#define _INSERT_H
#define SUCCESS 100001
#define FAILURE 100002
#define TRUE 100003
#define FALSE 100004
typedef int ElemType;
struct node
{
ElemType data;
struct node *next;
};
typedef struct node Node;
typedef Node *LinkList;
int ListInit(LinkList *L);
int ListInsert(LinkList *L,int
i,ElemType e);
int ListTraverse(LinkList L,void
(*print)(ElemType));
#endif
接口
include <stdio.h>
#include <stdlib.h>
#include "Insert.h"
int ListInit(LinkList *L)
{
(*L)=(LinkList)malloc(sizeof
(Node));
if(NULL==(*L))
{
return FAILURE;
}
(*L)->next=NULL;
return SUCCESS;
}
int ListInsert(LinkList *L,int i,
ElemType e)
{
LinkList p=*L;
int j=1;
while(p!=NULL&&j<i)
{
p=p->next;
j++;
}
if(NULL==p|j>i)
{
return FAILURE;
}
LinkList n=(LinkList)malloc(sizeof
(Node));
if(NULL==n)
{
return FAILURE;
}
n->data=e;
n->next=p->next;
p->next=n;
return SUCCESS;
}
int ListTraverse(LinkList L,void
(*print)(ElemType))
{
if(NULL==L)
{
return FAILURE;
}
LinkList p=L->next;
while(p)
{
print(p->data);
p=p->next;
}
return SUCCESS;
}
main函数文件
include <stdio.h>
#include <stdlib.h>
#include "Insert.h"
void visit(ElemType e)
{
printf("%d ",e);
}
int main()
{
int ret,i;
LinkList list;
ret=ListInit(&list);
if(FAILURE==ret)
{
printf("初始化失败!\n");
}
else
{
printf("初始化成功\n");
}
for(i=0;i<9;i++)
{
ret=ListInsert(&list,i+1,i+1);
if(FAILURE==ret)
{
printf("插入%d 失败\n",i+1);
}
else
{
printf("插入%d 成功\n",i+1);
}
}
ret=ListTraverse(list,visit);
if(FAILURE==ret)
{
printf("遍历失败\n");
}
else
{
printf("遍历成功\n");
}
return 0;
}
#define _INSERT_H
#define SUCCESS 100001
#define FAILURE 100002
#define TRUE 100003
#define FALSE 100004
typedef int ElemType;
struct node
{
ElemType data;
struct node *next;
};
typedef struct node Node;
typedef Node *LinkList;
int ListInit(LinkList *L);
int ListInsert(LinkList *L,int
i,ElemType e);
int ListTraverse(LinkList L,void
(*print)(ElemType));
#endif
接口
include <stdio.h>
#include <stdlib.h>
#include "Insert.h"
int ListInit(LinkList *L)
{
(*L)=(LinkList)malloc(sizeof
(Node));
if(NULL==(*L))
{
return FAILURE;
}
(*L)->next=NULL;
return SUCCESS;
}
int ListInsert(LinkList *L,int i,
ElemType e)
{
LinkList p=*L;
int j=1;
while(p!=NULL&&j<i)
{
p=p->next;
j++;
}
if(NULL==p|j>i)
{
return FAILURE;
}
LinkList n=(LinkList)malloc(sizeof
(Node));
if(NULL==n)
{
return FAILURE;
}
n->data=e;
n->next=p->next;
p->next=n;
return SUCCESS;
}
int ListTraverse(LinkList L,void
(*print)(ElemType))
{
if(NULL==L)
{
return FAILURE;
}
LinkList p=L->next;
while(p)
{
print(p->data);
p=p->next;
}
return SUCCESS;
}
main函数文件
include <stdio.h>
#include <stdlib.h>
#include "Insert.h"
void visit(ElemType e)
{
printf("%d ",e);
}
int main()
{
int ret,i;
LinkList list;
ret=ListInit(&list);
if(FAILURE==ret)
{
printf("初始化失败!\n");
}
else
{
printf("初始化成功\n");
}
for(i=0;i<9;i++)
{
ret=ListInsert(&list,i+1,i+1);
if(FAILURE==ret)
{
printf("插入%d 失败\n",i+1);
}
else
{
printf("插入%d 成功\n",i+1);
}
}
ret=ListTraverse(list,visit);
if(FAILURE==ret)
{
printf("遍历失败\n");
}
else
{
printf("遍历成功\n");
}
return 0;
}