用多个文件组合并在插入时同时实现排序功能

主函数代码如下:

#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;
	}
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值