C++数据结构和算法每天一练(线性表)

#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;
}

时间: 2024-09-20 06:30:50

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

关于数据结构(c语言版)线性表的问题

问题描述 关于数据结构(c语言版)线性表的问题 写完线性表实验代码后,有些错误 不会调试 求大神帮帮忙! ps.错误截图:图片说明 代码: #include #include #include #define LIST_INIT_SIZE 100 //线性表存储空间的初始分量 #define LISTINCREMENT 10 //线性表存储空间的分配增量 typedef struct{ ElemType* elem; //存储空间基址 int length; //当前长度 int listsiz

算法-单链表做线性表的存储

问题描述 单链表做线性表的存储 以带头结点的单链表做线性表的存储表示,编写算法删除表中的偶数序号结点,使(a1,a2,a3,a4,a5...)变成(a1,a3,a5...) 解决方案 #include<stdio.h> #include<malloc.h> typedef struct LNode { int data; struct LNode *next; }*LinkList; LinkList Creat_List(int n) { LinkList head,p,q; h

数据结构与算法07 之哈希表

  哈希表也称为散列表,是根据关键字值(key value)而直接进行访问的数据结构.也就是说,它通过把关键字值映射到一个位置来访问记录,以加快查找的速度.这个映射函数称为哈希函数(也称为散列函数),映射过程称为哈希化,存放记录的数组叫做散列表.比如我们可以用下面的方法将关键字映射成数组的下标:arrayIndex = hugeNumber % arraySize.         哈希化之后难免会产生一个问题,那就是对不同的关键字,可能得到同一个散列地址,即同一个数组下标,这种现象称为冲突,那

算法速成(八)线性表之链表

一:线性表的简单回顾 上一篇跟大家聊过"线性表"顺序存储,通过实验,大家也知 道,如果我每次向 顺序表的头部插入元素,都会引起痉挛,效率比较低下,第二点我们用顺序 存储时,容 易受到长度的限制,反之就会造成空间资源的浪费. 二:链表 对于 顺序表存在的若干问题,链表都给出了相应的解决方案. 1. 概念:其实链表的"每个节点" 都包含一个"数据域"和"指针域". "数据域"中包含当前的数据. "指针

《数据结构与算法 C语言版》—— 第3章 栈 与 队 列

第3章 栈 与 队 列 栈和队列是在软件设计中常用的两种数据结构,它们的逻辑结构与线性表相同.其特点在于操作受到了限制:栈按"后进先出"的规则进行操作,队列按"先进先出"的规则进行操作.故称它们为操作受限制的线性表.

【Java数据结构】线性表

线性表 线性表是最基本.最简单.也是最常用的一种数据结构. 线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的(注意,这句话只适用大部分线性表,而不是全部.比如,循环链表逻辑层次上也是一种线性表(存储层次上属于链式存储),但是把最后一个数据元素的尾指针指向了哨位结点).  我们说"线性"和"非线性",只在逻辑层次上讨论,而不考虑存储层次,所以双向链表和循环链表依旧是线性表. 在数据结构逻辑层次上细分,线性表可分为

线性表-数据结构 算法如何编译运行··跪求各位路过的大神帮帮忙啊

问题描述 数据结构 算法如何编译运行··跪求各位路过的大神帮帮忙啊 #include #include #define ERROR 0 #define OK 1 typedef int Status; typedef int ElemType;//顺序表测试用 const int MaxSize=100; //100只是示例性的数据,可以根据实际问题具体定义 const int Increasement=10; typedef struct{ ElemType *elem; int length

C#数据结构与算法揭秘二 线性结构_C#教程

上文对数据结构与算法,有了一个简单的概述与介绍,这篇文章,我们介绍一中典型数据结构--线性结构. 什么是线性结构,线性结构是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这 种一对一的关系指的是数据元素之间的位置关系,即: (1)除第一个位置的数据元素外,其它数据元素位置的前面都只有一个数据元素: (2)除最后一个位置的数据元素外,其它数据元素位置的后面都只有一个元素.也就是说,数据元素是一个接一个的排

《数据结构与算法 C语言版》—— 2.3线性表的链式表示与实现

2.3线性表的链式表示与实现 线性表的顺序存储结构的特点是逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,它的存储位置可用一个简单.直观的公式来表示.然而,从另一方面来看,这个特点也造成了这种存储结构的弱点:在作插入或删除操作时,需移动大量元素.本节我们将讨论线性表的另一种表示方法--链式存储结构,其特点是用一组地址任意的存储单元存储线性表中的数据元素.由于它不要求逻辑上相邻的元素在物理位置上也相邻,因此它没有顺序存储结构所具有的弱点,但同时也失去了顺序表随机存取的特点