C语言实现顺序表
C语言实现顺序表代码
文件SeqList.cpp
1 #pragma warning(disable: 4715) 2 3 #include"SeqList.h" 4 void ShowSeqList(SeqList *pSeq) 5 { 6 assert(pSeq); 7 printf("size = %d \n",pSeq->size); 8 for(size_t i = 0; i < pSeq->size;i++) 9 { 10 printf("%d ", pSeq->array[i]); 11 } 12 printf("\n"); 13 } 14 15 void InitSeqList(SeqList *pSeq) 16 { 17 assert(pSeq); 18 memset(pSeq->array,0,sizeof(ElemType)*MAX_SIZE); 19 pSeq->size = 0; 20 } 21 22 void PushBack(SeqList *pSeq,const ElemType &x) 23 { 24 assert(pSeq); 25 if(pSeq->size >= MAX_SIZE) 26 { 27 printf("SeqList is Full\n"); 28 return; 29 } 30 pSeq->array[pSeq->size++] = x; 31 } 32 33 void PopBack(SeqList *pSeq) 34 { 35 assert(pSeq); 36 if(pSeq->size <= 0) 37 { 38 printf("SeqList is Empty\n"); 39 return; 40 } 41 pSeq->array[--pSeq->size] = 0; 42 } 43 44 void PushFront(SeqList *pSeq,const ElemType &x) 45 { 46 size_t begin = pSeq->size; 47 assert(pSeq); 48 if(pSeq->size >=MAX_SIZE) 49 { 50 printf("SeqList is Full\n"); 51 return; 52 } 53 for(;begin > 0; --begin) 54 { 55 pSeq->array[begin] = pSeq->array[begin-1]; 56 } 57 pSeq->array[0] = x; 58 pSeq->size++; 59 } 60 void PopFront(SeqList *pSeq) 61 { 62 size_t begin = 0; 63 assert(pSeq); 64 if(pSeq->size <= 0) 65 { 66 printf("SeqList is Empty\n"); 67 return; 68 } 69 for(;begin < pSeq->size-1; ++begin) 70 { 71 pSeq->array[begin] = pSeq->array[begin+1]; 72 } 73 pSeq->array[--pSeq->size] = 0; 74 } 75 76 void Erase(SeqList *pSeq, size_t pos) 77 { 78 assert(pSeq); 79 if(pos > pSeq->size) 80 { 81 printf("Position Error\n"); 82 return; 83 } 84 size_t begin = pos; 85 for(; begin < pSeq->size -1;++ begin) 86 { 87 pSeq->array[begin] = pSeq->array[begin+1]; 88 } 89 pSeq->array[--pSeq->size] = 0; 90 } 91 92 void Remove(SeqList *pSeq, const ElemType &x) 93 { 94 size_t begin = 0; 95 assert(pSeq); 96 for(; begin < pSeq->size; ++begin) 97 { 98 if(x == pSeq->array[begin]) 99 { 100 Erase(pSeq,begin); 101 return; 102 } 103 } 104 printf("No this elemData\n"); 105 } 106 107 void RemoveAll(SeqList *pSeq, const ElemType &x) 108 { 109 110 size_t begin = 0; 111 assert(pSeq); 112 for(; begin < pSeq->size; ++begin) 113 { 114 if(x == pSeq->array[begin]) 115 { 116 Erase(pSeq,begin); 117 } 118 } 119 } 120 121 //////////冒泡排序 122 void BubbSort(SeqList *s) 123 { 124 for(size_t i = 0; i < s->size-1;++i) 125 { 126 for(size_t j = 0; j < s->size-1-i; ++j) 127 { 128 if(s->array[j] > s->array[j+1]) 129 { 130 ElemType tmp = s->array[j]; 131 s->array[j] = s->array[j+1]; 132 s->array[j+1] = tmp; 133 } 134 } 135 } 136 }
文件SeqList.h
1 //顺序表简单实现 2 3 #ifndef _SEQLIST_H 4 #define _SEQLIST_H 5 6 #include<stdio.h> 7 #include<string.h> //for memcpy 8 #include<assert.h> //for assert 9 #include<malloc.h> //for malloc 10 11 #define MAX_SIZE 100 12 13 typedef int ElemType; 14 typedef struct SeqList 15 { 16 ElemType array[MAX_SIZE]; 17 size_t size; 18 }SeqList; 19 20 void ShowSeqList(SeqList *pSeq); 21 void InitSeqList(SeqList *pSeq); 22 void PushBack(SeqList *pSeq,const ElemType &x); 23 void PopBack(SeqList *pSeq); 24 void PushFront(SeqList *pSeq,const ElemType &x); 25 void PopFront(SeqList *pSeq); 26 void Erase(SeqList *pSeq, size_t pos); 27 void Remove(SeqList *pSeq, const ElemType &x); 28 void RemoveAll(SeqList *pSeq, const ElemType &x);
测试文件Main.cpp
1 #pragma once 2 #include<stdio.h> 3 #include "SeqList.h" 4 //顺序表示例 5 void TestForSeqList() 6 { 7 SeqList Seq; 8 InitSeqList(&Seq); 9 PushBack(&Seq,2); 10 PopBack(&Seq); 11 PushBack(&Seq,6); 12 PushFront(&Seq,4); 13 PushFront(&Seq,4); 14 PushBack(&Seq,2); 15 16 Erase(&Seq,44); 17 Remove(&Seq,44); 18 19 ShowSeqList(&Seq); 20 PushBack(&Seq,5); 21 PushBack(&Seq,7); 22 PushBack(&Seq,2); 23 ShowSeqList(&Seq); 24 BubbSort(&Seq); 25 ShowSeqList(&Seq); 26 }