标签:顺序表
SeqList.h//头文件 #ifndef SEQ_LIST #define SEQ_LIST #include <stdio.h> typedef int DataType; #define MAX_SIZE 10 typedef struct SeqList { DataType arry[MAX_SIZE]; size_t Size;//(1)数组中有效数组的个数(2)数组中有效元素的最后一个元素的下一个位置 }SeqList, *PSeqList; // 初始化顺序表 void InitSeqList(SeqList* seqList); // 在顺序表的尾部插入元素data void PushBack(SeqList* seqList, DataType data); // 将顺序表尾部的元素抛出 void PopBack(SeqList* seqList); // 顺序表的头部插入元素data void PushFront(SeqList* pSeqList, DataType data); // 顺序表的头部的元素删除 void PopFront(SeqList* pSeqList); // 顺序表中查找数据data,返回该元素在顺序表中的位置 int Find(SeqList* pSeqList, DataType data); // 在顺序表的ipos位置上插入元素data void Insert(SeqList* pSeqList, size_t pos, DataType data); // 删除顺序表pos位置上的元素 void Erase(SeqList* pSeqList, size_t pos); // 移除顺序表中的元素data void Remove(SeqList* pSeqList, DataType data); // 移除顺序表中的所有值为data的元素 void RemoveAll(SeqList* pSeqList, DataType data); // 选择排序 void SelectSort(SeqList* pSeqList); // 冒泡排序 void BorbbleSort(SeqList* pSeqList); // 查找已序顺序表中的元素data int BinarySearch(SeqList* pSeqList, DataType data); // 打印顺序表 void PrintSeqList(SeqList* pSeqList); #endif pSeqList//函数实现 #include "SeqList.h" #include <string.h> #include <assert.h> void InitSeqList(SeqList *seqList)//初始化顺序表 { assert(seqList); memset(seqList->arry, 0, sizeof(DataType)*MAX_SIZE); seqList->Size = 0; } void PushBack(SeqList* seqList, DataType data)//在顺序表的尾部插入元素data { assert(NULL != seqList); if (seqList->Size >= MAX_SIZE) { return; } seqList->arry[seqList->Size++] = data; } void PopBack(SeqList* seqList)// 将顺序表尾部的元素抛出 { assert(seqList != NULL); if (seqList->Size <= 0) { return; } seqList->Size--; } void PushFront(SeqList* pSeqList, DataType data)//顺序表的头部插入元素data { int iBegin = pSeqList->Size - 1; assert(pSeqList); if (pSeqList->Size >= MAX_SIZE) { return; } for (; iBegin >= 0; iBegin--) { pSeqList->arry[iBegin + 1] = pSeqList->arry[iBegin]; } pSeqList->arry[0] = data; pSeqList->Size++; } void PopFront(SeqList* pSeqList)//顺序表的头部的元素删除 { int iBegin = 1; assert(pSeqList); if (pSeqList->Size == 0) { return; } for (; iBegin < pSeqList->Size; ++iBegin) { pSeqList->arry[iBegin - 1] = pSeqList->arry[iBegin]; } pSeqList->Size--; } int Find(SeqList* pSeqList, DataType data)//顺序表中查找数据data,返回该元素在顺序表中的位置 { int iBegin = 0; assert(pSeqList); for (; iBegin < pSeqList->Size; ++iBegin) { if (data == pSeqList->arry[iBegin]) { return iBegin; } /*return -1;*/ } return -1; } void Insert(SeqList* pSeqList, size_t pos, DataType data)//在顺序表的ipos位置上插入元素data { int iBegin = pSeqList->Size; if (pSeqList == NULL && pos > pSeqList->Size) { return; } // 边界 if (pSeqList->Size >= MAX_SIZE) { return; } //0 1 2 3 for (; iBegin > pos; iBegin--) { pSeqList->arry[iBegin] = pSeqList->arry[iBegin - 1]; } pSeqList->arry[pos] = data; pSeqList->Size++; } void Erase(SeqList* pSeqList, size_t pos)//删除顺序表pos位置上的元素 { int iBegin = pos; //0 1 2 3 4 5 isze = 6 if (pSeqList == NULL || pos > pSeqList->Size - 1) { return; } if (pSeqList->Size <= 0) { return; } for (; iBegin < pSeqList->Size - 1; ++iBegin) { pSeqList->arry[iBegin] = pSeqList->arry[iBegin + 1]; } pSeqList->Size--; } void Remove(SeqList* pSeqList, DataType data)// 移除顺序表中的元素data { int ipos = 0; if (pSeqList == NULL) { return; } ipos = Find(pSeqList, data); if (-1 == ipos) { return; } Erase(pSeqList, ipos); } void RemoveAll(SeqList* pSeqList, DataType data)// 移除顺序表中的所有值为data的元素 { int iBegin = 0; int iCount = 0; if (pSeqList == NULL) { return; } // 1 2 3 2 4 5 2 7 iCount++ for (; iBegin < pSeqList->Size; ++iBegin) { if (pSeqList->arry[iBegin] == data) { iCount++; } else { pSeqList->arry[iBegin - iCount] = pSeqList->arry[iBegin]; } } pSeqList->Size -= iCount; } void SelectSort(SeqList* pSeqList)// 选择排序 { int i = 0, j = 0; int iMaxPos = 0; int iMinPos = 0; DataType temp; for (; i < pSeqList->Size / 2; ++i) { iMaxPos = i; iMinPos = i; for (j = 1; j < pSeqList->Size - i; j++) { if (pSeqList->arry[iMaxPos] < pSeqList->arry[j]) { iMaxPos = j; } if (pSeqList->arry[iMinPos] > pSeqList->arry[j]) { iMinPos = j; } } if (iMaxPos != i) { temp = pSeqList->arry[iMaxPos]; pSeqList->arry[iMaxPos] = pSeqList->arry[pSeqList->Size - i - 1]; pSeqList->arry[pSeqList->Size - i - 1] = temp; } if (iMinPos != i) { temp = pSeqList->arry[iMinPos]; pSeqList->arry[iMinPos] = pSeqList->arry[i]; pSeqList->arry[i] = temp; } } } void BorbbleSort(SeqList* pSeqList)// 冒泡排序 { int iCount = 0; int iBegin = 0; int iFlag = 0; if (NULL == pSeqList) { return; } for (; iCount <pSeqList->Size - 1; iCount++) { iFlag = 1; for (iBegin = 0; iBegin < pSeqList->Size - iCount - 1; ++iBegin) { if (pSeqList->arry[iBegin] > pSeqList->arry[iBegin + 1]) { DataType temp = pSeqList->arry[iBegin]; pSeqList->arry[iBegin] = pSeqList->arry[iBegin + 1]; pSeqList->arry[iBegin + 1] = temp; iFlag = 0; } } if (iFlag) { return; } } } int BinarySearch(SeqList* pSeqList, DataType data)// 查找已序顺序表中的元素data { int iLeft = 0; int iRight = pSeqList->Size;// [) //int iMid = (iLeft + iRight)/2; int iMid = 0; DataType temp; if (NULL == pSeqList) { return; } // [0, 4] while (iLeft < iRight) { iMid = iLeft + (iRight - iLeft) / 2; temp = pSeqList->arry[iMid]; if (temp < data) { iLeft = iMid; } else if (temp > data) { iRight = iMid - 1; } else { return iMid; } } return -1; } void PrintSeqList(SeqList* pSeqList)//打印顺序表 { int iBegin = 0; assert(pSeqList); for (; iBegin < pSeqList->Size; ++iBegin) { printf("%d ", pSeqList->arry[iBegin]); } printf("\n"); } Test.c//测试函数 #include <stdio.h> #include "SeqList.h" //InitSeqList/PushBack/PopBack void FunTest() { SeqList seqList; InitSeqList(&seqList); PushBack(&seqList, 1); PushBack(&seqList, 2); PushBack(&seqList, 3); PushBack(&seqList, 4); PushBack(&seqList, 5); PushBack(&seqList, 6); PopBack(&seqList); PopBack(&seqList); } //PushFront/PopFront/Find void FunTest1() { SeqList seqList; InitSeqList(&seqList); PushBack(&seqList, 1); PushBack(&seqList, 2); PushBack(&seqList, 3); PushBack(&seqList, 4); PushBack(&seqList, 5); PushFront(&seqList, 6); PushFront(&seqList, 7); PushFront(&seqList, 8); PushFront(&seqList, 9); PushFront(&seqList, 10); /*PopFront(&seqList); PopFront(&seqList); PopFront(&seqList); PopFront(&seqList); PopFront(&seqList); PopFront(&seqList); PopFront(&seqList); PopFront(&seqList); PopFront(&seqList); PopFront(&seqList);*/ if (-1 == Find(&seqList, 6)) { printf("Not In\n"); } else { printf("In\n"); } if (-1 == Find(&seqList, 16)) { printf("Not In\n"); } else { printf("In\n"); } //PrintSeqList(&seqList); } //Insert/Erase/Remove/RemoveAll FunTest2() { SeqList seqList; InitSeqList(&seqList); PushBack(&seqList, 2); PushBack(&seqList, 2); PushBack(&seqList, 2); PushBack(&seqList, 2); PushBack(&seqList, 2); PushBack(&seqList, 2); PushBack(&seqList, 2); Insert(&seqList, 5, 12); PushBack(&seqList, 2); PushBack(&seqList, 2); Erase(&seqList, 6); //Remove(&seqList, 3); //RemoveAll(&seqList, 2); PrintSeqList(&seqList); } //SelectSort/BorbbleSort/BinarySearch FunTest3() { SeqList seqList; InitSeqList(&seqList); PushBack(&seqList, 1); PushBack(&seqList, 3); PushBack(&seqList, 2); PushBack(&seqList, 5); PushBack(&seqList, 4); PushBack(&seqList, 6); BorbbleSort(&seqList); PrintSeqList(&seqList); } int main() { //FunTest(); //FunTest1(); //FunTest2(); FunTest3(); return 0; }
本文出自 “11275984” 博客,请务必保留此出处http://11285984.blog.51cto.com/11275984/1754085
标签:顺序表
原文地址:http://11285984.blog.51cto.com/11275984/1754085