資料結構 順序表的簡單排列
阿新 • • 發佈:2019-02-13
初始化
列印順序表
尾插一個元素
只刪除第一個指定元素
刪除所有指定元素
判斷順序表是否為空 為空返回1 不為空返回0
氣泡排序
#pragma once #include <stdio.h> #define SeqListMax 1000 #define TITLE printf("\n==============%s==============\n",__FUNCTION__); typedef char SeqType; typedef struct SeqList{ SeqType data[SeqListMax]; size_t size; }SeqList; void SeqListInit(SeqList* seq); //初始化 void SeqListPrint(SeqList *seq);//列印順序表 void SeqListPushBack(SeqList* seq, SeqType value); //尾插一個元素 void SeqListRemove(SeqList* seq, SeqType value); //只刪除第一個指定元素 void SeqListRemoveAll(SeqList* seq, SeqType value);//刪除所有指定元素 void SeqListBubbleSort(SeqList* seq); //氣泡排序 size_t SeqListSize(SeqList* seq); //判定順序表是否為空 //void SeqListBubbleSortEx(SeqList* seq);//選擇排序
#include "seqlist.h" void SeqListInit(SeqList* seq) { if(seq==NULL) return; seq->size=0; } void SeqListPrint(SeqList* seq) { if(seq==NULL) return; size_t i = 0; for(;i<seq->size;i++) { printf("[%c] ",seq->data[i]); } printf("\n"); } void SeqListPushBack(SeqList* seq, SeqType value) { if(seq==NULL) return; if(seq->size>=SeqListMax) { printf("順序表已滿!\n"); return; } seq->data[seq->size]=value; seq->size++; } void SeqListRemove(SeqList* seq, SeqType value) { if(seq==NULL) return; if(seq->size==0) //順序表為空 return; int i = 0; for(;i<seq->size;i++) { if(seq->data[i]==value) break; } for(;i<seq->size;i++) { seq->data[i]=seq->data[i+1]; } } void SeqListRemoveAll(SeqList* seq, SeqType value) { if(seq==NULL) return; if(seq->size==0) //空順序表 return; int i = 0; int flag = 0; for(;i<seq->size;i++) { if(seq->data[i]==value) flag++; } for(i=0;i<flag;i++) { SeqListRemove(seq,value); } } size_t SeqListSize(SeqList* seq) { if(seq==NULL) return; if(seq->size==0) return 1; else return 0; } void swap(SeqType* a,SeqType* b) { SeqType x; x = *a; *a = *b; *b = x; } void SeqListBubbleSort(SeqList* seq) { if(seq==NULL) return; int i = 0; for(;i<seq->size;i++) { int j = 0; for(;j<seq->size-1-i;j++) { if(seq->data[j]>seq->data[j+1]) swap(&seq->data[j],&seq->data[j+1]); } } } //////////////////////////////////////////// // 以下是測試程式碼 // /////////////////////////////////////////// void TestSeqListInit() { TITLE; SeqList seq; SeqListInit(&seq); } void TestSeqListPushBack() { TITLE; SeqList seq; SeqListInit(&seq); SeqListPushBack(&seq,'a'); SeqListPushBack(&seq,'b'); SeqListPushBack(&seq,'c'); SeqListPushBack(&seq,'d'); SeqListPrint(&seq); } void TestSeqListRemove() { TITLE; SeqList seq; SeqListInit(&seq); SeqListPushBack(&seq,'a'); SeqListPushBack(&seq,'b'); SeqListPushBack(&seq,'b'); SeqListPushBack(&seq,'c'); SeqListPushBack(&seq,'d'); SeqListRemove(&seq,'b'); SeqListPrint(&seq); } void TestSeqListRemoveAll() { TITLE; SeqList seq; SeqListInit(&seq); SeqListPushBack(&seq,'a'); SeqListPushBack(&seq,'b'); SeqListPushBack(&seq,'b'); SeqListPushBack(&seq,'c'); SeqListPushBack(&seq,'d'); SeqListRemoveAll(&seq,'b'); SeqListPrint(&seq); } void TestSeqListSize() { TITLE; int i = 0; SeqList seq; SeqListInit(&seq); SeqListPushBack(&seq,'a'); SeqListPushBack(&seq,'b'); SeqListPushBack(&seq,'c'); SeqListPushBack(&seq,'d'); i = SeqListSize(&seq); printf("[%d]\n",i); SeqListInit(&seq); i = SeqListSize(&seq); printf("[%d]\n",i); } TestSeqListBubbleSort() { TITLE; SeqList seq; SeqListInit(&seq); SeqListPushBack(&seq,'d'); SeqListPushBack(&seq,'b'); SeqListPushBack(&seq,'c'); SeqListPushBack(&seq,'a'); SeqListPrint(&seq); SeqListBubbleSort(&seq); SeqListPrint(&seq); } int main() { TestSeqListInit(); TestSeqListPushBack(); TestSeqListRemove(); TestSeqListRemoveAll(); TestSeqListSize(); TestSeqListBubbleSort(); return 0; }