数据结构之顺序表(部分参考C博客—文逗,整理和参考严蔚敏版数据结构)

来源:互联网 发布:日本文学 知乎 编辑:程序博客网 时间:2024/05/22 00:45

#include<iostream>#include<malloc.h>using namespace std;#define LISTSIZE 100#define SIZEADD 10typedef int ElemType;typedef struct{ElemType *pBase;int Length;int Listsize;}SqList;void InitList(SqList &L){L.pBase = new ElemType[LISTSIZE];L.Length = 0;L.Listsize = LISTSIZE;}void CreateList(SqList &L, int len){cout << "请输入" << len << "个元素的值:" << endl;for (int i = 0; i<len; i++)cin >> L.pBase[i];L.Length = len;}void DestroyList(SqList &L){delete[]L.pBase;L.pBase = NULL;L.Length = 0;L.Listsize = 0;return;}void ClearList(SqList &L){L.Length = 0;return;}bool ListEmpty(SqList L){if (L.Length)return false;elsereturn true;}int ListLength(SqList L){return L.Length;}void ListReverse(SqList &L){ElemType tem;if (ListEmpty(L))return;for (int i = 0, j = L.Length - 1; i<j; ++i, --j){tem = L.pBase[i]; L.pBase[i] = L.pBase[j]; L.pBase[j] = tem;}return;}bool ListInsert(SqList &L, int pos, ElemType e){if (pos<1 || pos>L.Length + 1) return false;if (L.Length >= L.Listsize){ElemType *pNBase = (ElemType*)realloc(L.pBase, (L.Listsize + SIZEADD)*sizeof(ElemType));L.pBase = pNBase;L.Listsize += SIZEADD;}ElemType *q = &(L.pBase[pos - 1]);//指向当前ElemType *p = &(L.pBase[L.Length - 1]);//指向尾巴while (p >= q)//从尾巴至当前位置的值往后移动,当前插入位置不动{*(p + 1) = *(p);--p;}*q = e;++L.Length;return true;}bool ListDelete(SqList &L, int pos, ElemType &e){if (pos<1 || pos>L.Length)return false;ElemType *q = L.pBase + pos - 1;ElemType *p = L.pBase + L.Length - 1;e = *q;for (++q; q <= p; ++q)*(q - 1) = *q;--L.Length;return true;}bool DeletePart(SqList &L, int pos, int count)//删除从第i个元素起的count个元素{int i;if (pos < 1 || count<1 || pos + count - 1>L.Length)return false;for (i = 0; pos + i - 1 < L.Length - count; i++)//循环条件?L.pBase[pos + i - 1] = L.pBase[pos + i + count - 1];L.Length -= count;return true;}int LocateElem(SqList L, ElemType e)//获取元素的位置{int pos = 1;while (pos <= L.Length&&L.pBase[pos - 1] != e){pos++;}return pos;//注意当pos>L.Length时则是获取失败}bool GetElem(SqList L, int pos, ElemType &e){if (pos<1 || pos>L.Length)return false;e = L.pBase[pos - 1];return true;}bool PriorElem(SqList L, ElemType cur, ElemType &pre){int pos;pos = LocateElem(L, cur);            //先查询是否存在curif (pos > L.Length || pos == 1)return false;else                                 //存在{pre = L.pBase[pos - 2];return true;}}bool NextElem(SqList L, ElemType cur, ElemType &next){int pos;pos = LocateElem(L, cur);if (pos >= L.Length)return false;else{next = L.pBase[pos];return true;}}int ListCompare(SqList LA, SqList LB){int i = 0;while (i < LA.Length&&i < LB.Length){if (LA.pBase[i] == LB.pBase[i])i++;else if (LA.pBase[i] < LB.pBase[i])return -1;elsereturn 1;}if (i == LA.Length&&i == LB.Length)return 0;else if (i == LA.Length&&i < LB.Length)return -1;elsereturn 1;}void ListMerge(SqList LA, SqList LB, SqList &LC)//归并两个值非递减的顺序表为一个值非递减的顺序表{ElemType *pA, *pB, *pC;ElemType *pALast, *pBLast;pA = LA.pBase;pB = LB.pBase;pC = LC.pBase;LC.Listsize = LC.Length = LA.Length + LB.Length;pALast = LA.pBase + LA.Length - 1;pBLast = LB.pBase + LB.Length - 1;while (pA <= pALast&&pB <= pBLast){if (*pA <= *pB)*pC++ = *pA++;else*pC++ = *pB++;}while (pA <= pALast)*pC++ = *pA++;while (pB<=pBLast)*pC++ = *pB++;}void ListTraverse(SqList L){for (int i = 0; i < L.Length; i++){cout << L.pBase[i] << " ";}cout << endl;return;}int main(void){SqList LA, LB, LC;InitList(LA);InitList(LB);InitList(LC);CreateList(LA, 4);CreateList(LB, 5);ListMerge(LA, LB, LC);ListTraverse(LA);ListTraverse(LB);ListTraverse(LC);/*ListInsert(L, 7, 7);ListTraverse(L);ListDelete(L, 7, e);cout << e << endl;ListTraverse(L);GetElem(L, 2, e);cout << e << endl;i = LocateElem(L, 2);if (i > L.Length)cout << "元素位置获取失败!" << endl;elsecout << i << endl;PriorElem(L, 2, e);cout << e << endl;NextElem(L, 2, e);cout << e << endl;DeletePart(L, 2, 3);ListTraverse(L);*/return(0);}

0 0
原创粉丝点击