如何对线性表中下表为i的(第i+1个)元素的前驱和后继的算法?
#include "stdafx.h"#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
int i;
L.elem = (ElemType * )malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (! L.elem) exit (OVERFLOW);
for(i=1;i<=5;i++)
{
scanf("%d",&L.elem[i-1]);
}
L.length = 5;
L.listsize = LIST_INIT_SIZE;
return OK;
}
Status PrintList_Sq(SqList L)
{
int i;
printf("the list is:\n ");
for (i=1;i<=L.length;i++)
{
printf("%4d",L.elem[i-1]);
}
printf("\n");
return OK;
}
Status LocateElem_Sq(SqList L,ElemType e,Status(*compare)(ElemType,ElemType))//如何该如何查找前驱和后继?
{
int i;
int *p;
i=1;
p=L.elem;
while(i<=L.length&&!(*compare)(*p++,e))
{
++i;
}
if(i<L.length)
return i;
else
return 0;
return OK;
}
int main(int argc, char* argv[])
{
SqList L;
ElemType e;
int select,i;
InitList_Sq(L);
PrintList_Sq(L);
return 0;
}