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 = ; i < pSeq->size;i++)
{
printf("%d ", pSeq->array[i]);
}
printf("\n");
} void InitSeqList(SeqList *pSeq)
{
assert(pSeq);
memset(pSeq->array,,sizeof(ElemType)*MAX_SIZE);
pSeq->size = ;
} 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 <= )
{
printf("SeqList is Empty\n");
return;
}
pSeq->array[--pSeq->size] = ;
} 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 > ; --begin)
{
pSeq->array[begin] = pSeq->array[begin-];
}
pSeq->array[] = x;
pSeq->size++;
}
void PopFront(SeqList *pSeq)
{
size_t begin = ;
assert(pSeq);
if(pSeq->size <= )
{
printf("SeqList is Empty\n");
return;
}
for(;begin < pSeq->size-; ++begin)
{
pSeq->array[begin] = pSeq->array[begin+];
}
pSeq->array[--pSeq->size] = ;
} 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 -;++ begin)
{
pSeq->array[begin] = pSeq->array[begin+];
}
pSeq->array[--pSeq->size] = ;
} void Remove(SeqList *pSeq, const ElemType &x)
{
size_t begin = ;
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 = ;
assert(pSeq);
for(; begin < pSeq->size; ++begin)
{
if(x == pSeq->array[begin])
{
Erase(pSeq,begin);
}
}
} //////////冒泡排序
void BubbSort(SeqList *s)
{
for(size_t i = ; i < s->size-;++i)
{
for(size_t j = ; j < s->size--i; ++j)
{
if(s->array[j] > s->array[j+])
{
ElemType tmp = s->array[j];
s->array[j] = s->array[j+];
s->array[j+] = 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);
测试文件Main.cpp
#pragma once
#include<stdio.h>
#include "SeqList.h"
//顺序表示例
void TestForSeqList()
{
SeqList Seq;
InitSeqList(&Seq);
PushBack(&Seq,);
PopBack(&Seq);
PushBack(&Seq,);
PushFront(&Seq,);
PushFront(&Seq,);
PushBack(&Seq,); Erase(&Seq,);
Remove(&Seq,); ShowSeqList(&Seq);
PushBack(&Seq,);
PushBack(&Seq,);
PushBack(&Seq,);
ShowSeqList(&Seq);
BubbSort(&Seq);
ShowSeqList(&Seq);
}