最全线性表函数(打印,初始化,后插氛围,后删,前插,前删,在某位中插入,从某位向后寻找,删除某位,删除某元素,删除表中全部某元素,冒泡排序,选择排序,二分查找)
//**************fun.h******************* #pragma once #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> #define MAX_SIZE 5 typedef int Data_Type; typedef struct SeqList { Data_Type array[MAX_SIZE]; size_t size; }SeqL, *Seqlist; void PrintSeqList(Seqlist pSeq);//输出线性表 void InitSeqList(Seqlist pSeq);//初始化表 void PushBack(Seqlist pSeq, const Data_Type x);//后插 void PopBack(Seqlist pSeq);//后删 void PushFront(Seqlist pSeq, const Data_Type x);//前插 void PopFront(Seqlist pSeq);//前删 void Insert(Seqlist pSeq, const size_t pos, const Data_Type x);//在 pos 位置插入元素x size_t Find(Seqlist pSeq, const size_t pos, const Data_Type x);//在 pos 位置向后找元素x void Erase(Seqlist pSeq, const size_t pos);//删除 pos 位置元素 void Remove(Seqlist pSeq, const Data_Type x);//删除 元素x void RemoveAll(Seqlist pSeq, const Data_Type x);//删除表中全部元素x void BubbleSort(Seqlist pSeq);//冒泡排序 void SeclectSort(Seqlist pSeq);//选择排序 //一次选出最大最小的数据分别放在两端 int BinarySearch(Seqlist pSeq, const Data_Type x);//二分查找 //************fun.c*************************** #include"fun.h" void PrintSeqList(Seqlist pSeq)//输出线性表 { assert(pSeq); if (pSeq->size <= 0) { printf("Seqlist if empty\n"); return; } size_t begin = 0; for (; begin < pSeq->size; begin++) { printf("%d ", pSeq->array[begin]); } printf("\n"); } void InitSeqList(Seqlist pSeq)//初始化表 { assert(pSeq); memset(pSeq, 0, sizeof(Data_Type)*MAX_SIZE); pSeq->size = 0; } void PushBack(Seqlist pSeq, const Data_Type 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 if empty\n"); return; } pSeq->array[pSeq->size - 1] = 0; --pSeq->size; } void PushFront(Seqlist pSeq, const Data_Type x)//前插 { assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("Seqlist is full\n"); return; } size_t begin = pSeq->size; for (; begin > 0; begin--) pSeq->array[begin] = pSeq->array[begin - 1]; pSeq->array[0] = x; pSeq->size++; } void PopFront(Seqlist pSeq)//前删 { assert(pSeq); if (pSeq->size <= 0) { printf("Seqlist is empty\n"); return; } size_t begin = 0; if (pSeq->size <= 0) { printf("Seqlist is empty\n"); return; } for (begin = 0; begin < pSeq->size - 1; begin++) pSeq->array[begin] = pSeq->array[begin + 1]; --pSeq->size; } void Insert(Seqlist pSeq, const size_t pos, const Data_Type x)//在 pos 位置插入元素x { assert(pSeq); assert(pos <= pSeq->size); if (pos == MAX_SIZE) { printf("Seqlist is full\n"); return; } size_t begin = pSeq->size; for (; begin>pos; begin--) pSeq->array[begin] = pSeq->array[begin - 1]; pSeq->array[pos] = x; pSeq->size++; } size_t Find(Seqlist pSeq, const size_t pos, const Data_Type x)//在 pos 位置向后找元素x { assert(pSeq); assert(pos < pSeq->size); size_t begin = pos; for (; begin < pSeq->size; begin++) if (pSeq->array[begin] == x) return begin; return -1; } void Erase(Seqlist pSeq, const size_t pos)//删除 pos 位置元素 { assert(pSeq); assert(pos < pSeq->size); size_t begin = pos; for (; begin < pSeq->size - 1; begin++) pSeq->array[begin] = pSeq->array[begin + 1]; pSeq->size--; } void Remove(Seqlist pSeq, const Data_Type x)//删除 元素x { assert(pSeq); size_t begin = 0, i = 0; for (; begin < pSeq->size - 1; begin++) if (pSeq->array[begin] == x) { for (i = begin; i < pSeq->size - 1; i++) pSeq->array[i] = pSeq->array[i + 1]; pSeq->size--; printf("Remove %d success\n", x); return; } printf("Remove %d fail\n", x); return; } void RemoveAll(Seqlist pSeq, const Data_Type x)//删除表中全部元素x { assert(pSeq); size_t begin = 0; size_t count = 0; for (begin = 0; begin < pSeq->size; begin++) { if (x == pSeq->array[begin]) count++; else pSeq->array[begin - count] = pSeq->array[begin]; } pSeq->size = pSeq->size - count; } void BubbleSort(Seqlist pSeq)//冒泡排序 { assert(pSeq); size_t i = 0, j = 0; int flag = -1; size_t begin = 0; Data_Type tmp; for (i = 0; i < pSeq->size - 1; i++) { flag = -1; for (begin = 0; begin < pSeq->size - i; begin++) if (pSeq->array[begin] > pSeq->array[begin + 1]) { tmp = pSeq->array[begin]; pSeq->array[begin] = pSeq->array[begin + 1]; pSeq->array[begin + 1] = tmp; flag = 1; } if (flag == -1) return; } } void SeclectSort(Seqlist pSeq)//选择排序 { //一次选出最大最小的数据分别放在两端 assert(pSeq); size_t i = 0, min = 0, max = 0, begin = 0; size_t flag_min = 0, flag_max = 0; Data_Type tmp = pSeq->array[0]; for (begin = 0; begin < pSeq->size; begin++) { flag_min = begin; flag_max = pSeq->size - begin - 1; min = flag_min; max = flag_max; for (i = begin; i < pSeq->size - begin; i++) { if (pSeq->array[flag_min]>pSeq->array[i]) min = i; if (pSeq->array[flag_max] < pSeq->array[i]) max = i; } tmp = pSeq->array[min]; pSeq->array[min] = pSeq->array[flag_min]; pSeq->array[flag_min] = tmp; tmp = pSeq->array[flag_max]; pSeq->array[flag_max] = pSeq->array[max]; pSeq->array[max] = tmp; } } int BinarySearch(Seqlist pSeq, const Data_Type x)//二分查找 { assert(pSeq); size_t left = 0, right = pSeq->size - 1; size_t begin = 0, mid = 0; while (left <= right) { mid = left + (right - left) / 2; if (pSeq->array[mid] == x) return mid; else if (pSeq->array[mid] > x) right = mid - 1; else left = mid + 1; } return -1; } //****************test.c***************** #include"fun.h" //测试函数 Test1~5() void Test1() //后插后删 { SeqL Seq; InitSeqList(&Seq); PushBack(&Seq, 1); PushBack(&Seq, 2); PushBack(&Seq, 3); PushBack(&Seq, 4); PushBack(&Seq, 5); PrintSeqList(&Seq); PushBack(&Seq, 6); PrintSeqList(&Seq); PopBack(&Seq); PopBack(&Seq); PopBack(&Seq); PopBack(&Seq); PrintSeqList(&Seq); PopBack(&Seq); PrintSeqList(&Seq); PopBack(&Seq); PrintSeqList(&Seq); } void Test2() //前插前删 { SeqL Seq; InitSeqList(&Seq); PushFront(&Seq, 1); PushFront(&Seq, 2); PushFront(&Seq, 3); PushFront(&Seq, 4); PushFront(&Seq, 5); PrintSeqList(&Seq); PushFront(&Seq, 6); PrintSeqList(&Seq); PopFront(&Seq); PrintSeqList(&Seq); PopFront(&Seq); PopFront(&Seq); PrintSeqList(&Seq); PopFront(&Seq); PopFront(&Seq); PopFront(&Seq); PopFront(&Seq); } void Test3() //Insert { SeqL Seq; InitSeqList(&Seq); PushFront(&Seq, 1); PushFront(&Seq, 2); PrintSeqList(&Seq); // Insert(&Seq, 3, 1); Insert(&Seq, 2, 1); PrintSeqList(&Seq); PushBack(&Seq, 2); PrintSeqList(&Seq); Insert(&Seq, 0, 4); PushBack(&Seq, 1); PrintSeqList(&Seq); Insert(&Seq, 5, 1); PrintSeqList(&Seq); printf("%d \n", Find(&Seq, 0, 1)); printf("%d \n", Find(&Seq, 4, 1)); printf("%d \n", Find(&Seq, 5, 1)); } void Test4()//Erase\Remove\RemoveAll { SeqL Seq; InitSeqList(&Seq); PushFront(&Seq, 1); PushFront(&Seq, 2); PushBack(&Seq, 3); PushBack(&Seq, 1); PushBack(&Seq, 2); PrintSeqList(&Seq); // Erase(&Seq, 3); Remove(&Seq, 2); PrintSeqList(&Seq); RemoveAll(&Seq, 1); PrintSeqList(&Seq); Remove(&Seq, 3); PrintSeqList(&Seq); Erase(&Seq, 0); PrintSeqList(&Seq); Erase(&Seq, 1); } void Test5() //BubbleSort\SeclectSort\BinarySearch { SeqL Seq; InitSeqList(&Seq); PushFront(&Seq, 1); PushFront(&Seq, 2); PushBack(&Seq, 3); PushBack(&Seq, 4); PushBack(&Seq, 0); PrintSeqList(&Seq); /*BubbleSort(&Seq); PrintSeqList(&Seq);*/ SeclectSort(&Seq); PrintSeqList(&Seq); printf("%d\n", BinarySearch(&Seq, 1)); printf("%d\n", BinarySearch(&Seq, 3)); printf("%d\n", BinarySearch(&Seq, 5)); } int main() { //Test1();//后插后删 //Test2();//前插前删 //Test3();//Insert\Find //Test4();//Erase\Remove\RemoveAll Test5();//BubbleSort\SeclectSort\BinarySearch system("pause"); return 0; }
最全线性表函数(打印,初始化,后插氛围,后删,前插,前删……)
原文:http://10739786.blog.51cto.com/10729786/1726521