将头文件,调用函数与主函数分开写

头文件如下:

#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, void (*print)(Element));
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 
#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, void (*print)(Element))
{
	int i, count = 0;
	for(i = 0; i < t.Length; i++)
	{
	/*	if(t.List[i] == s)*/
		if(Equal(s,t.List[i]))
		{
			print(i+1);
			count++;
		}
	}
	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;
	}
}

主函数:

#include 
#include 
#include "SequenceList.h"

void print(Element s)
{
	printf("%d ", s);
}
int main()
{
	SLT t;
	int i, ret;
	srand(time(NULL));

	ret = SequenceInit(&t);

	if(ret == Success)
	{
		printf("Init success!\n");
	}
	else if(ret == Failure)
	{
		printf("Init failure!\n");
	}

	for(i = 0; i < 5; i++)
	{
		ret = SequenceInsert(&t,i+1,rand()%10);
		if(ret == Success)
		{
			printf("Insert success!\n");
		}
		else
		{
			printf("Insert failure!\n");
		}
	}

	ret = ListLength(t);
	printf("Length = %d\n", ret);

	ret = ListEmpty(t);
	if(ret == Success)
	{
		printf("Is empty!\n");
	}
	else
	{
		printf("Is not empty!\n");
	}

	int p = 3;
	Element e;
	ret = ListFind(t, p, &e);
	if(ret == Success)
	{
		printf("%dTh is %d\n", p, e);
	}
	else
	{
		printf("Find failure!\n");
	}

	ListCheck(t, print);
	putchar('\n');

	Element s = 3;
	int q;
	ret = ElementLocation(t, s, print);
	if(ret == Failure)
	{
		printf("Locat Failure!\n");
	}
	else
	{
		putchar('\n');
	}

	int j = 3;
	Element r;
	ret = ElementDelete(&t, j, &r, print);
	if(ret == Success)
	{
		putchar('\n');
		printf("%dth is delete success!\n");
	}
	else
	{
		printf("%dth is delete failure!\n");
	}

	ret = ListClear(&t);
	if(ret == Success)
	{
		printf("ListClear Success!\n");
	}
	else
	{
		printf("ListClear Failure!\n");
	}

	ret = ListDestroy(&t);
	if(ret == Success)
	{
		printf("ListDestory Success!\n");
	}
	else
	{
		printf("ListDestory Failure!\n");
	}
	ListCheck(t,print);

	return 0;
}

一起编译执行便可实现功能。 

 

 

 

你可能感兴趣的:(将头文件,调用函数与主函数分开写)