线性表的顺序存储结构

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE 100
#define OK 1
#define ERROR 0
#define	TRUE 1
#define FALSE 0
#define OVERFLOW -2

typedef int Status;

typedef struct {
	double a;
	int b;
}ElemType;

typedef struct List {
	ElemType* elem = NULL;
	int length;
}SqList;

void visit(ElemType e);
int compare(ElemType& a, ElemType& b);
int compare_size(ElemType a, ElemType b);

Status InitList(SqList& L);										//初始化线性表
void DestroyList(SqList& L);									//销毁线性表
void ClearList(SqList& L);										//清空线性表
Status ListEmpty(SqList L);										//判断线性表是否为空
Status ListLength(SqList L);									//返回线性表中元素的个数
Status GetElem(SqList L, int i, ElemType& e);					//返回线性表中第i个元素的值
Status LocateElem(SqList L, ElemType e);						//判断线性表中是否存在元素e
Status PriorElem(SqList L, ElemType cur_e, ElemType& pre_e);	//返回当前元素的直接前驱元素
Status NextElem(SqList L, ElemType cur_e, ElemType& next_e);	//返回当前元素的直接后继元素
Status ListInsert(SqList& L, int i, ElemType e);				//将元素e插入线性表第i个位置
Status ListDelete(SqList& L, int i, ElemType& e);				//将第i个位置的元素删除,并用e返回
void ListTraverse(SqList L);									//依次访问线性表中的每一个元素
void Union(SqList& La, SqList Lb);								//线性表的合并
void MergeList(SqList La, SqList Lb, SqList& Lc);				//有序表的合并


int main()
{
	SqList La, Lb, Lc;
	InitList(La);
	InitList(Lb);

	int sum_a, sum_b;
	ElemType elem_a, elem_b;

	scanf("%d", &sum_a);
	for (int i = 0; i < sum_a; i++)
	{
		scanf("%lf%d", &elem_a.a, &elem_a.b);
		ListInsert(La, i + 1, elem_a);
	}
	ListTraverse(La);

	printf( "---------------------------------------\n" );


	scanf("%d", &sum_b);
	for (int i = 0; i < sum_b; i++)
	{
		scanf("%lf%d", &elem_b.a, &elem_b.b);
		ListInsert(Lb, i + 1, elem_b);
	}
	ListTraverse(Lb);

	printf("---------------------------------------\n");

	/*Union(La, Lb);*/
	MergeList(La, Lb, Lc);

	ListTraverse(Lc);

	/*SqList L;
	if (InitList(L))
		printf("初始化线性表成功!\n");
	else
		printf("初始化线性表失败!\n");

	int sum;
	ElemType elem;

	scanf("%d", &sum);
	for (int i = 0; i < sum; i++)
	{
		scanf("%lf%d", &elem.a, &elem.b);
		ListInsert(L, i + 1, elem);
	}

	ElemType pre_e, next_e, del_e, get_e;

	if (GetElem(L, 2, get_e))
	{
		printf("第2个元素是:");
		visit(get_e);
		printf("---------------------------------\n\n");
	}
	else
		printf("线性表中没有第2个元素\n");


	int position = LocateElem(L, get_e);
	if (position)
	{
		printf("元素e的位置是:%d\n", position);
		printf("---------------------------------\n\n");
	}
	else
		printf("线性表中没有元素e\n");


	if (PriorElem(L, get_e, pre_e))
	{
		printf("元素e的直接前驱元素是:");
		visit(pre_e);
		printf("---------------------------------\n\n");
	}
	else
		printf("元素e没有直接前驱元素\n");


	if (NextElem(L, get_e, next_e))
	{
		printf("元素e的直接后继元素是:");
		visit(next_e);
		printf("---------------------------------\n\n");
	}
	else
		printf("元素e没有直接后继元素\n");


	if (ListDelete(L, 1, del_e))
	{
		printf("删除线性表中第一个位置的元素del_e是:");
		visit(del_e);
		printf("---------------------------------\n\n");
	}
	else
		printf("删除元素失败\n");

	ListTraverse(L);
	printf("-------------------------------------\n\n");

	int length = ListLength(L);
	printf("线性表的长度为:%d\n\n", length);


	ClearList(L);
	if (ListEmpty(L))
		printf("线性表为空!\n\n");
	else
		printf("线性表不为空!\n\n");


	DestroyList(L);
	if (L.elem)
		printf("线性表销毁失败!\n\n");
	else
		printf("线性表销毁成功!\n\n");*/

	return 0;
}


Status InitList(SqList& L)
{
	L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if (!L.elem)
		exit(OVERFLOW);
	L.length = 0;
	return OK;
}


void DestroyList(SqList& L)
{
	if (L.elem)
		free(L.elem);
}


void ClearList(SqList& L)
{
	L.length = 0;
}


Status ListEmpty(SqList L)
{
	if (!L.length)
		return TRUE;
	return FALSE;
}

Status ListLength(SqList L)
{
	return L.length;
}


Status GetElem(SqList L, int i, ElemType& e)
{
	if (i < 1 || i > L.length)
		return ERROR;
	e = L.elem[i - 1];
	return OK;
}


int compare(ElemType& a, ElemType& b)
{
	if (a.a == b.a && a.b == b.b)
		return TRUE;
	return FALSE;
}


Status LocateElem(SqList L, ElemType e)
{
	for (int i = 0; i < L.length; i++)
	{
		if (compare(e, L.elem[i]))
			return i + 1;
	}
	return FALSE;
}


Status PriorElem(SqList L, ElemType cur_e, ElemType& pre_e)
{
	int position = LocateElem(L, cur_e);
	if (position == 1)
		return ERROR;
	pre_e = L.elem[position - 2];
	return TRUE;
}


Status NextElem(SqList L, ElemType cur_e, ElemType& next_e)
{
	int position = LocateElem(L, cur_e);
	if (position == L.length)
		return ERROR;
	next_e = L.elem[position];
	return TRUE;
}


Status ListInsert(SqList& L, int i, ElemType e)
{
	if (i < 1 && i > L.length + 1)
		return ERROR;
	if (L.length == LIST_INIT_SIZE)
		return ERROR;
	for (int j = L.length; j >= i; j--)
	{
		L.elem[j] = L.elem[j - 1];
	}
	L.elem[i - 1] = e;
	L.length++;
	return OK;
}


Status ListDelete(SqList& L, int i, ElemType& e)
{
	if (i < 1 || i > L.length)
		return ERROR;
	e = L.elem[i - 1];
	for (int j = i - 1; j < L.length - 1; j++)
	{
		L.elem[j] = L.elem[j + 1];
	}
	L.length--;
	return OK;
}


void visit(ElemType e)
{
	printf("%lf %d\n", e.a, e.b);
}

void ListTraverse(SqList L)
{
	for (int i = 0; i < L.length; i++)
	{
		visit(L.elem[i]);
	}
}


void Union(SqList& La, SqList Lb)
{
	int La_len = ListLength(La);
	int Lb_len = ListLength(Lb);

	for (int i = 1; i <= Lb_len; i++)
	{
		ElemType elem;
		GetElem(Lb, i, elem);

		if (!LocateElem(La, elem))
		{
			ListInsert(La, ++La_len, elem);
		}
	}
}


int compare_size(ElemType a, ElemType b)
{
	if (a.b > b.b)
		return 1;
	else if (a.b < b.b)
		return -1;
	else
	{
		if (a.a > b.a)
			return 1;
		else if (a.a < b.a)
			return -1;
		else
			return 0;
	}
}


void MergeList(SqList La, SqList Lb, SqList& Lc)
{
	ElemType* pa = La.elem;
	ElemType* pb = Lb.elem;

	Lc.length = La.length + Lb.length;
	Lc.elem = (ElemType*)malloc(Lc.length * sizeof(ElemType));

	ElemType* pc = Lc.elem;
	ElemType* pa_last = pa + La.length - 1;
	ElemType* pb_last = pb + Lb.length - 1;

	while (pa <= pa_last && pb <= pb_last)
	{
		if (compare_size(*pa, *pb) == 1)
			*pc++ = *pb++;
		else
			*pc++ = *pa++;
	}

	while (pa <= pa_last)
		*pc++ = *pa++;
	while (pb <= pb_last)
		*pc++ = *pb++;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值