摘要:创建一个结构体用于存放顺序表相关数据有效数据个数容量初始化顺序表插入元素插入到表头插入到指定位置插入到尾部先检查容量是否够用如果空间满了,扩容增
#define SEQTYPE inttypedef struct SeqList{ SEQTYPE* data; int size; //有效数据个数 int capacity; //容量}SeqList;
void SeqListInit(SeqList* pq){ CheckNull(pq); pq->data = NULL; pq->capacity = 0; pq->size = 0;}
void CheckCapacity(SeqList* pq){ CheckNull(pq); //如果空间满了,扩容 if (pq->size >= pq->capacity) { int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2; SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity); if (new == NULL) { perror("realloc"); exit(-1); } pq->data = new; pq->capacity = newcapacity; } puts("增容成功");}//往顺序表指定位置插入数据void SeqListInsert(SeqList* pq, int pos){ CheckNull(pq); assert(pos <= pq->size); SEQTYPE InsertVal; if (pos == -1) { printf("请分别输入添加的数据和位置,空格隔开:>"); scanf("%d %d", &InsertVal, &pos); if (pos > pq->size) { printf("请正确输入/n"); return; } } else { printf("请输入添加的数据:>"); scanf("%d", &InsertVal); } //检查容量是否足够 CheckCapacity(pq); //插入数据 int end = pq->size; int begin = pos; while (begin < end) { pq->data[end] = pq->data[end - 1]; --end; } pq->data[pos] = InsertVal; ++pq->size; printf("添加成功/n");}//往顺序表末位置插入数据void SeqListPushBack(SeqList* pq){ CheckNull(pq); SeqListInsert(pq, pq->size);}//往顺序表首位置插入数据void SeqListPushFront(SeqList* pq){ CheckNull(pq); SeqListInsert(pq, 0);}
//从顺序表指定位置删除数据void SeqListErase(SeqList* pq, int pos){ CheckNull(pq); if (pos == -1) { printf("请输入要删除数据的位置:>"); scanf("%d", &pos); if (pos < 0 || pos >= pq->size) { printf("请正确输入/n"); return; } } int begin = pos; int end = pq->size - 1; while (begin < end) { pq->data[begin] = pq->data[begin + 1]; ++begin; } --pq->size; puts("删除成功");}//从顺序表末位置删除数据void SeqListPophBack(SeqList* pq){ CheckNull(pq); SeqListErase(pq, pq->size - 1);}//从顺序表首位置删除数据void SeqListPophFront(SeqList* pq){ CheckNull(pq); SeqListErase(pq, 0);}
//修改顺序表指定位置数据void SeqListModify(SeqList* pq){ CheckNull(pq); int pos; SEQTYPE x; printf("请输入修改的位置和新的数据,空格隔开:>"); scanf("%d %d", &pos, &x); if (pos < 0 && pos >= pq->size) { printf("请正确输入/n"); return; } pq->data[pos] = x; puts("修改成功");}
//查找所需数据是否存在顺序表中void SeqListFindData(SeqList* pq){ CheckNull(pq); SEQTYPE x; printf("请输入要查找的数据:>"); scanf("%d", &x); for (int i = 0; i < pq->size; i++) { if (pq->data[i] == x) { printf("所需查询数据存在,下标为:>%d/n", i); return; } } printf("找不到/n");}
//排序顺序表void SeqListSort(SeqList* pq){ CheckNull(pq); int option = 0; printf("输入0为升序,1为降序:>"); scanf("%d", &option); for (int i = 0; i < pq->size - 1; i++) { for (int j = 0; j < pq->size - i - 1; j++) { if (pq->data[j] > pq->data[j + 1]) { SEQTYPE tmp = pq->data[j]; pq->data[j] = pq->data[j + 1]; pq->data[j + 1] = tmp; } } } if (option) { SeqListReverse(pq); return; }}
//顺序表反转void SeqListReverse(SeqList* pq){ CheckNull(pq); int left = 0; int right = pq->size - 1; while (left < right) { SEQTYPE tmp = pq->data[left]; pq->data[left] = pq->data[right]; pq->data[right] = tmp; ++left; --right; }}
#include "SeqList.h"void CheckNull(SeqList* pq){ if (pq == NULL) { perror("pq::"); exit(-1); }}//初始化顺序表void SeqListInit(SeqList* pq){ CheckNull(pq); pq->data = NULL; pq->capacity = 0; pq->size = 0;}void SeqListDestory(SeqList* pq){ CheckNull(pq); free(pq->data); pq->data = NULL; pq->size = 0; pq->capacity = 0;}void CheckCapacity(SeqList* pq){ CheckNull(pq); //如果空间满了,扩容 if (pq->size >= pq->capacity) { int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2; SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity); if (new == NULL) { perror("realloc"); exit(-1); } pq->data = new; pq->capacity = newcapacity; } puts("增容成功");}void SeqListPrint(SeqList* pq){ CheckNull(pq); if (pq->size == 0) printf("/n"); else { for (int i = 0; i < pq->size; i++) { printf("%d ", pq->data[i]); } puts("/n--------------------------------------"); }}//往顺序表末位置插入数据void SeqListPushBack(SeqList* pq){ CheckNull(pq); SeqListInsert(pq, pq->size);}//往顺序表首位置插入数据void SeqListPushFront(SeqList* pq){ CheckNull(pq); SeqListInsert(pq, 0);}//往顺序表指定位置插入数据void SeqListInsert(SeqList* pq, int pos){ CheckNull(pq); assert(pos <= pq->size); SEQTYPE InsertVal; if (pos == -1) { printf("请分别输入添加的数据和位置,空格隔开:>"); scanf("%d %d", &InsertVal, &pos); if (pos > pq->size) { printf("请正确输入/n"); return; } } else { printf("请输入添加的数据:>"); scanf("%d", &InsertVal); } //检查容量是否足够 CheckCapacity(pq); //插入数据 int end = pq->size; int begin = pos; while (begin < end) { pq->data[end] = pq->data[end - 1]; --end; } pq->data[pos] = InsertVal; ++pq->size; printf("添加成功/n");}//从顺序表指定位置删除数据void SeqListErase(SeqList* pq, int pos){ CheckNull(pq); if (pos == -1) { printf("请输入要删除数据的位置:>"); scanf("%d", &pos); if (pos < 0 || pos >= pq->size) { printf("请正确输入/n"); return; } } int begin = pos; int end = pq->size - 1; while (begin < end) { pq->data[begin] = pq->data[begin + 1]; ++begin; } --pq->size; puts("删除成功");}//从顺序表末位置删除数据void SeqListPophBack(SeqList* pq){ CheckNull(pq); SeqListErase(pq, pq->size - 1);}//从顺序表首位置删除数据void SeqListPophFront(SeqList* pq){ CheckNull(pq); SeqListErase(pq, 0);}//修改顺序表指定位置数据void SeqListModify(SeqList* pq){ CheckNull(pq); int pos; SEQTYPE x; printf("请输入修改的位置和新的数据,空格隔开:>"); scanf("%d %d", &pos, &x); if (pos < 0 && pos >= pq->size) { printf("请正确输入/n"); return; } pq->data[pos] = x; puts("修改成功");}//查找顺序表指定位置数据void SeqListFindPos(SeqList* pq){ CheckNull(pq); int pos; printf("请输入要查找数据的位置:>"); scanf("%d", &pos); if (pos < 0 && pos >= pq->size) { printf("请正确输入/n"); return; } for (int i = 0; i < pq->size; i++) { if (pq->data[i] == pq->data[pos]) { printf("查找位置的数据为:>%d/n", pq->data[pos]); break; } }}//查找所需数据是否存在顺序表中void SeqListFindData(SeqList* pq){ CheckNull(pq); SEQTYPE x; printf("请输入要查找的数据:>"); scanf("%d", &x); for (int i = 0; i < pq->size; i++) { if (pq->data[i] == x) { printf("所需查询数据存在,下标为:>%d/n", i); return; } } printf("找不到/n");}//排序顺序表void SeqListSort(SeqList* pq){ CheckNull(pq); int option = 0; printf("输入0为升序,1为降序:>"); scanf("%d", &option); for (int i = 0; i < pq->size - 1; i++) { for (int j = 0; j < pq->size - i - 1; j++) { if (pq->data[j] > pq->data[j + 1]) { SEQTYPE tmp = pq->data[j]; pq->data[j] = pq->data[j + 1]; pq->data[j + 1] = tmp; } } } if (option) { SeqListReverse(pq)
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/122330.html
阅读 3186·2021-11-23 09:51
阅读 1523·2021-11-22 09:34
阅读 2835·2021-10-27 14:15
阅读 2263·2021-10-12 10:17
阅读 1883·2021-10-12 10:12
阅读 944·2021-09-27 14:00
阅读 1994·2021-09-22 15:19
阅读 1029·2019-08-30 10:51