标签:
seqlist.h
<span style="font-size:24px;">#ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include<stdio.h> #include<assert.h> #include<string.h> #define MAX_SIZE 1000 typedef int DataType; typedef struct seqlist { DataType array[MAX_SIZE]; size_t size; }seqlist; typedef enum tag { one,//删去第一个 more,//全部都删去 }Tag; void Init(seqlist* pSeq); void PushBack(seqlist* pSeq, DataType x); void PushFront(seqlist* pSeq, DataType x); void Print(seqlist* pSeq); void PopBack(seqlist* pSeq); void PopFront(seqlist*pSeqsssss); void Insert(seqlist* pSeq, size_t index, DataType x); void Modify(seqlist* pSeq, size_t index, DataType x); void removed(seqlist* pSeq, size_t index); void Erase(seqlist* pSeq, DataType x, Tag all); int Find(seqlist* pSeq, DataType x, size_t index); #endif //__SEQLIST_H__</span>
#include"seqlist.h" void Init(seqlist* pSeq) { assert(pSeq); memset(pSeq->array, 0, MAX_SIZE*sizeof(DataType)); pSeq->size = 0;/////////////size要初始化,否则是随机值会出现问题 } void PushBack(seqlist* pSeq, DataType x) { assert(pSeq); assert(pSeq->size <= MAX_SIZE); pSeq->array[pSeq->size++] = x; } void PushFront(seqlist* pSeq, DataType x) { assert(pSeq); assert(pSeq->size <= MAX_SIZE); int i; for (i = pSeq->size; i > 0; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[0] = x; pSeq->size++; } void Insert(seqlist* pSeq, size_t index, DataType x) { assert(pSeq); assert(index < pSeq->size); assert(pSeq->size < MAX_SIZE); int i = pSeq->size - 1; for (; i >= (int)index; i--) { pSeq->array[i + 1] = pSeq->array[i]; } pSeq->array[index] = x; pSeq->size++; } void Print(seqlist* pSeq) { assert(pSeq); size_t i = 0; for (; i < pSeq->size; i++) { printf("%d ", pSeq->array[i]); } printf("\n"); } void PopBack(seqlist* pSeq) { assert(pSeq); pSeq->size--; } void PopFront(seqlist* pSeq) { assert(pSeq); size_t i; for (i = 0; i < pSeq->size; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } void Modify(seqlist* pSeq, size_t index, DataType x) { assert(pSeq); assert(index < pSeq->size); pSeq->array[index] = x; } void removed(seqlist* pSeq, size_t index) { assert(pSeq); assert(index < pSeq->size); size_t i = index; for (; i<pSeq->size-1 ; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } //返回的值为-1时,找不到此元素;否则返回的值为找到的元素的下标。 int Find(seqlist* pSeq, DataType x, size_t index) { assert(pSeq); int i = index; for (; i < (int)pSeq->size; i++) { if (pSeq->array[i] == x) { return i; } } return -1; } void Erase(seqlist* pSeq, DataType x, Tag all) { assert(pSeq); int ret; ret = Find(pSeq, x, 0); if (ret == -1) { printf("顺序表中没有这个元素\n"); } else if (all == one) { removed(pSeq, ret); } else { for (; ret != -1;) { removed(pSeq, ret); ret = Find(pSeq, x, ret); } } } void Test() { seqlist s; Init(&s); PushBack(&s, 1); PushBack(&s, 2); PushBack(&s, 2); PushBack(&s, 2); PushBack(&s, 3); PushBack(&s, 2); PushBack(&s, 4); PushBack(&s, 2); Print(&s); PopBack(&s); Print(&s); PushFront(&s, 5); Print(&s); PopFront(&s); Print(&s); Insert(&s, 0, 24); Print(&s); Modify(&s, 2, 90); Print(&s); removed(&s, 0); Print(&s); Erase(&s, 2, more); Print(&s); } void main() { Test(); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/kkmdmcgxi/article/details/46714389