您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include<assert.h> #define MAXSIZE 100 typedef int DataType; typedef struct SeqList { DataType _array[MAXSIZE]; size_t _size; }SeqList; void InitSeqList(SeqList* pSeq) { assert(pSeq); memset(pSeq->_array,0, sizeof(DataType)*MAXSIZE); pSeq->_size = 0; } void PushBack(SeqList* pSeq,DataType x) { assert(pSeq->_size< MAXSIZE); pSeq->_array[pSeq->_size++] = x; } void PopBack(SeqList* pSeq) { assert(pSeq->_size>0); --pSeq->_size; } void PushFront(SeqList* pSeq,DataType x) { assert(pSeq->_size < MAXSIZE); for (int i = pSeq->_size-1; i >= 0; i--) { pSeq->_array[i+1] = pSeq->_array[i]; } ++pSeq->_size; pSeq->_array[0] = x; } void PopFront(SeqList* pSeq) { assert(pSeq->_size > 0); for (int i = 1; i < pSeq->_size; i++) { pSeq->_array[i - 1] = pSeq->_array[i]; } --pSeq->_size; } void Insert(SeqList* pSeq,int pos, DataType x) { assert(pos >= 0); assert(pSeq->_size < MAXSIZE); for (int i = pSeq->_size - 1; i >= pos; i--) { pSeq->_array[i+1] = pSeq->_array[i]; } ++pSeq->_size; pSeq->_array[pos] = x; } int Find(SeqList*pSeq,DataType x) { int i = 0; for (i = 0; i < pSeq->_size; i++) { if (pSeq->_array[i] == x) { return i; } } return -1; } void Erase(SeqList* pSeq, int pos) { assert(pos > 0); for (int i = pos; i < pSeq->_size; i++) { pSeq->_array[i] = pSeq->_array[i + 1]; } --pSeq->_size; } void PrintSeqList(SeqList* pSeq) { if (pSeq->_size == 0) { printf("当前顺序表为空!"); return; } for (int i = 0; i < pSeq->_size; i++) { printf("%d ", pSeq->_array[i]); } printf("\n"); } void Test() { SeqList List; InitSeqList(&List); PushBack(&List,1); PushBack(&List, 2); PushBack(&List, 3); PushBack(&List, 4); PushBack(&List, 5); PushBack(&List, 6); PrintSeqList(&List); PopBack(&List); PopBack(&List); PopBack(&List); PrintSeqList(&List); PushFront(&List,0); PushFront(&List, -1); PushFront(&List, -2); PrintSeqList(&List); PopFront(&List); PopFront(&List); PopFront(&List); PrintSeqList(&List); int ret = Find(&List, 2); printf("%d\n", ret); Erase(&List, 1); PrintSeqList(&List); Insert(&List, 1, 2); PrintSeqList(&List); } int main() { Test(); system("pause"); return 0; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。