ADT - 顺序表(Squencial List)

仅供参考,正确性有待检验(QAQ)
//ADT  Squencial List
#include<iostream>


#define		TRUE				1
#define		FALSE				0	
#define		OK					1
#define		ERROR				0
#define		OVERFLOW			-1
#define		LISTINICIALSIZE		100
#define		LISTINCREASE		10


using namespace std;


typedef int ElemType;
typedef	int Status;


typedef struct Sq_List {
	ElemType *elem;	//首地址
	int listLength;	//表长
	int listSize;	//表容量
}SqList;


//创建顺序表
Status ListCreater_Sq(SqList &L) {
	L.elem = (ElemType *)malloc(sizeof(ElemType) * LISTINICIALSIZE);
	if (!L.elem) exit(OVERFLOW);
	L.listLength = 0;
	L.listSize = LISTINICIALSIZE;
	return OK;
}


//清空顺序表
Status ListClear_Sq(SqList &L) {
	L.listLength = 0;
	return OK;
}


//销毁顺序表
Status ListFree_Sq(SqList &L) {
	free(L.elem);
	L.listLength = L.listSize = 0;
	return OK;
}


//表容量增加
void ListIncrease_Sq(SqList &L) {
	L.elem = (ElemType *)realloc(L.elem, sizeof(ElemType) * (L.listLength + LISTINCREASE));
	L.listSize += LISTINCREASE;
}


//线性表输入
void ListInput_Sq(SqList &L) {
	int len;
	cin >> len;
	if (len >= L.listSize) {
		ListIncrease_Sq(L);
	}
	for (int i = 1; i <= len; i++) {
		cin >> *(L.elem + i - 1);
	}
	L.listLength = len;
}


//线性表插入元素
Status ListInsert_Sq(SqList &L, int pos, ElemType e) {
	int len = L.listLength;
	if (pos <= 0 || pos > len + 1) return ERROR;
	if (L.listLength >= L.listSize) {
		ListIncrease_Sq(L);
	}
	for (int i = len + 1; i >= pos; i--) {
		*(L.elem + i) = *(L.elem + i - 1);
	}
	*(L.elem + pos - 1) = e;
	L.listLength++;
	return OK;
}


//线性表删除元素
Status ListDelete_Sq(SqList &L, int pos, ElemType &e) {
	int len = L.listLength;
	if (pos <= 0 || pos > L.listLength) return ERROR;
	e = *(L.elem + pos - 1);
	for (int i = pos - 1; i < L.listLength; i++) {
		*(L.elem + i) = *(L.elem + i + 1);
	}
	L.listLength--;
	return OK;
}


//线性表改值
Status ListSetVal_Sq(SqList &L, int pos, ElemType e) {
	if (pos > L.listLength || pos <= 0) return ERROR;
	*(L.elem + pos - 1) = e;
	return OK;
}


//线性表逆序
void ListReverse_Sq(SqList &L) {
	int len = L.listLength;
	for (int i = 1; i <= len / 2; i++) {
		ElemType tmp;
		tmp = *(L.elem + i - 1);
		*(L.elem + i - 1) = *(L.elem + len - i);
		*(L.elem + len - i) = tmp;
	}
}




//线性表元素排序 (<)
void ListSort_Sq(SqList &L) {
	int len = L.listLength;
	int j;
	for (int i = 2; i <= len; i++) {
		ElemType t = *(L.elem + i - 1);
		for (j = i - 1; j >= 1 && *(L.elem + j - 1) > t; j--) {
			*(L.elem + j) = *(L.elem + j - 1);
		}
		*(L.elem + j) = t;
	}
}


//线性表锁定元素位置
int ListLocate_Sq(SqList &L, ElemType e) {
	int len = L.listLength;
	for (int i = 1; i <= len; i++) {
		if (*(L.elem + i - 1) == e)
			return i;
	}
	return 0;
}


//判断表空
Status IsEmpty_Sq(SqList &L) {
	if (L.listLength == 0)
		return TRUE;
	return FALSE;
}


//线性表查找元素个数
int ListCount_Sq(SqList &L, ElemType e) {
	int len = L.listLength;
	int cnt = 0;
	for (int i = 1; i <= len; i++) {
		if (*(L.elem + i - 1) == e)
			cnt++;
	}
	return cnt;
}


//返回表长
int ListLength_Sq(SqList &L) {
	return L.listLength;
}


//返回表容量
int ListSize_Sq(SqList &L) {
	return L.listSize;
}


//线性表遍历输出
void ListPrint_Sq(SqList &L) {
	for (int i = 1; i <= L.listLength; i++) {
		if (i == 1)
			cout << *(L.elem + i - 1);
		else
			cout << ' ' << *(L.elem + i - 1);
	}
	cout << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值