关闭

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

标签: C++链表数据结构
224人阅读 评论(0) 收藏 举报
分类:

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);
}

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






   
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4471次
    • 积分:131
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    文章分类
    文章存档
    最新评论