线性表(小白,如有错误还望海涵)
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define ADDSIZE 10
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
typedef struct {
?? ?int* elem;
?? ?int length;
}SqList;
int InitList(SqList& L) {//初始化線性表
?? ?L.elem = new int[MAXSIZE];
?? ?if (!L.elem)
?? ??? ?exit(OVERFLOW);
?? ?L.length = 0;
?? ?return OK;
}
void DestoryList(SqList& L) {//銷毀線性表
?? ?if (L.elem)
?? ??? ?delete L.elem;
}
void ClearList(SqList& L) {//清空線性表
?? ?if (L.length != 0)
?? ??? ?L.length = 0;
}
int ListInsert(SqList& L, int i, int e) {//在線性表中i的位置插入元素e
?? ?if (i<1 || i>L.length + 1)
?? ??? ?return ERROR;
?? ?if (L.length == MAXSIZE)
?? ??? ?return ERROR;
?? ?for (int j = L.length - 1; j >= i - 1; j--)
?? ??? ?L.elem[j + 1] = L.elem[j];
?? ?L.elem[i - 1] = e;
?? ?L.length++;
?? ?return OK;
}
int ListDelete(SqList& L, int i) {//刪除第i個元素,用e返回
?? ?if (i<1 || i>L.length)
?? ??? ?return ERROR;
?? ?if (L.length == 0)
?? ??? ?return ERROR;
?? ?int e = L.elem[i - 1];
?? ?cout << e << endl;
?? ?for (int j = i - 1; j < L.length-1; j++)
?? ??? ?L.elem[j] = L.elem[j + 1];
?? ?L.length++;
}
bool IsEmpty(SqList L) {//判斷線性表是否為空
?? ?if (L.length == 0)
?? ??? ?return FALSE;
?? ?else
?? ??? ?return TRUE;
}
int ListLength(SqList L) {//返回線性表中元素個數
?? ?return L.length;
}
int LocateElem(SqList L, int e) {//查找與e等值的元素,返回其位置
?? ?for (int j = 0; j <= L.length - 1; j++) {
?? ??? ?if (L.elem[j] == e)
?? ??? ??? ?return j + 1;
?? ??? ?break;
?? ?}
}
int GetElem(SqList L, int i, int& e) {//返回第i個元素
?? ?e = L.elem[i - 1];
?? ?return e;
}
void PrintList(SqList L) {//輸出線性表
?? ?for (int j = 0; j <= L.length - 1; j++)
?? ??? ?cout << L.elem[j]<<" ?";
?? ?cout << endl;
}
int main() {
?? ?SqList L1;
?? ?InitList(L1);
?? ?ListInsert(L1, 1, 1);
?? ?ListInsert(L1, 2, 2);
?? ?ListInsert(L1, 3, 3);
//?? ?PrintList(L1);
//?? ?ListDelete(L1, 2);
?? ?system("pause");
?? ?return 0;
}
總結
以上是生活随笔為你收集整理的线性表(小白,如有错误还望海涵)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 量子计算更适用于优化:专访首家量子计算上
- 下一篇: 听说月薪3万的公众号运营者,都偷偷藏了这