#include<iostream> #define MaxSize 100//宏替换没有分号 #define lenth 5 using namespace std; typedef struct LNode * List; struct LNode { int Data[MaxSize]; int Last; }; List ListMakeEmpty() { List PtrL; PtrL = (List)malloc(sizeof(struct LNode)); PtrL->Last = -1; return PtrL; } void insert(int X,int k,List PtrL ) { if (k <= (PtrL->Last + 1) && k >= 0) { for (int i = (PtrL->Last+1); i >= k; i--) { PtrL->Data[i + 1] = PtrL->Data[i]; } PtrL->Data[k] = X; PtrL->Last++; } else if (k < 0) { cout << "所插入位置小于0" << endl; } else if (k > (PtrL->Last + 1)) { cout << "所插入位置在表尾后,且不紧邻表尾" << endl; } } void Delete(int k, List PrtL) { if (k >= 0 && k <= PrtL->Last) { for (int i = k; i <= PrtL->Last; i++) { PrtL->Data[i] = PrtL->Data[i + 1]; } PrtL->Last--; } } void Print_All(List PtrL) { for (int i = 0; i <= PtrL->Last; i++) { cout << PtrL->Data[i] << endl; } } int Find(int X, List PtrL) { for (int i = 0; i <= PtrL->Last; i++) { if (PtrL->Data[i] == X)return i; } cout << "没有查询到该元素" << endl; return 1; } int main() { List PtrL = ListMakeEmpty(); int a[lenth] = { 5,4,3,2,1 }; for (int i = 0; i < lenth; i++) { insert(a[i], i, PtrL); } Print_All(PtrL); system("pause"); for (int i =0;i<=PtrL->Last;i++) { cout << Find(a[i], PtrL) << endl; } system("pause"); int k = PtrL->Last; for (int i = 0; i <=k; i++) { cout << "删除序列"<<i<< endl; Delete(0, PtrL); Print_All(PtrL); } system("pause"); return 0; }
顺序存储 C++ 代码实现
猜你喜欢
转载自blog.csdn.net/weixin_41525190/article/details/80637631
今日推荐
周排行