C++ 顺序表

/***
1顺序表
1、必做题

  1. 编写程序建立一个数续表,并逐个输出顺序表中所有数据元素的值。编写主函数测试结果。
  2. 编写顺序表定位操作子函数,在顺序表中查找是否存在数据元素x。
      如果存在,返回顺序表中和x值相等的第1个数据元素的序号(序号从0开始编号);
    如果不存在,返回-1。编写主函数测试结果。
  3. 在递增有序的顺序表中插入一个新结点x,保持顺序表的有序性。
    解题思路:首先查找插入的位置,再移位,最后进行插入操作;
    从第一个元素开始找到第一个大于该新结点值x的元素位置i即为插入位置;
    然后将从表尾开始依次将元素后移一个位置直至元素i;最后将新结点x插入到i位置。
  4. 删除顺序表中所有等于X的数据元素。

2、选做题
  已知两个顺序表A和B按元素值递增有序排列,
  要求写一算法实现将A和B归并成一个按元素值递减有序排列的顺序表
  (允许表中含有值相同的元素)。

***/

//在头文件中"SeqList.h"
/**< writer:FDA_orangebook */
/** \brief: 顺序表
*
* int Length(){return length;}
//用于获取长度1-N
* T Get(int i);
//用于获取第I个数据
* T Locate(T value);
//用于获取 值为value的位置
* void Insert(T value);
//在递增的顺序中,插入value值
* void Insert(int position,T value);
//在position这个位置,插入value
* void Insert(T *_data,int _length);
//将_data与data以递减的顺序合并
* T Delete(int position);
//将position这个中的元素删除
* void PrintList();
//打印 data 序列
*/ const int MaxSize=;
#define T int //此处修改 数组的类型
class SeqList
{
public:
//定义两个构造函数
SeqList()
{
length=;
}
SeqList(T a[],int n);
~SeqList() {}
int Length()
{
return length;
}
T Get(int i);
T Locate(T value);
void Insert(T value);
void Insert(int position,T value);
void Insert(T *_data,int _length);
T Delete(int position);
void PrintList();
private:
T data[MaxSize];
int length;
};
//在SwqList.cpp
#ifndef SEQLIST_H_ #define SEQLIST_H_
#include "SeqList.h"
#include<iostream>
#include<algorithm>
using namespace std;
SeqList::SeqList(T *a,int n)
{
if(n>MaxSize) cout<<"Parameters of illegal"<<endl;
for(int i=; i<n; i++)
data[i]=a[i];
length=n;
} void SeqList::Insert(T value)
{
int j;
sort(data,data+length);
cout<<length<<endl;
for(j=length; value<=data[j-]&&j>; --j)
data[j]=data[j-];
data[j]=value;
++length;
cout<<length<<endl;
} void SeqList::Insert(int position,int value)
{
if(length>=MaxSize||position<||position>length+)
cout<<"input error"<<endl;
for(int j=length; j>=position; --j)
data[j]=data[j-];
data[position-]=value;
++length;
}
bool myfunction (int i,int j)
{
return (i>j);
}
void SeqList::Insert(T *_data,int _length)
{
//int j=0;
sort(data,data+length,myfunction);
sort(_data,_data+_length,myfunction);
PrintList();
int i=;int j;
for(; i<_length;++i)
{
for( j=;_data[i]<data[j]&&j<length;++j);
Insert(j+,_data[i]); } } T SeqList::Delete(int position)
{
if(length==||position<||position>length) cout<<"error"<<endl;
else
{
int x=data[position-];
for(int j=position; j<length; j++)
data[j-]=data[j];
length--;
return x;
}
return (T)-;
} T SeqList::Get(int i)
{
if(i<||i>length) cout<<"error"<<endl;;
return data[i-];
} T SeqList::Locate(T value)
{
for(int i=; i<length; i++)
if(data[i]==value) return i+;
return -;
} void SeqList::PrintList()
{
cout<<"\n****************************"<<endl;
for(int i=; i<=length; i++)
i%==?cout<<data[i-]<<"\n":cout<<data[i-]<<"\t";
};
#endif // SwqList_H_
#include"SeqList.h"
#include<iostream>
#include<stdlib.h> using namespace std;
#define MAX 100 int main()
{
T a[]= {,,,,,,,,,};
T bb[]= {,,,,,,};
SeqList test(a,); test.PrintList(); if(test.Locate(a[])!=-)
cout<<test.Locate(a[])<<endl;
test.Insert(-);
test.PrintList(); test.Delete(test.Locate(-));
test.PrintList(); test.Insert(bb,);
test.PrintList();
}

C++ 顺序表的更多相关文章

  1. jdk顺序表笔记

    一.AbstractCollection 提供了集合的最大实现 继承该类,必须实现size()和iterator(),因为该类操作集合都是通过iterator 二.fail-fast策略 该策略在集合 ...

  2. c++顺序表基本功能

    头文件 #define LIST_MAX_SIZE 5#define LISTINCREMENT 2#include<assert.h>#include<string>temp ...

  3. 数据结构:顺序表(python版)

    顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...

  4. 《数据结构》2.2顺序表(sequence list)

    //顺序表节点的定义 typedef struct { datatype data[MAXSIZE]; //数组容量的上限 int len; //记录最后一个元素的位置,相当于一个指针,表空时len= ...

  5. c数据结构 顺序表和链表 相关操作

    编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...

  6. java顺序表和树的实现

    一.顺序表 1.线性表 //java顺序表的实现,如ArrayList就是用线性表实现的,优点是查找快,缺点是添加或删除要移动很多元素,速度慢 public class SequenceList { ...

  7. 数据结构顺序表删除所有特定元素x

    顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...

  8. 顺序表C语言版

    #include <stdio.h> /* * 顺序表最多输入N个数 */ #define N 10 #define OK 1 #define ERROR -1 struct sequeu ...

  9. C#线性表之顺序表

    线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这种一对一的关系指的是数据元素之间的位置关系,即: ...

  10. C语言 线性表 顺序表结构 实现

    一个能够自动扩容的顺序表 ArrList (GCC编译). #include <stdio.h> #include <stdlib.h> #include <string ...

随机推荐

  1. Akka官方文档翻译:Cluster Specification

    参加了CSDN的一个翻译项目,翻译Akka的文档.CSDN提供的翻译系统不好使,故先排版一下放在博客上. 5.1 集群规范 注意:本文档介绍了集群的设计理念.它分成两部分,第一部分描述了当前已经实现的 ...

  2. 用IDEA调试Play工程

    IDEA的版本是14.0.1,运行在MAC OS X Yosemite上. IDEA已经装了Scala插件,但是在新建工程中,Scala的选项中并没有Play框架,不知道什么原因. 导入Play工程 ...

  3. nagios plugins之 check_http

    nagios下的check_http ZT具体参数是一个比较重要的点,我带大家来看看.. //显示版本 #./check_http -V check_http v2053 (nagios-plugin ...

  4. ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map

    原文地址: ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NE ...

  5. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  6. win7打开或关闭windows功能 提示“出现错误,并非所有的功能被更改”,管理员权限惹的祸

    2013-07-25 18:12:06 最近要用到windows的telnet功能,本来是很简单的事情,因为管理员权限的问题,花了不少时间,才发现是管理员权限惹的祸,更滑稽的是,自己一直以来都不是管理 ...

  7. ARM处理器全解析:A8/A9/A15都是什么?

    前不久ARM正式宣布推出新款ARMv8架构的Cortex-A50处理器系列产品,以此来扩大ARM在高性能与低功耗领域的领先地位,进一步抢占移动终端市场份额.Cortex-A50是继Cortex-A15 ...

  8. Eclipse优化

    未特别说明,以下均处理在Window->Preferences下 General列表下 Startup and Shutdown可以去掉一些不必要的启动项 怎样才能知道哪些启动项有用呢?我现在把 ...

  9. ubuntu程序安装方法

    以前一直使用window,今天安装了一个ubuntu系统(如果有同学也想装,建议装英文版的),因为以前ubuntu系统用的不多,所以安装软件就是一个问题. 就以安装chrome来说吧: 1.在Goog ...

  10. poj 1924 Paths on a Grid(组合数学)

    题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...