严蔚敏数据结构之线性表的基本操作

源自严蔚敏老师的教材,最近学校刚上完数据结构中的线性表,敲了下基本操作,没啥好说的,直接上代码,
注:这是我随手敲的,可能存在一些问题,仅供参考,仅供参考,仅供参考!!!

#include <iostream>
using namespace std;
#define LIST_INIT_SIZE 100//线性表存储空间的初始分配量
#define LISTINCREMENT 10//线性表初始存储空间的分配增量
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; 
typedef int  ElemType; 

typedef struct
{
	ElemType *elem;
	int length;
	int listsize;
}SqList;
Status InitList_Sq(SqList &L);//初始化线性表
Status DestroyList(SqList &L);//销毁线性表
Status ClearList(SqList &L);//清空线性表
Status ListEmpty(SqList L);//判断线性表是否为空
Status ListLength(SqList L);//返回线性表中的元素个数
Status GetElem(SqList L, int i, ElemType &e);//用e返回L中第i个元素
Status LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType));在线性表中寻找元素并返回元素位置
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e);//寻找元素cur_e的前驱元素
Status NextElem(SqList L, ElemType cur_e, ElemType &next_e);//寻找元素cur_e的后继元素
Status ListInsert(SqList &L, int i, ElemType e);//在线性表第i个位置插入元素e
Status ListDelete(SqList &L, int i, ElemType e);//删除线性表第i个位置的元素,其值用e带出
int main()
{
	
	return 0;
}
Status InitList_Sq(SqList &L)
{
	L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(int));
	if (!L.elem)
	{
		exit(OVERFLOW);
	}
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	return OK;
}
Status DestroyList(SqList &L)
{
	free(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 == 0)
	{
		return TRUE;
	}
	return FALSE;
}
Status ListLength(SqList L)
{
	return L.length;
}
Status GetElem(SqList L, int i, ElemType &e)
{
	if (i<0 || i>L.length)
	{
		return ERROR;
	}
	else
	{
		return e = L.elem[i-1];
	}
}
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e)
{
	if (L.elem[0] == cur_e)
	{
		return ERROR;
	}
	for (int i = 1; i < L.length; i++)
	{
		if (L.elem[i] == cur_e)
		{
			pre_e = L.elem[i - 1];
			return OK;
		}
	}
	return ERROR;
}
Status NextElem(SqList L, ElemType cur_e, ElemType &next_e)
{
	if (L.elem[L.length - 1] == cur_e)
	{
		return ERROR;
	}
	for (int i = 0; i < L.length - 1; i++)
	{
		if (L.elem[i] == cur_e)
		{
			next_e = L.elem[i + 1];
			return OK;
		}
	}
	return ERROR;
}
Status ListInsert(SqList &L, int i, ElemType e)
{
	if (i<0 || i>L.length)
	{
		return ERROR;
	}
	if (L.length >= L.listsize)
	{
		ElemType* newbase = (ElemType*)malloc((L.listsize + LISTINCREMENT)*sizeof(ElemType));

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值