C语言实现顺序表
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);
}
C语言实现顺序表的更多相关文章
- C++语言实现顺序表
C++语言实现顺序表 顺序表的定义及其特点 顺序表的定义是:把线性表中的所有表项按照其逻辑顺序依次存储到从计算机存储中指定存储位置开始的一块连续的存储空间中. 这样,线性表中第一个表项的存储位置就是被 ...
- C语言实现顺序表(顺序存储结构)
顺序表(顺序存储结构)及初始化过程详解 顺序表,全名顺序存储结构,是线性表的一种.通过<线性表>一节的学习我们知道,线性表用于存储逻辑关系为"一对一"的数据,顺序表自然 ...
- java语言建立顺序表
package datastructure; //线性表 public interface IList { public void clear(); public boolean isEmpty(); ...
- C语言实现顺序表的基本操作(从键盘输入 生成线性表,读txt文件生成线性表和数组生成线性表----三种写法)
经过三天的时间终于把顺序表的操作实现搞定了.(主要是在测试部分停留了太长时间) 1. 线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素. 2. 采用的实现方式:一段地 ...
- c语言实现--顺序表操作
经过三天的时间终于把顺序表的操作实现搞定了.(主要是在测试部分停留了太长时间) 1;线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素. 2;采用的实现方式:一段地址连 ...
- 顺序表及其多种实现方式 --- C/C++
所谓顺序表,即线性表的顺序存储结构.下面给出的是数据结构---线性表的定义. ADT List{ 数据对象: 线性表的数据对象的集合为{a1,a2,a3,...,an},每个元素的类型为ElemTyp ...
- C++实现动态顺序表
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构.这样的存储方式使得线性表逻辑上相邻的元素,其在物理存储单元中也是相邻的.只要知道了第一个元素的存 ...
- 动态顺序表(C++实现)
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构. 这样的存储方式使得线性表逻辑上相邻的元素,其在物理存储单元中也是相邻的.只要知道了第一个元素的 ...
- 数据结构C语言版--动态顺序表的基本功能实现(二)
/* * 若各个方法结构体变量参数为: &L(即地址符加变量)则结构体变量访问结构成员变量时使用"." * 若为:*L(即取地址符加变量)则结构体变量访问结构体成员变量使用 ...
随机推荐
- boost circular buffer环形缓冲类
Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据. 它是一个与STL兼容的容器,类似于 std::list或std::de ...
- Linux下BLAST的使用---转载
1.把BLAST的压缩文件解压,然后将bin目录下的文件拷贝至/usr/local/bin下:2.制作软链接,将解压后的文件中bin目录链接至/home/username下,eg:ln -s /hom ...
- v9上传图片/附件失败出现undefined的解决方法之一
把phpcms\modules\attachment\attachments.php中将 if(empty($this->userid)){改成 ...
- CCF 工资计算
思路: 因为T<=10000,所以税前极限金额肯定不超过1000000(设个比较大的数字就行),然后逐一计算即可. #include<cstdio> int main() { int ...
- Maven 在 IntelliJ IDEA 中的使用
一.概述 Maven 为构建软件,与 Gradle 类似,也能以插件的方式在 IntelliJ IDEA 中得到使用. 同样地,你也可以配置环境变量,这样就能够在命令行中进行操作了. 二.使用方式 其 ...
- Linux进程管理 lsof命令:列出进程调用或打开的文件信息
lsof命令 通过 ps 命令查询到系统中所有的进程, 通过lsof 命令可以知道这个进程到底在调用哪些文件.lsof 命令格式如下: [root@localhost ~]# lsof [选项] 选项 ...
- ubuntu下通过mono+jexus布署mvc5网站
本文使用的ubuntu为14.04 LTS 一.安装mono,本文使用源码安装的方式 1.搭架mono编译环境 sudo apt-get update sudo apt-get install bui ...
- 《阿里巴巴Java开发规约》插件使用
通过Jetbrains官方仓库安装 1. 打开 Settings >> Plugins >> Browse repositories... 2. 在搜索框输入alibaba即可 ...
- linux卸载mysql
第二.停止MYSQL运行以及卸载老版本 service mysqld stop #暂停MYSQL yum remove mysql mysql-* #卸载老版本MYSQL 通过上面的命令,我们先停止 ...
- Stitching模块中对特征提取的封装解析(以ORB特性为例)
titching模块中对特征提取的封装解析(以ORB特性为例) OpenCV中Stitching模块(图像拼接模块)的拼接过程可以用PipeLine来进行描述,是一个比较复杂的过程.在这个过程 ...