#include <iostream>

using namespace std; 

class  ArrayLinerTable

{

public:  

    void InitLinerTable(int); //初始化线性表

 void MakeEmpty() ;//清空线性表

    int  GetLength() ;//获取长度

 int  Locate(int) ;//获取制定位置的数据

 ArrayLinerTable* Insert(int,int) ;//在制定位置插入一个元素

 ArrayLinerTable* AppendTo(int) ;//追加

 ArrayLinerTable* PrintLinerTable() ;//打印线性表

 ArrayLinerTable* Delete(int) ;//删除指定位置

 ArrayLinerTable* Update(int,int) ;//修改元素

 bool IsEmpty() ;//是否空

 ArrayLinerTable*ClearAllData() ;//清除所有元素

 ArrayLinerTable*SortAsc() ;//升序

 ArrayLinerTable*SortDesc();//降序

 ArrayLinerTable(); //构造

 ~ArrayLinerTable();//析构

private:

 int *pLinerTableHeader ; //表头

 int length ;//

 int maxIndex ;//当前最大数量

 

};

ArrayLinerTable::ArrayLinerTable()

{

  this->maxIndex= -1;  //刚开始不存在-1

  pLinerTableHeader=NULL ;

}

void ArrayLinerTable::InitLinerTable(int length)//不能使用模版成员指针 因为在编译期间无法确定类型所以是错误的

{

    this->pLinerTableHeader=new int[length] ;

 this->length=length ;

}

void ArrayLinerTable::MakeEmpty()

{

  delete this->pLinerTableHeader ;

  this->pLinerTableHeader=NULL ;

}

int   ArrayLinerTable::GetLength()  

{

      return this->length ;

}

int  ArrayLinerTable::Locate(int i)

{  

    if(-1==maxIndex)

 {

   cout<<"表内没有数据!"<<endl ;

   return  0;

 }

 if (i>maxIndex)

 {    

  

  cout<<"超出最大长度"<<endl ;

  return  0;

 }

  return pLinerTableHeader[i] ;

}

ArrayLinerTable*  ArrayLinerTable::Insert(int position,int data)

{     

 if(pLinerTableHeader==NULL)

 {

  InitLinerTable(100) ;//初始化

  this->length=100 ;

  this->maxIndex=0 ;

  pLinerTableHeader[0]=data ;

  return this ;

   }

      if(maxIndex>=length-1)

   {

     cout<<"线性表已满!"<<endl ;

  return this ;

   }  

   if ((position>maxIndex+1||position==maxIndex+1)&&position<length) //尾部

   {

    pLinerTableHeader[maxIndex+1]=data ;

    maxIndex++ ;

    return  this;

   }

   int x,y ;

   for (int i=position;i<maxIndex;i++)

   { 

    if(i==position)  //第一次的叫唤

    {

     x=pLinerTableHeader[i+1] ;

     pLinerTableHeader[i+1]=pLinerTableHeader[position] ;

     continue;

    }

    y=pLinerTableHeader[i+1];

    pLinerTableHeader[i+1]=x ;

    x=y ;

   }

   pLinerTableHeader[maxIndex+1]=x;

   pLinerTableHeader[position]=data ;

   maxIndex++ ;

   return this ;

}

ArrayLinerTable* ArrayLinerTable::AppendTo(int data)



  if(pLinerTableHeader==NULL)

  {

   InitLinerTable(100) ;//初始化

   this->length=100 ;

   this->maxIndex=0 ;

   pLinerTableHeader[0]=data ;

   return this ;

   }

   if (maxIndex==length-1)

   {

    cout<<"表满!"<<endl ;

    return this;

   }

   pLinerTableHeader[maxIndex+1] =data ;

   maxIndex++ ;

   return this ;

}

ArrayLinerTable*ArrayLinerTable::PrintLinerTable()



   if (maxIndex==-1)

   {

    cout<<"No Data"<<endl ;

   }

   for (int i=0;i<=maxIndex;i++)

   {

     cout<<"Position "<<i<< " Data: "<<this->Locate(i)<<endl ;

   }

   return this;

}

ArrayLinerTable*  ArrayLinerTable::Delete(int position)

{  

    if(position>maxIndex){

  cout<<"位置超过最大索引"<<endl ;

  return this ;

 }

 if(position==maxIndex){  //尾部删除

  maxIndex-- ;

  return this ;

 } 

   for(int i=position;i<maxIndex;i++)

   {

    pLinerTableHeader[i]=pLinerTableHeader[i+1];

   }

   maxIndex--;

   return this ;

}

bool  ArrayLinerTable::IsEmpty()

{

 return this->pLinerTableHeader==NULL?true:false ;

}

ArrayLinerTable*ArrayLinerTable::ClearAllData()

{

   for (int i=0;i<maxIndex;i++)

   {

    pLinerTableHeader[i]=0 ;

   

   }

   maxIndex=-1 ;

   return this ;

}

ArrayLinerTable* ArrayLinerTable::Update(int position,int data)

{

    if(position>maxIndex){

  cout<<"位置超过最大索引"<<endl ;

  return this ;

 } 

 pLinerTableHeader[position]=data ;

 return this ;

}

ArrayLinerTable* ArrayLinerTable::SortAsc() //升序

{

    for (int i=0;i<=maxIndex-1;i++)

    {

  for (int j=i+1;j<=maxIndex;j++)

  {

   if (pLinerTableHeader[i]>pLinerTableHeader[j])

   {

               pLinerTableHeader[i]=pLinerTableHeader[j]+pLinerTableHeader[i] ;

      pLinerTableHeader[j]=pLinerTableHeader[i]-pLinerTableHeader[j] ;

      pLinerTableHeader[i]=pLinerTableHeader[i]-pLinerTableHeader[j] ;

   }

}

    }

 return this ;

}

ArrayLinerTable* ArrayLinerTable::SortDesc() //降序

{

for (int i=0;i<=maxIndex-1;i++)

    {

  for (int j=i+1;j<=maxIndex;j++)

  {

   if (pLinerTableHeader[i]<pLinerTableHeader[j])

   {

    pLinerTableHeader[i]=pLinerTableHeader[j]+pLinerTableHeader[i] ;

    pLinerTableHeader[j]=pLinerTableHeader[i]-pLinerTableHeader[j] ;

    pLinerTableHeader[i]=pLinerTableHeader[i]-pLinerTableHeader[j] ;

   }

   

  }

    }

 return this ;

}

ArrayLinerTable::~ArrayLinerTable()



   if(pLinerTableHeader!=NULL)

    delete this->pLinerTableHeader;

   cout<<"对象释放!"<<endl ;

}

#include <iostream>

#include "ArrayLinerTable.h"

using namespace std; 

int main(int*argc,char **agrgv)

{

    

   //cout<<__FILE__<<"---"<<__LINE__<<endl; 显示行 文件

   //预编译指令用于防止某些代码被编译  通常被用作调试使用

#ifdef DEBUG  

   cout<<"开启DEBUG模式!"<<endl ;

#endif

   //system("COLOR C9") ;

   ArrayLinerTable *linerTable=new ArrayLinerTable() ;

   for(int i=0;i<10;i++)

    linerTable->AppendTo(i) ;

   linerTable->Insert(1,15) ;

   linerTable->PrintLinerTable();

   cout<<"--------升序排序---------"<<endl ;

   linerTable->SortAsc()->PrintLinerTable() ;

   cout<<"-------降序排序----------"<<endl ;

   linerTable->SortDesc()->PrintLinerTable() ;

   cout<<"-------清空数据----------"<<endl ;

   linerTable->ClearAllData()->PrintLinerTable()->MakeEmpty();

   delete linerTable ;

 return    0;

}

C++数据结构和算法每天一练(线性表)的更多相关文章

  1. Java数据结构和算法(一)线性结构之单链表

    Java数据结构和算法(一)线性结构之单链表 prev current next -------------- -------------- -------------- | value | next ...

  2. Java数据结构和算法(一)线性结构

    Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...

  3. "《算法导论》之‘线性表’":基于静态分配的数组的顺序表

    首先,我们来搞明白几个概念吧(参考自网站数据结构及百度百科). 线性表 线性表是最基本.最简单.也是最常用的一种数据结构.线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外, ...

  4. java数据结构和算法09(哈希表)

    树的结构说得差不多了,现在我们来说说一种数据结构叫做哈希表(hash table),哈希表有是干什么用的呢?我们知道树的操作的时间复杂度通常为O(logN),那有没有更快的数据结构?当然有,那就是哈希 ...

  5. HNCU1324:算法2-2:有序线性表的有序合并(线性表)

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1324 题目描述 已知线性表 LA 和 L ...

  6. 数据结构与算法【Java】02---链表

    前言 数据 data 结构(structure)是一门 研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构才可以编写出更加漂亮,更加有效率的代码. 要学习好数据结构就要多多考虑如何将生 ...

  7. [PTA] 数据结构与算法题目集 6-2 顺序表操作集

    //创建并返回一个空的线性表: List MakeEmpty() { List L; L = (List)malloc(sizeof(struct LNode)); L->Last = -1; ...

  8. Android版数据结构与算法(四):基于哈希表实现HashMap核心源码彻底分析

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 存储键值对我们首先想到HashMap,它的底层基于哈希表,采用数组存储数据,使用链表来解决哈希碰撞,它是线程不安全的,并且存储的key只能有一个为 ...

  9. "《算法导论》之‘线性表’":基于数组实现的单链表

    对于单链表,我们大多时候会用指针来实现(可参考基于指针实现的单链表).现在我们就来看看怎么用数组来实现单链表. 1. 定义单链表中结点的数据结构 typedef int ElementType; cl ...

随机推荐

  1. bzoj 3823: 定情信物 线性筛逆元

    3823: 定情信物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 108  Solved: 2[Submit][Status] Descriptio ...

  2. shell 基础 $(cd `dirname $0`;pwd)

    $ cd `dirname $0` 和PWD%/* shell变量的一些特殊用法 在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的"." ...

  3. CF 370

    A:http://codeforces.com/problemset/problem/370/A #include<stdio.h> #include<string.h> #i ...

  4. mybatis源码分析(2)——事务概述

    这篇文章主要对mybatis中的事务做一简单的分析,帮助读者理清一些概念. 先来看看在JAVA事务的相关技术,在JAVA中有两类事务,JDBC事务和JTA事务,如果是JDBC类型的事务,则是由Conn ...

  5. C#实例:5个.net经典例子(窗体与界面设计)

    实例001  带历史信息的菜单 实例说明 在开发图纸管理软件时,要求在菜单上记录用户最近打开的档案或图纸,以方便下次使用.如图1.1所示,单击“文件”菜单下的“打开文件”子菜单,打开需要查阅的图纸.下 ...

  6. Unity3D游戏开发入门(一)

    视频: 慕课网适合入门 http://www.imooc.com/video/6582 蛮牛网: http://www.manew.com/ 圣殿中文手册 5.3.2破解工具 面试题 pdf 书籍:

  7. django 项目部署在 Apache 后, 设置二级域名(Apache虚拟主机 、 万网二级域名设置)

    上一篇文章简单说了怎么把django的项目部署到Apache上. 现在想弄个二级域名,也就是我原来有个域名 www.mysite.com,现在我想弄个 bbs.mysite.com ,该怎么做呢. 要 ...

  8. 三大跨平台网盘--dropbox

    背景介绍 Dropbox是一个提供同步本地文件的网络存储在线应用.支持在多台电脑多种操作中自动同步.并可当作大容量的网络硬盘使用. 准备工作 帐号--dropbox官网 软件--windows/ubu ...

  9. fork和缓冲区

    fork在面试中经常被问到,在这里复习一下. frok创建子进程,父子进程共享.text段,子进程获得父进程数据段.堆和栈的副本,由于在fork之后经常跟随者exec,所以很多实现并不执行父进程数据段 ...

  10. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...