线性表的顺序表示和实现

 1.顺序表的存储结构

#define ElemType int
#define MAXSIZE 100
typedef struct {
	ElemType* Data;
	int last;
}SeqList;

2.顺序表的基本操作

/// <summary>
/// initialize the seqlist.
/// </summary>
/// <param name="L">is a seqlist.</param>
void InitList(SeqList* L)
{
	L->Data = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
	if (L->Data == NULL) {
		printf("Fail to initialize the seqlist\n");
		return;
	}
	L->last = -1;
	return;
}

/// <summary>
/// Get the length of seqlist.
/// </summary>
/// <param name="L"> is a seqlist.</param>
/// <returns> The lenght of seqlist, not the array index.</returns>
int ListLength(SeqList* L)
{
	return L->last + 1;
}

/// <summary>
/// Get the i(th) data of seqlist.
/// </summary>
/// <param name="L">is a seqlist</param>
/// <param name="i">is the order of seqlist elem.</param>
/// <returns>the data of i(th) data of seqlist</returns>
ElemType GetData(SeqList* L, int i)
{
	return L->Data[i - 1];
}

/// <summary>
/// Insert the data before the i(th) elem of seqlist.
/// </summary>
/// <param name="L">is a seqlist</param>
/// <param name="i">is the order of seqlist elem.</param>
/// <param name="e">is the data of seqlist you want to insert.</param>
/// <returns>1 is success, and 0 is fail.</returns>
int InsList(SeqList* L, int i, ElemType e)
{
	if ((i < 1) || (i > L->last + 2)) {
		printf("Error location\n");
		return 0;
	}
	if (L->last == MAXSIZE - 1) {
		printf("The seqlist is full\n");
		return 0;
	}
	int num;
	for (num = L->last; num >= i - 1; num--) {
		L->Data[num + 1] = L->Data[num];
	}
	L->Data[i - 1] = e;
	L->last++;
	return 1;
}

/// <summary>
/// Delete the i(th) elem of seqlist
/// </summary>
/// <param name="L">is a seqlist</param>
/// <param name="i">is the order of seqlist (from 1 to L.last+1) </param>
/// <param name="e">the ptr of the elem deleted</param>
/// <returns>0 is fail, and 1 is success</returns>
int DelList(SeqList* L, int i,ElemType*e)
{
	if (i < 1 || i > L->last + 1) {
		printf("Error location\n");
		return 0;
	}
	*e = L->Data[i - 1];
	int num;
	for (num = i - 1; num < L->last; num++) {
		L->Data[num] = L->Data[num + 1];
	}
	L->last--;
	return 1;
}

/// <summary>
/// search the location of one of the seqlist's data, not the index of array
/// </summary>
/// <param name="L">is a seqlist</param>
/// <param name="e">the data of seqlist</param>
/// <returns>the location (from 1 to L.last+1), and 0 is fail</returns>
int Locate(SeqList* L, ElemType e)
{
	int i;
	for (i = 0; i <= L->last; i++) {
		if (L->Data[i] == e) {
			return i + 1;
		}
	}
	return 0;
}

/// <summary>
/// Destroy the seqlist
/// </summary>
/// <param name="L">is a seqlist</param>
void DestoryList(SeqList* L)
{
	L->last = -1;
	free(L->Data);
}

/// <summary>
/// make the seqlist empty
/// </summary>
/// <param name="L">is a seqlist</param>
void ClearList(SeqList* L)
{
	L->last = -1;
}

/// <summary>
/// judje if the seqlist is empty or not
/// </summary>
/// <param name="L"></param>
/// <returns>seqlist is empty return 1, seqlist is not empty,return 0</returns>
int EmptyList(SeqList* L)
{
	if (L->last == -1)return 1;
	else return 0;
}

3.顺序表的拓展操作

/// <summary>
/// Reverse the data of seqlist
/// </summary>
/// <param name="L">is a seqlist</param>
void Reverse(SeqList* L)
{
	int i;
	ElemType temp;
	for (i = 0; i <= (L->last) / 2; i++) {
		temp = L->Data[i];
		L->Data[i] = L->Data[L->last - i];
		L->Data[L->last - i] = temp;
	}
}

/// <summary>
/// sort the data of seqlist in ascending order by simple-sorting
/// </summary>
/// <param name="L">is a seqlist</param>
void SortList(SeqList* L)
{
	int i, j;
	ElemType temp;
	for (i = 0; i < L->last; i++)
		for (j = 0; j < L->last - i; j++)
			if (L->Data[j] > L->Data[j + 1]) {
				temp = L->Data[j];
				L->Data[j] = L->Data[j + 1];
				L->Data[j + 1] = temp;
			}

}

/// <summary>
/// merge two seqlist which in ascending order to a new seqlist
/// </summary>
/// <param name="LA">is a seqlist in ascending order</param>
/// <param name="LB">like LA, too</param>
/// <param name="LC">us the result of merge</param>
void MergeList(SeqList* LA, SeqList* LB, SeqList* LC)
{
	if (LA->last + LB->last + 2 > MAXSIZE) {
		printf("LA+LB is full, can't merge to a new seqlist");
		return;
	}
	int i = 0, j = 0;
	while (i <= LA->last && j <= LB->last) {
		if (LA->Data[i] <= LB->Data[j]) {
			LC->Data[i + j] = LA->Data[i];
			i++;
		}
		else {
			LC->Data[i + j] = LB->Data[j];
			j++;
		}
	}
	while (i <= LA->last) {
		LC->Data[i + j] = LA->Data[i];
		i++;
	}
	while (j <= LB->last) {
		LC->Data[i + j] = LB->Data[j];
		j++;
	}
	LC->last = LA->last + LB->last + 1;
}

4.主程序及验证

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

void InitList(SeqList* L);
int ListLength(SeqList* L);
ElemType GetData(SeqList* L,int i);
int InsList(SeqList* L, int i, ElemType e);
int DelList(SeqList* L, int i, ElemType* e);
int Locate(SeqList* L, ElemType e);
void DestoryList(SeqList* L);
void ClearList(SeqList* L);
int EmptyList(SeqList* L);

void Reverse(SeqList* L);
void SortList(SeqList* L);
void MergeList(SeqList* LA, SeqList* LB, SeqList* LC);

int main(void)
{
	SeqList L, * ptrL = &L;
	InitList(ptrL);
	int i= ListLength(ptrL);
	printf("The length of seqlist is %d\n",i);
	//we put 1 to 10 into the SeqList
	//and the seqlist have 10 elem
	for (i = 1; i <= 10; i++) {
		InsList(ptrL, i, i);
	}
	//we delete the 9th data of seqlist
	//and the seqlist have 9 elem
	ElemType temp;
	DelList(ptrL, 9,&temp);

	//we reverse the seqlist,
	//from(1,2,3,4,5,6,7,8,10) to (10,8,7,6,5,4,3,2,1)
	Reverse(ptrL);

	//we sort the seqlist in ascending order 
	//from (10,8,7,6,5,4,3,2,1) to (1,2,3,4,5,6,7,8,10)
	SortList(ptrL);

	//we output the 1st to 9th data of seqlist
	for (i = 1; i <= 9; i++) {
		printf("%dth elem is %d\n", i, GetData(ptrL, i));
	}

	//we search the location whose data is "10"
	int location = Locate(ptrL, 10);
	printf("Location==%d\n",location);
	
	SeqList L2, L3;
	InitList(&L2);
	for (i = 1; i <= 8; i++) {
		InsList(&L2, i, i * 2);
	}
	InitList(&L3);
	MergeList(&L, &L2, &L3);
	for (i = 1; i <= 17; i++) {
		printf("%dth elem is %d\n", i, GetData(&L3, i));
	}
	//we destory the seqlist
	DestoryList(ptrL);

	return 0;
}

5.运行结果截图(Visual Studio 2022)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值