顺序表的基本操作 SCAU8576、SCAU8577、SCAU8578

//此项目功能:完成顺序表的基本操作及OJ给出的三道题:8576、8577、8578
#include <stdio.h>
#include <malloc.h>
#define ElemType int
#define Status int
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100

typedef struct SqList //定义顺序表结构
{
	ElemType *Elem;
	int length;
	int ListSize;
} SqList;

Status InItList(SqList &L) //初始化顺序表
{
	L.Elem = new ElemType[MAXSIZE];
	L.length = 0;
	L.ListSize = MAXSIZE;
	return OK;
}

Status DestroyList(SqList &L) //销毁顺序表
{
	delete L.Elem;
	L.Elem = NULL;
	L.length = 0;
	L.ListSize = 0;
	return OK;
}

Status ClearList(SqList &L) //将顺序表重置为空表
{
	L.length = 0;
	return OK;
}

Status ListEmpty(SqList L) //判断顺序表是否为空
{
	if (L.length)
		return FALSE;
	else
		return TRUE;
}

int ListLength(SqList L) //返回顺序表长度
{
	return L.length;
}

Status GetElem(SqList L, int i, ElemType &e) //返回顺序表中第i个元素的值
{
	if (i > L.length || i < 1)
		return ERROR;
	else
		e = L.Elem[i - 1];
	return OK;
}

int LocateElem(SqList L, ElemType e) //返回顺序表中第一个与e值相同的元素的位置
{
	int i;
	for (i = 0; L.Elem[i] != e && i < L.length; i++)
		;
	if (L.Elem[i] == e)
		return i + 1;
	else
		return 0;
}

Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e) //若cur_e是L的元素且不是第一个,返回其前驱至pre_e,若有多个cur_e符合条件,返回的前驱是第一个满足条件的cur_e的前驱
{
	int Location = LocateElem(L, cur_e);
	if (Location == 0 || Location == 1)
		return ERROR;
	else
		GetElem(L, Location - 1, pre_e);
	return OK;
}

Status NextElem(SqList L, ElemType cur_e, ElemType &next_e) //若cur_e是L的元素且不是最后一个,返回其后继至next_e,若有多个cur_e符合条件,返回的后继是第一个满足条件的cur_e的后继
{
	int Location = LocateElem(L, cur_e);
	if (Location == 0 || Location == L.length)
		return ERROR;
	else
		GetElem(L, Location + 1, next_e);
	return OK;
}

Status ListInsert(SqList &L, int i, ElemType e) //在第i个位置之前插入元素e
{
	if (i < 1 || i > L.length + 1 || L.length == L.ListSize)
		return ERROR;
	else
	{
		for (int j = L.length - 1; j >= i - 1; j--)
			L.Elem[j + 1] = L.Elem[j];
		L.Elem[i - 1] = e;
		L.length++;
	}
	return OK;
}

Status ListDelete(SqList &L, int i, ElemType &e) //删除第i个元素
{
	if (i < 1 || i > L.length || !L.length)
		return ERROR;
	else
	{
		e = L.Elem[i - 1];
		for (int j = i - 1; j <= L.length - 2; j++)
			L.Elem[j] = L.Elem[j + 1];
		L.length--;
	}
	return OK;
}

Status Traverse(SqList L) //遍历顺序表
{
	if (ListEmpty(L))
	{
		printf("The List is empty!\n");
		return ERROR;
	}
	else
	{
		//printf("The List is: ");
		for (int i = 0; i < L.length; i++)
			printf("%d ", L.Elem[i]);
		printf("\n");
	}
	return OK;
}

void PreMerge(SqList &C, SqList &A) //合并有序顺序表的前置函数
{
	int x;
	ListDelete(A, 1, x);
	ListInsert(C, C.length + 1, x);
}

void Merge(SqList &C, SqList &A, SqList &B) //合并顺序表
{
	while (A.length && B.length)
	{
		if (A.Elem[0] == B.Elem[0])
		{
			PreMerge(C, A);
			PreMerge(C, B);
		}
		else if (A.Elem[0] < B.Elem[0])
			PreMerge(C, A);
		else
			PreMerge(C, B);
	}
	while (A.length || B.length)
	{
		while (A.length)
			PreMerge(C, A);
		while (B.length)
			PreMerge(C, B);
	}
}

void TurnList(SqList &L) //逆置顺序表
{
	for (int i = 0; i < L.length - 1; i++)
	{
		int x;
		ListDelete(L, 1, x);
		ListInsert(L, L.length + 1 - i, x);
	}
}

void FuctionsTest(SqList &L)
{
	int a, i;
	ElemType e, x;
	ElemType cur_e, pre_e, next_e;
	while (1)
	{
		scanf("%d", &a);
		switch (a)
		{
		case 1:
			InItList(L);
			break;
		case 2:
			ClearList(L);
			break;
		case 3:
			scanf("%d", &i);
			if (!GetElem(L, i, e))
				printf("GetElem Error.\n");
			else
				printf("%d\n", e);
			break;
		case 4:
			scanf("%d", &e);
			if (!LocateElem(L, e))
				printf("LocateElem Error.\n");
			else
				printf("%d\n", LocateElem(L, e));
			break;
		case 5:
			scanf("%d", &cur_e);
			if (!PriorElem(L, cur_e, pre_e))
				printf("No cur_e or no pre_e.\n");
			else
				printf("%d\n", pre_e);
			break;
		case 6:
			scanf("%d", &cur_e);
			if (!NextElem(L, cur_e, next_e))
				printf("No cur_e or no next_e.\n");
			else
				printf("%d\n", next_e);
			break;
		case 7:
			scanf("%d%d", &i, &x);
			if (!ListInsert(L, i, x))
				printf("Insert Error!\n");
			else
				printf("The Element %d is Successfully Inserted!\n", x);
			break;
		case 8:
			scanf("%d", &i);
			if (!ListDelete(L, i, e))
				printf("Delete Error!\n");
			else
				printf("The Element %d is Successfully Deleted!\n", e);
			break;
		case 9:
			Traverse(L);
			break;
		case 0:
			return;
		}
	}
}

int main()
{
	/*SCAU 8576
	SqList L;
	if (InItList(L))
	{
		printf("A Sequence List Has Created.\n");
	}
	int a, i;
	ElemType x,e;
	while (1)
	{
		printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
		scanf("%d", &a);
		switch (a)
		{
		case 1: scanf("%d%d", &i, &x);
			if (!ListInsert(L,i,x)) printf("Insert Error!\n");
			else printf("The Element %d is Successfully Inserted!\n", x);
			break;
		case 2: scanf("%d", &i);
			if (!ListDelete(L,i,e)) printf("Delete Error!\n");
			else printf("The Element %d is Successfully Deleted!\n", e);
			break;
		case 3: Traverse(L);
			break;
		case 0: return 1;
		}
	}
	*/
	/*SCAU 8577
  SqList A,B,C;
  InItList(A);
  InItList(B);
  InItList(C);
  int a,b;
  scanf("%d", &a);
  for (int i = 0; i < a; i++)
  {
	  int x;
	  scanf("%d", &x);
	  ListInsert(A,A.length+1,x);
  }
  scanf("%d", &b);
  for (int i = 0; i < b; i++)
  {
	  int x;
	  scanf("%d", &x);
	  ListInsert(B,B.length+1,x);
  }
  printf("List A:");
  Traverse(A);
  printf("List B:");
  Traverse(B);
  Merge(C,A,B);
  printf("List C:");
  Traverse(C);
	*/
	/*SCAU 8578
	SqList L;
	InItList(L);
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int x;
		scanf("%d", &x);
		ListInsert(L,L.length+1,x);
	}
	printf("The List is:");
  Traverse(L);
  TurnList(L);
	printf("The turned List is:");
  Traverse(L);
	*/
	/*
	SqList L;
	FuctionsTest(L);
	*/
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值