顺序表 c语言的顺序表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顺序表 c语言的顺序表相关的知识,希望对你有一定的参考价值。
顺序表:一段地址连续的存储单元依次存储数据元素的线性结构。
静态存储的结构:
#define MAX_SIZE 5 typedef int DataType; typedef struct SeqList { DataType array[MAX_SIZE]; size_t size; }SeqList;
///////////// 顺序表的 增 删 查 //////////////////
void InitSeqList(SeqList* pSeq) //初始化 { assert(pSeq); memset(pSeq->array, 0, sizeof(DataType)*MAX_SIZE); pSeq->size = 0; } void PrintSeqList(SeqList* pSeq) { int i = 0; assert(pSeq); for (i = 0; i <(int)pSeq->size; ++i) { printf("%d->", pSeq->array[i]); } printf("\n"); } void PushBack(SeqList* pSeq, DataType x) //尾插 { assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } pSeq->array[pSeq->size++] = x; } void PopBack(SeqList* pSeq) //尾删 { assert(pSeq); if (pSeq->size <= 0) { printf("Seqlist is empty\n"); } pSeq->array[--pSeq->size] = 0; } void PushFront(SeqList* pSeq, DataType x) //头插 { int i = 0; assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } for (i = pSeq->size - 1; i >= 0; i--) { pSeq->array[i + 1] = pSeq->array[i]; } pSeq->array[0] = x; ++pSeq->size; } void PopFront(SeqList* pSeq) //头删 { int i = 0; assert(pSeq); if (pSeq->size <= 0) { printf("SeqList is empty\n"); return; } for (i = 1; i <(int)pSeq->size; i++) { pSeq->array[i - 1] = pSeq->array[i]; } --pSeq->size; } void Insert(SeqList* pSeq, size_t pos, DataType x) //增 { int i = 0; assert(pSeq); assert(pos <= pSeq->size); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } for (i = pSeq->size; i > (int)pos; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[pos] = x; ++pSeq->size; } int Find(SeqList* pSeq, size_t pos, DataType x) //查(从顺序表pSeq的第pos个位置开始查x) { int i = pos; assert(pSeq); for (; i <(int)pSeq->size; ++i) { if (pSeq->array[i] == x) { return i; } } return -1; } void Erase(SeqList* pSeq, size_t pos) //删 { int i = 0; assert(pSeq); assert(pos < pSeq->size); if (pSeq->size <= 0) { printf("SeqList is empty\n"); return; } for (i = pos + 1; i <(int)pSeq->size; i++) { pSeq->array[i - 1] = pSeq->array[i]; } --pSeq->size; } int Remove(SeqList* pSeq, DataType x) { int pos; assert(pSeq); pos = Find(pSeq, 0, x); if (pos != -1) { Erase(pSeq, pos); } return pos; } void RemoveAll(SeqList* pSeq, DataType x) //清空 { int count = 0; int i = 0; assert(pSeq); for (i = 0; i <(int)pSeq->size; i++) { if (pSeq->array[i] == x) { count++; } else { pSeq->array[i - count] = pSeq->array[i]; } } pSeq->size -= count; }
///////////////// 顺序表的几种排序 //////////////////////
void Swap(DataType* left, DataType* right) //交换 { DataType tmp = *left; *left = *right; *right = tmp; } //冒泡排序 void BubbleSort(SeqList* pSeq) { int i = 0, j = 0; assert(pSeq); for (j = 0; j <(int)pSeq->size - 1; ++j) { int exchange = 0; for (i = 1; i < (int)pSeq->size - j; ++i) { if (pSeq->array[i - 1] > pSeq->array[i]) { Swap(&pSeq->array[i - 1], &pSeq->array[i]); exchange = 1; } } if (exchange == 0) { return; } } } // 选择排序 void SeclectSort(SeqList* pSeq) { int i, j; int min; assert(pSeq); for (j = 0; j <(int)pSeq->size - 1; ++j) { min = j; for (i = j + 1; i <(int)pSeq->size; ++i) { if ((int)pSeq->array[min] >(int) pSeq->array[i]) { min = i; } } Swap(&pSeq->array[min], &pSeq->array[j]); } } //选择排序的优化 void SeclectSort_OP(SeqList* pSeq) { int left = 0, right = pSeq->size - 1; int min = 0, max = 0, i = 0; assert(pSeq); while (left < right) { for (i = left; i <= right; ++i) { if (pSeq->array[i] < pSeq->array[left]) { Swap(&pSeq->array[i], &pSeq->array[left]); } if (pSeq->array[i] > pSeq->array[right]) { Swap(&pSeq->array[i], &pSeq->array[right]); } } ++left; --right; } } //二分查找 数组应按顺序排列 int BinarySearch(SeqList* pSeq, DataType x) { int left = 0; int right = pSeq->size - 1; assert(pSeq); while (left <= right) { int mid = right - (right - left) / 2; if (x > pSeq->array[mid]) { left = mid + 1; } else if (x < pSeq->array[mid]) { right = mid - 1; } else { return mid; } } return -1; }
欢迎大家补充顺序表的更多知识。
本文出自 “娜些维度的雪” 博客,请务必保留此出处http://1536262434.blog.51cto.com/10731069/1753701
以上是关于顺序表 c语言的顺序表的主要内容,如果未能解决你的问题,请参考以下文章