順序表是在計(jì)算機(jī)內(nèi)存中以數(shù)組的形式保存的線性表,是指用一組地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線性結(jié)構(gòu)。線性表采用順序存儲(chǔ)的方式存儲(chǔ)就稱之為順序表。順序表是將表中的結(jié)點(diǎn)依次存放在計(jì)算機(jī)內(nèi)存中一組地址連續(xù)的存儲(chǔ)單元中。 順序表的存儲(chǔ)特點(diǎn)是:只要確定了起始位置,表中任一元素地址都可以求出。 在c中實(shí)現(xiàn)順序表時(shí),由于函數(shù)較多,所以把函數(shù)的實(shí)現(xiàn)放在頭文件中,在主函數(shù)中進(jìn)行單元函數(shù)測(cè)試。 SequenceList_Static.h #ifndef __SEQUENCELIST_STATIC_H__ #define __SEQUENCELIST_STATIC_H__ #include晉城網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)建站從2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。#include #include #include typedef int DataType;//用typedef有利于順序表存儲(chǔ)類型的修改 #define MAX 100// 利用宏定義使得順序表存儲(chǔ)容量修改方便 typedef struct SeqList { DataType arr[MAX]; int size; }SeqList, *pSeqList; //順序表的基本操作 void InitSeqList(pSeqList pSeq); void PrintSeqList(SeqList Seq); void PushBack(pSeqList pSeq, DataType x); void PopBack(pSeqList pSeq); void PushFront(pSeqList pSeq, DataType x); void PopFront(pSeqList pSeq); void Insert(pSeqList pSeq,int pos,DataType x); int Find(SeqList Seq,DataType x); void Remove(pSeqList pSeq,DataType x); void RemoveAll(pSeqList pSeq,DataType x); void ReverseList(pSeqList pSeq); void SortList(pSeqList pSeq); int BinarySearch(SeqList Seq,DataType x); //順序表的初始化 void InitSeqList(pSeqList pSeq) { assert(pSeq); memset(pSeq->arr,0,sizeof(pSeq->arr)); pSeq->size = 0; } //為了方便查看對(duì)順序表進(jìn)行打印 void PrintSeqList(SeqList Seq) { int i = 0; for(i = 0; i < Seq.size; i++) { printf("%d\t",Seq.arr[i]); } printf("over\n"); } //存放數(shù)據(jù) void PushBack(pSeqList pSeq, DataType x) { assert(pSeq); if(pSeq->size >= MAX)//順序表的存放都應(yīng)該先判斷順序表是否已滿 { printf("sequence list is full\n"); } pSeq->arr[pSeq->size++] = x; } void PopBack(pSeqList pSeq) { assert(pSeq); if(pSeq->size == 0) { printf("The sequencelist is empty\n"); } pSeq->size--; } void PushFront(pSeqList pSeq, DataType x) { int i = 0; assert(pSeq); if(pSeq->size == MAX) { printf("sequence list is full\n"); return; } for(i = pSeq->size; i > 0; i--) { pSeq->arr[i] = pSeq->arr[i-1]; } pSeq->arr[0] = x; pSeq->size++; } void PopFront(pSeqList pSeq) { int i = 0; assert(pSeq); if(pSeq->size == 0) { printf("The sequencelist is empty\n"); return; } for(i = 0; i < pSeq->size-1; i++) { pSeq->arr[i] = pSeq->arr[i+1]; } pSeq->size--; } void Insert(pSeqList pSeq,int pos,DataType x) { int i = 0; assert(pSeq); //插入的位置應(yīng)該合法 assert((pos size) && (pos >= 0)); if(pSeq->size == MAX) { printf("The sequence list is full\n"); return; } for(i = pSeq->size; i>pos; i--) { pSeq->arr[i] = pSeq->arr[i-1]; } pSeq->arr[pos] = x; pSeq->size++; } int Find(SeqList Seq,DataType x) { int i = 0; for(i = 0; i size; i++) { pSeq->arr[i] = pSeq->arr[i+1]; } } pSeq->size--; } void RemoveAll(pSeqList pSeq,DataType x) { int i = 0; int j = 0; assert(pSeq); for(i =0 ; i size; i++) { if(pSeq->arr[i] == x) { for(j = i; j size; j++) { pSeq->arr[j] = pSeq->arr[j+1]; } pSeq->size--; } } } void ReverseList(pSeqList pSeq) { int start = 0; int end = pSeq->size-1; DataType tmp = 0; while(start < end) { tmp = pSeq->arr[start]; pSeq->arr[start] = pSeq->arr[end]; pSeq->arr[end] = tmp; start++; end--; } } //冒泡排序 void SortList(pSeqList pSeq) { int i = 0; int j = 0; assert(pSeq); for(i = 0; i size-1; i++) { for(j = 0; j size-1-i; j++) { if(pSeq->arr[j] > pSeq->arr[j+1]) { DataType tmp = pSeq->arr[j]; pSeq->arr[j] = pSeq->arr[j+1]; pSeq->arr[j+1] = tmp; } } } } int BinarySearch(SeqList Seq,DataType x) { int left = 0; int right = Seq.size-1; while(left <= right)//應(yīng)該注意邊界條件 { int mid = left-((left - right)>>1); if(Seq.arr[mid] > x) { right = mid - 1; } else if(Seq.arr[mid] == x) { return mid; } else { left = mid + 1; } } return -1; } #endif//__SEQUENCELIST_STATIC_H__ 以下是對(duì)函數(shù)的測(cè)試: test.c #include "SequenceList_Static.h" void Test1() { SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,1); PushBack(&myseqlist,2); PushBack(&myseqlist,3); PrintSeqList(myseqlist); } void Test2() { SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,1); PushBack(&myseqlist,2); PushBack(&myseqlist,3); PrintSeqList(myseqlist); PopBack(&myseqlist); PrintSeqList(myseqlist); } void Test3() { SeqList myseqlist; InitSeqList(&myseqlist); PushFront(&myseqlist,3); PushFront(&myseqlist,2); PushFront(&myseqlist,1); PushFront(&myseqlist,0); PrintSeqList(myseqlist); } void Test4() { SeqList myseqlist; InitSeqList(&myseqlist); PushFront(&myseqlist,3); PushFront(&myseqlist,2); PushFront(&myseqlist,1); PushFront(&myseqlist,0); PrintSeqList(myseqlist); PopFront(&myseqlist); PopFront(&myseqlist); PopFront(&myseqlist); PopFront(&myseqlist); PopFront(&myseqlist); PrintSeqList(myseqlist); } void Test5() { SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,1); PushBack(&myseqlist,2); PushBack(&myseqlist,3); PrintSeqList(myseqlist); Insert(&myseqlist,0,0); PrintSeqList(myseqlist); } void Test6() { SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,1); PushBack(&myseqlist,2); PushBack(&myseqlist,3); PrintSeqList(myseqlist); Remove(&myseqlist,1); PrintSeqList(myseqlist); } void Test7() { SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,1); PushBack(&myseqlist,2); PushBack(&myseqlist,3); PrintSeqList(myseqlist); ReverseList(&myseqlist); PrintSeqList(myseqlist); } void Test8() { SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,2); PushBack(&myseqlist,5); PushBack(&myseqlist,1); PushBack(&myseqlist,3); PushBack(&myseqlist,4); PrintSeqList(myseqlist); SortList(&myseqlist); PrintSeqList(myseqlist); } void Test9() { int ret = 0; SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,1); PushBack(&myseqlist,2); PushBack(&myseqlist,3); PushBack(&myseqlist,4); PushBack(&myseqlist,5); PrintSeqList(myseqlist); ret = BinarySearch(myseqlist,5); printf("%d\n",ret); } void Test10() { SeqList myseqlist; InitSeqList(&myseqlist); PushBack(&myseqlist,1); PushBack(&myseqlist,2); PushBack(&myseqlist,3); PushBack(&myseqlist,1); PushBack(&myseqlist,5); PrintSeqList(myseqlist); RemoveAll(&myseqlist,1); PrintSeqList(myseqlist); } int main() { Test10(); system("pause"); return 0; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。