下面的代码中包括获取某一结点的数据和增删操作
#include<stdio.h>
#include<stdlib.h>
typedef int status;
typedef int ElemType;
#define ERROR 0;
#define OK 1;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node;
typedef struct Node *LinkList;
status GetElem(LinkList L,int i ,ElemType * e)
{
int j;
LinkList p ;
p=L->next;
j=1;
while (p&&j<i)
{
p=p->next;
++i;
}
if(!p||j>i)
return ERROR;
*e = p->data;
return OK;
}
status ListInsert(LinkList * L, int i ,ElemType e )
{
int j ;
LinkList p,s;
p=*L;
j=1;
while(p&&j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
{
return ERROR;
}
s= (LinkList)malloc(sizeof(Node));
s->data=e;
s->next = p->next;
p->next = s;
return OK;
}
status ListDelete (LinkList *L,int i,ElemType * e)
{
int j;
LinkList p,q;
p=*L;
j=1;
while(p->next&&j<i)
{
p=p->next;
++j;
}
if(!(p->next)||j>i)
{
return ERROR;
}
q=p->next;
p->next =q->next;
*e = q->data;
free(q);
return OK;
}
网友评论