您好,登錄后才能下訂單哦!
SeqList 聲明文件 #pragma once #define MAX_SIZE 5 typedef int DataType; typedef struct SeqList { DataType array[MAX_SIZE]; size_t size; }SeqList; void PrintSeqList(SeqList* pSeq); void InitSeqList(SeqList* pSeq);//初始化 void PushBack(SeqList* pSeq, DataType x);//尾插 void PopBack(SeqList* pSeq);//尾刪 void PushFront(SeqList* pSeq, DataType x);//頭插 void PopFront(SeqList* pSeq); void Insert(SeqList* pSeq, size_t pos, DataType x);//插入 int Find(SeqList* pSeq, size_t pos, DataType x);//查找 void Erase(SeqList* pSeq, size_t pos);//刪除 int Remove(SeqList* pSeq, DataType x); void RemoveAll(SeqList* pSeq, DataType x);//去重復 void Swap(DataType* left, DataType* right); void BubbleSort(SeqList* pSeq);//冒泡 void SelectSort(SeqList* pSeq);//void SelectSort_OP(SeqList* pSeq)//選擇 int BinarySearch(SeqList* pSeq, DataType x);//二分查找 SeqList實現文件 #include"SeqList.h" #include<assert.h> #include<iostream> #include<string.h> using namespace std; void InitSeqList(SeqList* pSeq) { memset(pSeq->array, 0, sizeof(DataType)*MAX_SIZE); pSeq->size = 0; } void PushBack(SeqList* pSeq, DataType x) { assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } pSeq->array[pSeq->size] = x; pSeq->size++; } void PopBack(SeqList* pSeq) { assert(pSeq); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); return; } pSeq->size--; } void PushFront(SeqList* pSeq, DataType x) { int begin = pSeq->size-1; assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } for (begin; begin >=0; begin--) { pSeq->array[begin+1] = pSeq->array[begin]; } pSeq->array[0] = x; pSeq->size++; } void PopFront(SeqList* pSeq) { int begin =0; assert(pSeq); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); } for (begin; begin < pSeq->size; begin++) { pSeq->array[begin] = pSeq->array[begin+1]; } pSeq->size--; } void Insert(SeqList* pSeq, size_t pos, DataType x)//插入位置按數組下標順序 { int begin = pSeq->size; assert(pSeq); assert(pos <= pSeq->size); if (pSeq->size >= MAX_SIZE) { printf("SeqList is full\n"); return; } for (begin; begin > pos; begin--) { pSeq->array[begin] = pSeq->array[begin-1]; } pSeq->array[pos] = x; pSeq->size++; } int Find(SeqList* pSeq, size_t pos, DataType x)//查找返回數組下標位置 { int i = pos; assert(pSeq); assert(pos <= pSeq->size); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); } for (i; i < pSeq->size; i++) { if (pSeq->array[i] == x) { return i; } } return -1; } void Erase(SeqList* pSeq, size_t pos) { assert(pSeq); assert(pos <= pSeq->size); if (pSeq->size <= 0) { printf("SeqList is Empty\n"); return; } for (pos; pos < pSeq->size; pos++) { pSeq->array[pos] = pSeq->array[pos +1]; } pSeq->size--; } void PrintSeqList(SeqList* pSeq) { int i = 0; assert(pSeq); for (; i < pSeq->size; i++) { printf("%d ", pSeq->array[i]); } cout << endl; } 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 begin = 0; assert(pSeq); for (; begin < pSeq->size; begin++) { if (pSeq->array[begin] == x) { count++; } else { pSeq->array[begin - count] = pSeq->array[begin]; } } pSeq ->size -= count; } void Swap(DataType* left, DataType* right) { DataType tmp = *left; *left = *right; *right = tmp; } void BubbleSort(SeqList* pSeq)//冒泡排序 { assert(pSeq); int first = 0; int second = 0; for (first = 0; first < pSeq->size - 1; first++) { //int Flag = 0; for (second = 1; second < pSeq->size - first; second++) { if (pSeq->array[second - 1] > pSeq->array[second]) { Swap(&pSeq->array[second - 1], &pSeq->array[second]); //Flag = 1; } } /*if (Flag == 0) { return; }*/ } } void SelectSort(SeqList* pSeq)//快速排序 { assert(pSeq); int i, j, max; for (j = pSeq->size -1; j >= 0; j--) { max = j; for (i = j-1; i >= 0; i--) { if (pSeq->array[max] < pSeq->array[i]) { max = i; } } Swap(&pSeq->array[max], &pSeq->array[j]); } } void SelectSort_OP(SeqList* pSeq)//快排優化 { assert(pSeq); int i, min, max; int left = 0; int right = pSeq->size - 1; while (left < right) { for (i = left; i <= right; i++) { min = left; max = right; if (pSeq->array[min] > pSeq->array[i]) { Swap(&pSeq->array[left], &pSeq->array[i]); } if (pSeq->array[max] < pSeq->array[i]) { Swap(&pSeq->array[max], &pSeq->array[i]); } } left++; right--; } } int BinarySearch(SeqList* pSeq, DataType x)//二分查找 { int left = 0; int right = pSeq->size - 1; while (left < right) { int mid = left + (right - left) / 2; if (pSeq->array[mid] > x) { right = mid - 1; } else if (pSeq->array[mid] < x) { left = mid + 1; } else { return mid; } } return -1; } 測試文件 int main() { SeqList seqlist; InitSeqList(&seqlist); /*PushBack(&seqlist, 1); PushBack(&seqlist, 2); PushBack(&seqlist, 3); PushBack(&seqlist, 4); PrintSeqList(&seqlist); PopBack(&seqlist); PopBack(&seqlist); PopBack(&seqlist);*/ PushFront(&seqlist, 2); PushFront(&seqlist, 1); PushFront(&seqlist, 4); PushFront(&seqlist, 3); PushFront(&seqlist, 5); PrintSeqList(&seqlist); int key = BinarySearch(&seqlist, 4); printf("%d ", key); cout << key << endl; //BubbleSort(&seqlist); //SelectSort_OP(&seqlist); PrintSeqList(&seqlist); //RemoveAll(&seqlist, 3); /*Erase(&seqlist, 0); PrintSeqList(&seqlist); Erase(&seqlist, 0); Erase(&seqlist, 0); Erase(&seqlist, 0); PrintSeqList(&seqlist*/ //int value = Remove(&seqlist, 4); //printf("%d\n", value); //Insert(&seqlist, 0, 1); //PrintSeqList(&seqlist); /*PopFront(&seqlist); PopFront(&seqlist); PrintSeqList(&seqlist); PopFront(&seqlist); PopFront(&seqlist); PopFront(&seqlist); PopFront(&seqlist);*/ system("pause"); return 0; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。