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(即取地址符加变量)则结构体变量访问结构体成员变量使用 ...
随机推荐
- IOS 此时无法安装XXX
背景介绍 替一家公司做了企业APP,由于经常需要更新,考虑到上传到APP Store的审核过程,所以当初选定了是用企业证书发布,然后通过网页自动跳转下载APP. 事情原委 昨天下午突然接到客户反馈,I ...
- 转载 C#开发串口总结,并提炼串口辅助类到公用类库中
C#开发串口总结,并提炼串口辅助类到公用类库中 开发C#相关的项目有很多年了,一直没有接触串口的开发,近期由于工作的需要,需要了解熟悉对硬件串口的开发,通过对串口的深入了解,串口也不再是什么神秘的东西 ...
- C# comport 打印图像
public string GetLogo() { string logo = ""; if (!File.Exists(@"C:\bitmap.bmp")) ...
- mac 安装Sequel Pro
安装命令如下 Install the App Press Command+Space and type Terminal and press enter/return key. Run in Term ...
- iClap:产品经理再忙也要看《琅琊榜》
最先知道<琅琊榜>,是半年前偶然看了整整21分钟的<琅琊榜>片花,对麒麟才子梅长苏这一角色甚是期待,开播后每集必看,重复看,此剧果真不负众望,口碑爆棚,收视爆红,确是一部久违的 ...
- mac/linux查询网络端口占用
参考:http://www.cnblogs.com/kaiye/archive/2013/05/25/3099393.html netstat命令 netstat -an|grep 8080 lsof ...
- Python 实例2—购物车
老男孩教学学习笔记 """启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒可随机退出,退出 ...
- win32api win32gui win32con 窗口句柄 发送消息 常用方法
Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量.接口.线程以及COM机制等等. 1.通过类名和标题查找窗口句柄,并获得窗口位置和 ...
- JS的checkbox状态切换dom无变化
今天调试checkbox,手动加上checked="checked"和去掉,都对实际页面没有产生影响 搜索一番 1.对radio .checkbox 来说说,checked属性可以 ...
- 20145230熊佳炜《网络对抗》实验五:MSF基础应用
20145230熊佳炜<网络对抗>实验五:MSF基础应用 主动攻击 首先,我们需要弄一个xp sp3 English系统的虚拟机,然后本次主动攻击就在我们kali和xp之间来完成. 然后我 ...