我的Bit路-C语言实现顺序

C语言实现顺序表储存

文件SeqList.cpp
<span style="font-size:14px;">#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;
			}
		}
	}
}
</span>
文件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);

测试文件
#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);
}

以上是本人勉力实现的顺序表储存,欢迎各位大神指正,如有问题请留言。谢谢






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值