摘要:創建一個結構體用于存放順序表相關數據有效數據個數容量初始化順序表插入元素插入到表頭插入到指定位置插入到尾部先檢查容量是否夠用如果空間滿了,擴容增
#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)
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/122330.html
閱讀 3187·2021-11-23 09:51
閱讀 1524·2021-11-22 09:34
閱讀 2836·2021-10-27 14:15
閱讀 2265·2021-10-12 10:17
閱讀 1884·2021-10-12 10:12
閱讀 946·2021-09-27 14:00
閱讀 1996·2021-09-22 15:19
閱讀 1032·2019-08-30 10:51