《数据结构》C语言版 顺序表的基本操作实现

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

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef int Status;
typedef int ElemType;

typedef struct
{
	ElemType *elem;//存储空间基址
	int length;
	int listsize;//当前分配的存储容量
}SqList;

//构造空线性表

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


//写入数据
void InputData(SqList& L)
{
	int n, i;
	printf("input n:");
	scanf_s("%d", &n);
	for (i = 0; i < n; i++)
	{
		printf("No.%d:", i + 1);
		scanf_s("%d", &L.elem[i]);
	}
	L.length = n;
}

//遍历
void ListPrint(SqList L)
{
	int i;
	printf("The list is :");
	for (i = 0; i < L.length; i++)
	{
		printf("%d ", L.elem[i]);
	}
	printf("\n");
}
//判空
Status ListEmpty(SqList L)
{
	return L.length == 0 ? TRUE : FALSE;
}

//输出线性表元素个数
int ListLength(SqList L)
{
	return L.length;
}

//清空线性表
Status ClearList(SqList& L)
{
	L.length = 0;
	return OK;
}

//销毁线性表
void DestroyList(SqList& L)
{
	free(L.elem);//非free(L)
	L.length = 0;
	L.listsize = 0;//存储容量为0
	L.elem = NULL;//数据为空
}

//从线性表中获取第i个元素
Status GetElem(SqList L, int i, ElemType* e)
{
	if (i<1 || i>L.length)
		return ERROR;
	*e = L.elem[i - 1];
	return OK;
}

//查找线性表中的数据在线性表中第一次出现的位置
int LocateElem_Sq(SqList L, ElemType e)
{
	//输入一个数在线性表中找到他的位置
	int i;
	ElemType* p;
	i = 0;
	p = L.elem;
	while (i <= L.length && *p != e)
	{
		++i;
		p++;
	}
	if (i <= L.length)return i;
	else
		return 0;
}

//删除线性表中的数据
Status ListDelete_Sq(SqList& L, int i, ElemType& e)

{//删除第i个元素并返回其值e
	ElemType* p;
	ElemType* q;
	if (i < 1 || (i > L.length))
		return ERROR;
	p = &(L.elem[i - 1]);		//被删除元素的位置
	e = *p;//找到需要删除的值传给*e
	q = L.elem + L.length - 1;//表尾元素的位置
	for (++p; p <= q; ++p)
		*(p - 1) = *p;			// 被删除之后的元素左移
	--L.length;				// 表长减一
	return OK;
}


//线性表中插入数据
Status ListInsert_Sq(SqList &L, int i, ElemType e)
{
	ElemType* q;
	ElemType* p;
	ElemType* newbase;
	if (i<1 || i>L.length + 1)//插入位置不符合条件
		return ERROR;
	if (L.length >= L.listsize)//当前存储空间已满,需要增加分配
	{
		newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
		if (newbase == 0)exit(OVERFLOW);	//分配空间失败
		L.elem = newbase;
		L.listsize += LISTINCREMENT;
	}
	// 注意:C语言中数组的下标从0开始,线性表中第i个元素是L.elem[i-1]
	q = &(L.elem[i - 1]);//q为插入位置

	for (p = &(L.elem[L.length - 1]); p >= q; --p) *(p + 1) = *p;//插入位置之后元素后移
	*q = e;
	++L.length;
	return OK;
}

Status MergeList(SqList La, SqList Lb, SqList& Lc) {
	int* pa, * pa_last, * pb, * pb_last, * pc;
	pa = La.elem;
	pb = Lb.elem;
	Lc.listsize = Lc.length = La.length + Lb.length;//不用InitList()创建空表Lc
	pc = Lc.elem = (int*)malloc(Lc.listsize * sizeof(int));
	if (!Lc.elem) //存储分配失败
		exit(0);
	pa_last = La.elem + La.length - 1;
	pb_last = Lb.elem + Lb.length - 1;
	while (pa <= pa_last && pb <= pb_last) //表La和表Lb均非空
	{ //归并
		if (*pa < *pb)
			*pc++ = *pa++;
		else  if (*pa > * pb)
			*pc++ = *pb++;
		else {
			*pc++ = *pa++;
			pb++;
			Lc.length--;
		}
	}
	while (pa <= pa_last) //表La非空且表Lb空
		*pc++ = *pa++; //插入La的剩余元素
	while (pb <= pb_last) //表Lb非空且表La空
		*pc++ = *pb++; //插入Lb的剩余元素
	return OK;
}



void main()
{
	SqList L;
	int choice = 1;

	if (InitList_Sq(L) == OK)
		printf("success!\n");
	else
		printf("fail!\n");
	while (choice != 0)
	{
		//meau
		system("cls");
		printf("1.InputDate\t2.ListPrint\t3.ListEmpty\t4.ListLength\t5.ClearList\n");
		printf("6.DestroyList\t7.GetElem\t8.LocateElem\t9.ListDelete\t10.ListInsert\t11.MergeList\n");
		printf("0.Exit\n");

		//input choice
		printf("Input your choice:");
		scanf_s("%d", &choice);

		//action
		switch (choice)
		{
		case 0:
			printf("ByeBye!\n");
			break;
		case 1:
			InputData(L);
			break;
		case 2:
			ListPrint(L);
			break;
		case 3:
			if (ListEmpty(L) == TRUE)
				printf("The list is empty!\n");
			else
				printf("The list is not empty!\n");
			break;
		case 4:
		{
			int len;
			len = ListLength(L);
			printf("The length is %d.\n", len);
			break;
		}
		case 5:
			if (ClearList(L) == OK)
				printf("Clear!\n");
			break;
		case 6:
			DestroyList(L);
			break;
		case 7:
		{
			int pos;
			ElemType e;
			printf("Input the position:");
			scanf_s("%d", &pos);
			if (GetElem(L, pos, &e) == OK)
				printf("The No.%d element is %d.", pos, e);
			else
				printf("The position is invalid.\n");
			break;
		}
		case 8:
		{
			ElemType e;

			printf("Input the number:");
			scanf_s("%d", &e);
			int pos = LocateElem_Sq(L, e);
			if (pos > 0)
				printf("Found! Tts position is %d.\n", pos + 1);
			else
				printf("Not found!\n");
			break;
		}
		case 9:
		{
			ElemType e;
			int i;
			printf("Input the index of list:");
			scanf_s("%d", &i);
			if ((ListDelete_Sq(L, i, e)) == TRUE)
				printf("Have deleted!\n");
			else
				printf("Have not deleted!\n");
			break;
		}
		case 10:
		{
			int i;
			ElemType e;
			printf("Input the position:");
			scanf_s("%d", &i);
			printf("Input a number:");
			scanf_s("%d", &e);
			if (ListInsert_Sq(L, i, e) == OK)
				printf("Have inserted!\n");
			else
				printf("Not inserted!\n");
			break;
		}
		case 11:
		{
			SqList Lb;
			SqList Lc;
			if (InitList_Sq(Lb) == OK) {		//初始化Lb
				printf("Lb Init success!\n");
				InputData(Lb);
				MergeList(L, Lb, Lc);
				ListPrint(Lc);
				free(Lb.elem);
			}

			else
				printf("Lb Init fail!\n");


			break;
		}

		}
		system("pause");
	}
	DestroyList(L);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值