【C语言】顺序表,数组

    顺序表的搭建,就是一个本身的结构,利用的是数组,开辟的内存空间大小是固定的。没有链式结构那样的开辟空间的可控性。最基本的数据结构认知。

    然后我们来看看数据结构中的基本实现和思想:

    

#include "seqList.h"
#include <stdio.h>
#include <assert.h>
//顺序表的初始化
void InitSeqList(SeqList* seq)
{
	int _index = 0;
	assert(seq);
	for(;_index < MAX_SIZE;++_index)
	{
		seq->_array[_index] = 0;
	}
	seq->_size = 0;
}

//打印顺序表
void PrintSeqList(SeqList* seq)
{
	int _index = 0;
	assert(seq);
	if(0 == seq->_size)
	{
		printf("顺序表没有数据\n");
		return;
	}
	for(;_index<seq->_size;++_index)
	{
		printf("%d ",seq->_array[_index]);
	}
	printf("\n");
}
//尾部插入
void PushBack(SeqList* seq, DataType x)
{
	int _CheckSeq;
	assert(seq);
	if(1 == (_CheckSeq = CheckSeqList(seq)))
	{
		printf("顺序表已满");
		return;
	}
	seq->_array[seq->_size++] = x;
}
//尾部删除
void PopBack(SeqList* seq)
{	
	int _CheckSeq;
	assert(seq);
	if(0 == (_CheckSeq = CheckSeqList(seq)))
	{
		printf("顺序表为空\n");
		return;
	}
	seq->_array[--seq->_size] = 0;
}
//头部插入
void PushFront(SeqList* seq, DataType x)
{
	int _index = 0;
	int _CheckSeq;
	assert(seq);
	if(1 == (_CheckSeq = CheckSeqList(seq)))
	{
		printf("顺序表已满");
		return;
	}
	for(;_index < seq->_size;++_index)
	{
		seq->_array[seq->_size-_index] = seq->_array[seq->_size - _index - 1];
	}
	seq->_array[0] = x;
	seq->_size++;
}
//头部删除
void PopFront(SeqList* seq)
{
	int _index = 0;
	int _CheckSeq;
	assert(seq);
	if(0 == (_CheckSeq = CheckSeqList(seq)))
	{
		printf("顺序表为空\n");
		return;
	}
	for(;_index<seq->_size-1;++_index)
	{
		seq->_array[_index] = seq->_array[_index+1];
	}
	seq->_size--;
}
//插入操作
void Insert(SeqList* seq, size_t pos, DataType x)
{
	int _index = seq->_size;
	int _CheckSeq;
	assert(seq);
	if(1 == (_CheckSeq = CheckSeqList(seq)))
	{
		printf("顺序表已满");
		return;
	}
	if(pos < 0 || pos > seq->_size)
	{
		printf("修改位置超出边界");
		return;
	}
	for(;_index > pos;--_index)
	{
		seq->_array[_index] = seq->_array[_index - 1];
	}
	seq->_array[pos] = x;
	seq->_size++;
}

//寻找。
// 返回-1表示未找到数据
int Find(SeqList* seq, DataType x)
{
	int _endIndex = 0;
	assert(seq);
	for(; _endIndex < seq->_size;++ _endIndex)
	{
		if(x == seq->_array[_endIndex])
		{
			return _endIndex;
		}
	}
	return -1;
}
//擦除输入坐标的数据
void Erase(SeqList* seq, size_t pos)
{
	int _index = pos;
	assert(seq);
	if(pos < 0 || pos > seq->_size)
	{
		printf("修改位置超出边界");
		return;
	}
	for(;_index < seq->_size-1;++_index)
	{
		seq->_array[_index] = seq->_array[_index+1];
	}
}
//移除查找到的第一个函数
void Remove(SeqList* seq, DataType x)
{
	int _pos = 0;
	int _index = -1;
	for(;_pos < seq->_size;++_pos)
	{
		if(x == seq->_array[_pos])
		{
			_index = _pos;
			break;
		}
	}
	if(-1 != _index)
	{
		for(;_index < seq->_size - 1;++_index)
		{
			seq->_array[_index] = seq->_array[_index + 1];
		}
		seq->_size--;
	}
	else
	{
		printf("没有这个数");
	}
}
//移除所有查询到的数据,这个算法比较优质。直接移动替换。
void RemoveAll(SeqList* seq, DataType x)
{
	int _pos = 0;
	int _index = 0;
	for(;_pos < seq->_size;++_pos)
	{
		if(x != seq->_array[_pos])
		{
			seq->_array[_index] = seq->_array[_pos];
			_index++;
		}
	}
}
//修改函数。
void Modify(SeqList* seq, size_t pos, DataType x)
{
	assert(seq);
	if(pos < 0 || pos > seq->_size)
	{
		printf("修改位置超出边界");
		return;
	}
	seq->_array[pos] = x;                                      
}

int CheckSeqList(SeqList *seq)
{
	if(MAX_SIZE == seq->_size)
	{
		return 1;
	}
	if(0 == seq->_size)
	{
		return 0;
	}
}

    就这么多了。其实就是一些简单的数据结构的基本思维。增删改插的基本思维。

本文出自 “剩蛋君” 博客,请务必保留此出处http://memory73.blog.51cto.com/10530560/1703683

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值