C语言实现顺序表
2015-11-21 20:03
323 查看
C语言实现顺序表代码
文件SeqList.cpp#pragma warning(disable: 4715) #include"SeqList.h" void ShowSeqList(SeqList *pSeq) { assert(pSeq); printf("size = %d \n",pSeq->size); for(size_t i = 0; i < pSeq->size;i++) { printf("%d ", pSeq->array[i]); } printf("\n"); } void InitSeqList(SeqList *pSeq) { assert(pSeq); memset(pSeq->array,0,sizeof(ElemType)*MAX_SIZE); pSeq->size = 0; } void PushBack(SeqList *pSeq,const ElemType &x) { assert(pSeq); if(pSeq->size >= MAX_SIZE) { printf("SeqList is Full\n"); return; } pSeq->array[pSeq->size++] = x; } void PopBack(SeqList *pSeq) { assert(pSeq); if(pSeq->size <= 0) { printf("SeqList is Empty\n"); return; } pSeq->array[--pSeq->size] = 0; } void PushFront(SeqList *pSeq,const ElemType &x) { size_t begin = pSeq->size; assert(pSeq); if(pSeq->size >=MAX_SIZE) { printf("SeqList is Full\n"); return; } for(;begin > 0; --begin) { pSeq->array[begin] = pSeq->array[begin-1]; } pSeq->array[0] = x; pSeq->size++; } void PopFront(SeqList *pSeq) { size_t begin = 0; assert(pSeq); if(pSeq->size <= 0) { printf("SeqList is Empty\n"); return; } for(;begin < pSeq->size-1; ++begin) { pSeq->array[begin] = pSeq->array[begin+1]; } pSeq->array[--pSeq->size] = 0; } void Erase(SeqList *pSeq, size_t pos) { assert(pSeq); if(pos > pSeq->size) { printf("Position Error\n"); return; } size_t begin = pos; for(; begin < pSeq->size -1;++ begin) { pSeq->array[begin] = pSeq->array[begin+1]; } pSeq->array[--pSeq->size] = 0; } void Remove(SeqList *pSeq, const ElemType &x) { size_t begin = 0; assert(pSeq); for(; begin < pSeq->size; ++begin) { if(x == pSeq->array[begin]) { Erase(pSeq,begin); return; } } printf("No this elemData\n"); } void RemoveAll(SeqList *pSeq, const ElemType &x) { size_t begin = 0; assert(pSeq); for(; begin < pSeq->size; ++begin) { if(x == pSeq->array[begin]) { Erase(pSeq,begin); } } } //////////冒泡排序 void BubbSort(SeqList *s) { for(size_t i = 0; i < s->size-1;++i) { for(size_t j = 0; j < s->size-1-i; ++j) { if(s->array[j] > s->array[j+1]) { ElemType tmp = s->array[j]; s->array[j] = s->array[j+1]; s->array[j+1] = tmp; } } } }
文件SeqList.h
//顺序表简单实现 #ifndef _SEQLIST_H #define _SEQLIST_H #include<stdio.h> #include<string.h> //for memcpy #include<assert.h> //for assert #include<malloc.h> //for malloc #define MAX_SIZE 100 typedef int ElemType; typedef struct SeqList { ElemType array[MAX_SIZE]; size_t size; }SeqList; void ShowSeqList(SeqList *pSeq); void InitSeqList(SeqList *pSeq); void PushBack(SeqList *pSeq,const ElemType &x); void PopBack(SeqList *pSeq); void PushFront(SeqList *pSeq,const ElemType &x); void PopFront(SeqList *pSeq); void Erase(SeqList *pSeq, size_t pos); void Remove(SeqList *pSeq, const ElemType &x); void RemoveAll(SeqList *pSeq, const ElemType &x);
View Code
测试文件Main.cpp
#pragma once #include<stdio.h> #include "SeqList.h" //顺序表示例 void TestForSeqList() { SeqList Seq; InitSeqList(&Seq); PushBack(&Seq,2); PopBack(&Seq); PushBack(&Seq,6); PushFront(&Seq,4); PushFront(&Seq,4); PushBack(&Seq,2); Erase(&Seq,44); Remove(&Seq,44); ShowSeqList(&Seq); PushBack(&Seq,5); PushBack(&Seq,7); PushBack(&Seq,2); ShowSeqList(&Seq); BubbSort(&Seq); ShowSeqList(&Seq); }
相关文章推荐
- KMP-快速模式匹配算法
- c语言符号常量与常变量的区别?
- c语言输出4*5的数列?
- c语言 变量的存储类别以及对应的内存分配?
- c语言编译执行过程
- win编程实践(3)【c++】-枚举值
- jni c语言使用指针交换两个值
- 快速排序(已优化) -- C语言
- C++学习笔记27——泛型算法之统计(accumate count)
- C++赋值操作符重载简介
- C++赋值操作符重载简介
- C++11 为自定义容器实现标准的forward迭代器
- C语言扫雷
- c语言俄罗斯方块
- c++排序算法
- c语言贪吃蛇
- c语言:输入一个数,从高位向低位输出,如:输入12345,输出1 2 3 4 5
- C语言王国探秘一
- 【C语言】 实现strncpy,strncat,strncmp
- C语言模拟实现memset.memcmp函数