您好,登錄后才能下訂單哦!
順序表是在計算機內存中以數組的形式保存的線性表,是指用一組地址連續的存儲單元依次存儲數據元素的線性結構。線性表采用順序存儲的方式存儲就稱之為順序表。順序表是將表中的結點依次存放在計算機內存中一組地址連續的存儲單元中。 順序表的存儲特點是:只要確定了起始位置,表中任一元素地址都可以求出。 在c中實現順序表時,由于函數較多,所以把函數的實現放在頭文件中,在主函數中進行單元函數測試。 SequenceList_Static.h #ifndef __SEQUENCELIST_STATIC_H__ #define __SEQUENCELIST_STATIC_H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> typedef int DataType;//用typedef有利于順序表存儲類型的修改 #define MAX 100// 利用宏定義使得順序表存儲容量修改方便 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; } //為了方便查看對順序表進行打印 void PrintSeqList(SeqList Seq) { int i = 0; for(i = 0; i < Seq.size; i++) { printf("%d\t",Seq.arr[i]); } printf("over\n"); } //存放數據 void PushBack(pSeqList pSeq, DataType x) { assert(pSeq); if(pSeq->size >= MAX)//順序表的存放都應該先判斷順序表是否已滿 { 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); //插入的位置應該合法 assert((pos<pSeq->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<Seq.size; i++) { if(Seq.arr[i] == x) { return i; } return -1; } } void Remove(pSeqList pSeq,DataType x) { int pos = Find(*pSeq,x); int i = 0; assert(pSeq); if(pos != -1) { for(i = pos; i<pSeq->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<pSeq->size; i++) { if(pSeq->arr[i] == x) { for(j = i; j<pSeq->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<pSeq->size-1; i++) { for(j = 0; j<pSeq->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)//應該注意邊界條件 { 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__ 以下是對函數的測試: 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; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。