主函数代码如下:
#include <stdio.h>
#include <time.h>
#include "SequenceList.h"
void print(Element s)
{
printf("%d ", s);
}
int main()
{
SLT t;
int i, ret, p, temp;
srand(time(NULL));
ret = SequenceInit(&t);
if(ret == Success)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
for(i = 0; i < 8; i++)
{
ElementLocation(t, temp = rand()%10, &p);
printf("%d ",temp);
ret = SequenceInsert(&t, p, temp);
if(ret == Success)
{
printf("Insert Success!\n");
}
else
{
printf("Insert failure!\n");
}
}
ret = ListCheck(t,print);
if(ret == Success)
{
putchar('\n');
printf("Check success!\n");
}
else
{
printf("Check failure!\n");
}
return 0;
}
头文件如下:
#ifndef _SEQUENCE_h
#define _SEQUENCE_h
#define SIZE 10
#define Success 10001
#define Failure 10000
typedef int Element;
struct SequenceList
{
int Length;
Element *List;
};
typedef struct SequenceList SLT;
int SequenceInit(SLT *p);
int SequenceInsert(SLT *t, int p, Element s);
int ListLength(SLT t);
int ListEmpty(SLT t);
int ListFind(SLT t, int p, Element *e);
int ElementLocation(SLT t, Element s, int *p);
int ListCheck(SLT t, void (*p)(Element));
int ElementDelete(SLT *t, int p, Element *e, void (*print)(Element));
int ListClear(SLT *t);
int Listdestroy(SLT *t);
int Equal(Element x, Element y);
#endif
调用函数文件如下:
#include <stdlib.h>
#include "SequenceList.h"
int Greater(Element x, Element y)
{
return (x >= y) ? 1 : 0;
}
int Littler(Element x, Element y)
{
return (x < y) ? 1 : 0;
}
int Equal(Element x, Element y)
{
return (x == y) ? 1 : 0;
}
int SequenceInit(SLT *p)
{
if(p == NULL)
{
return Failure;
}
p->Length = 0;
p->List = (Element *)malloc(sizeof(Element)*SIZE);
return Success;
}
int SequenceInsert(SLT *t, int p, Element s)
{
int i;
if(t == NULL||p < 1||p > t->Length+1||t->Length >= SIZE)
{
return Failure;
}
for(i = 0; i < t->Length-p+1; i++)
{
t->List[t->Length-i] = t->List[t->Length-i-1];
}
t->Length++;
t->List[p-1] = s;
return Success;
}
int ListLength(SLT t)
{
return t.Length;
}
int ListEmpty(SLT t)
{
return (t.Length == 0) ? Success : Failure;
}
int ListFind(SLT t, int p, Element *e)
{
if(p < 1||p > t.Length)
{
return Failure;
}
else
{
*e = t.List[p-1];
return Success;
}
}
int ListCheck(SLT t, void (*p)(Element))
{
int i;
if(t.Length < 1||t.List == NULL)
{
return Failure;
}
else
{
for(i = 0; i < t.Length; i++)
{
p(t.List[i]);
}
return Success;
}
}
int ElementLocation(SLT t, Element s, int *p)
{
int i, count = 0;
if(t.Length == 0)
{
*p = 1;
}
else
{
for(i = 0; i < t.Length; i++)
{
/* if(t.List[i] == s)*/
if(Greater(s,t.List[i]))
{
*p = i+1;
break;
}
else
{
count++;
}
}
if(count == t.Length)
{
*p = t.Length+1;
}
}
/* if(count == 0)
{
return Failure;
}*/
}
int ElementDelete(SLT *t, int p, Element *e, void (*print)(Element))
{
int i;
if(t == NULL)
{
return Failure;
}
*e = t->List[p-1];
for(i = 0; i < t->Length-p; i++)
{
t->List[p-1+i] = t->List[p+i];
}
t->Length--;
for(i = 0; i < t->Length; i++)
{
print(t->List[i]);
}
return Success;
}
int ListClear(SLT *t)
{
if(t == NULL)
{
return Failure;
}
else
{
t->Length = 0;
return Success;
}
}
int ListDestroy(SLT *t)
{
if(t == NULL)
{
return Failure;
}
else
{
t->Length = 0;
free(t->List);
t->List = NULL;
return Success;
}
}