数据结构---顺序表(C++)
顺序表
- 是用一段地址连续的存储单元依次存储线性表的数据元素。
- 通常用一维数组来实现
基本操作:
- 初始化
- 销毁
- 求长
- 按位查找
- 按值查找
- 插入元素
- 删除位置i的元素
- 判空操作
- 遍历操作
示例代码:
//声明.h
const int MAXSIZE = 100;
template<typename dataType>
class SeqList {
public:
SeqList(); //无参构造
SeqList(dataType a[], int length); //有参构造
~SeqList();
int seqlist_Length();
dataType get_i(int i); //查找第i位的元素
int search_x(dataType x); //查找值为x的元素
void insert_x(int n, dataType x); //在第n位插入值位x的元素
dataType delete_i(int i); //删除第i位元素
void print_list();
private:
dataType data_[MAXSIZE];
int length_;
};
//定义.h
#include<iostream>
using std::cout;
using std::endl;
template<typename dataType>
inline SeqList<dataType>::SeqList()
{
length_ = 0;
}
template<typename dataType>
inline SeqList<dataType>::SeqList(dataType a[], int length)
{
if (length > MAXSIZE)
{
throw"长度非法";
}
for (int i = 0; i < length; ++i)
{
data_[i] = a[i];
}
length_ = length;
}
template<typename dataType>
inline SeqList<dataType>::~SeqList()
{}
template<typename dataType>
inline int SeqList<dataType>::seqlist_Length()
{
return length_;
}
template<typename dataType>
inline dataType SeqList<dataType>::get_i(int i)
{
if (i > length_ || i < 1)
{
throw"查找位置不存在";
}
else
{
return data_[i - 1];
}
}
template<typename dataType>
inline int SeqList<dataType>::search_x(dataType x)
{
for (int i = 0; i < length_; ++i)
{
if (data_[i] == x)
{
return i + 1;
}
}
return 0;
}
template<typename dataType>
inline void SeqList<dataType>::insert_x(int n, dataType x)
{
if (length_ == MAXSIZE)
{
throw"表已满";
}
if (n >= length_ + 1 || n<1)
{
throw"插入位置非法";
}
for (int i = length_; i >= n; i--)
{
data_[i] = data_[i - 1];
}
data_[n - 1] = x;
++length_;
}
template<typename dataType>
inline dataType SeqList<dataType>::delete_i(int i)
{
if (length_ == 0)
{
throw"表为空";
}
if (i<1 || i>length_)
{
throw"删除位置非法";
}
int x = data_[i - 1];
for (int j = i; j < length_; j++)
{
data_[j - 1] = data_[j];
}
length_--;
return x;
}
template<typename dataType>
inline void SeqList<dataType>::print_list()
{
for (int i = 0; i < length_; i++)
{
cout << data_[i];
}
cout << endl;
}
//main.cpp
#include"announced.h"
int main()
{
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
数据结构---顺序表(C++)的更多相关文章
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
基础数据结构——顺序表(2) Time Limit: 1000 MS Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...
- hrbust-1545-基础数据结构——顺序表(2)
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1545 基础数据结构——顺序表(2) ...
- c数据结构 顺序表和链表 相关操作
编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...
- 数据结构顺序表删除所有特定元素x
顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...
- 数据结构顺序表Java实现
Java实现顺序表算法:1:首先我们需要定义我们的接口,关于顺序表的一些基本的操作:顺序表中的操作都有增删改查. //List接口 public interface IList { //返回线性表的大 ...
- python算法与数据结构-顺序表(37)
1.顺序表介绍 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素.顺序表可以分配一段连续的存储空间 ...
- 数据结构——顺序表(sequence list)
/* sequenceList.c */ /* 顺序表 */ /* 线性表的顺序存储是指在内存中用地址连续的一块存储空间顺序存放线性表中的各项数据元素,用这种存储形式的线性表称为顺序表. */ #in ...
- 数据结构顺序表中Sqlist *L,&L,Sqlist *&L
//定义顺序表L的结构体 typedef struct { Elemtype data[MaxSize]: int length; }SqList; //建立顺序表 void CreateList(S ...
- Java数据结构——顺序表
一个线性表是由n(n≥0)个数据元素所构成的有限序列. 线性表逻辑地表示为:(a0,a1,…,an-1).其中,n为线性表的长度,n=0时为空表.i为ai在线性表中的位序号. 存储结构:1.顺序存储, ...
随机推荐
- git 常用命令 创建查看删除分支,创建查看删除tag等
1. git 文档 https://github.com/progit/progit/blob/master/zh/02-git-basics/01-chapter2.markdown https ...
- [转]Oracle ORA-01403: no data found Exception SYS_REFCURSOR
本文转自:http://stackoverflow.com/questions/9104153/what-is-the-correct-way-to-deal-with-this-oracle-ora ...
- my_vimrc
" ----------------- Author: Ruchee" ----------------- Email: my@ruchee.com" --------- ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- $GLOBALS['HTTP_RAW_POST_DATA']、$_POST和php://input深入探究三者的区别
$_POST:通过 HTTP POST 方法传递的变量组成的数组.是自动全局变量. $GLOBALS['HTTP_RAW_POST_DATA'] :总是产生 $HTTP_RAW_POST_DATA 变 ...
- Java 简单算法--排序
1. 冒泡排序 package cn.magicdu.algorithm; public class BubbleSort { public static void main(String[] arg ...
- linux中的常用命令
cat tail -f 日 志 文 件 说 明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 /var/log/secure 与安全相关 ...
- FusionChart 导出图片 功能实现(转载)
FusionChart 导出图片 功能实现(转载) http://www.cnblogs.com/jiagoushi/archive/2013/02/05/2893468.html 题目:精美Fusi ...
- <Error>: CGContextRestoreGState
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please ...
- MVC3中 swfupload 按钮不显示 解决方案
这两天在做图片上传并显示的功能,之前就用过swfupload,觉得很不错,之前是用asp.net webform做的,这次的项目是用asp.net MVC3来做,视图引擎用的是Razor. 将js文件 ...