赞
踩
仅供参考,正确性有待检验(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;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。