您的位置:首页 > 编程语言 > C语言/C++

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: