顺序表功能实现c++

#include<iostream>
#include<string.h>
using namespace std;
class List
{
public:
    List(int size);   //构造函数
    ~List();          //析构函数
    void ClearList(); //清空函数
    bool ListEmpty(); //判空函数
    int ListLength(); //取长度函数
    bool GetElem(int i,int *e);//取值函数
    int LocateElem(int *e);//取对应值地址函数
    bool PriorElem(int *currentElem,int *preElem);//前驱值
    bool NextElem(int *currentElem, int *nextElem);//后继值
    bool ListInsert(int i,int *e);//插入函数
    bool ListDelete(int i,int *e);//删除函数
    void ListTraverse();//遍历函数
    int PrintSize();//取大小函数
private:
    int *m_pList;
    int m_iSize;
    int m_iLength;
};
List::List(int size)
{
    m_iSize=size;
    m_pList=new int[m_iSize];
    m_iLength=0;
}
List::~List()
{
    delete[] m_pList;
    m_pList=NULL;
}
void List::ClearList()
{
    m_iLength=0;
}
bool List::ListEmpty()
{
    return m_iLength==0?true:false;
}
int List::ListLength()
{
    return m_iLength;
}
bool List::GetElem(int i, int *e)
{
    if(i<0||i>=m_iSize)
        return false;
    *e=m_pList[i];
    return true;
}
int List::LocateElem(int *e)
{
    for(int i=0;i<m_iLength;i++)
        if(m_pList[i]==*e)
        {
            return i;
        }
        return -1;
}
bool List::PriorElem(int *currentElem,int *preElem)
{
    for(int i=0;i<m_iLength;i++)
    {
        if(m_pList[i]==*currentElem)
        {
            if(i==0)
            {
                return false;
            }
            *preElem=m_pList[i-1];
            return true;
        }
    }
    return false;
}
bool List::NextElem(int *currentElem,int *nextElem)
{
    int temp=LocateElem(currentElem);
    if(temp<0||temp==m_iLength-1)
    {
        return false;
    }
    else
    {
        *nextElem=m_pList[temp+1];
        return true;
    }
}
void List::ListTraverse()
{
    for(int i=0;i<m_iLength;i++)
    {
        cout<<m_pList[i]<<" ";
    }
}
bool List::ListInsert(int i,int *e)
{
    if(i<0&&i>m_iLength)
    {
        return false;
    }
    for(int k=m_iLength-1;k>=i;k--)
    {
        m_pList[k+1]=m_pList[k];
    }
    m_pList[i]=*e;
    m_iLength++;
    return true;
}
bool List::ListDelete(int i,int *e)
{
    if(i<0||i>m_iLength-1)
    {
        return false;
    }
    *e=m_pList[i];
    for(int k=i;k<m_iLength;k++)
    {
        m_pList[k]=m_pList[k+1];
    }
    m_iLength--;
    return true;
}
int List::PrintSize()
{
    return m_iSize;
}
int main()
{
    //3572918
    int e1=3;int e2=5;int e3=7;int e4=2;int e5=9;int e6=1;int e7=8;
    int temp;
    List *list1 = new List(10);//用构造函数初始化
    cout<<"length: "<<list1->ListLength()<<endl;//取长度
    list1->ListInsert(0,&e1);//插入规定值
    list1->ListInsert(1,&e2);
    list1->ListInsert(2,&e3);
    list1->ListInsert(3,&e4);
    list1->ListInsert(4,&e5);
    list1->ListInsert(5,&e6);
    list1->ListInsert(6,&e7);
    list1->PriorElem(&e4,&temp);//取前驱
    cout<<"temp: "<<temp<<endl;
    list1->NextElem(&e4,&temp);//取后继
    cout<<"temp: "<<temp<<endl;
    list1->GetElem(0,&temp);   //取值
    cout<<"temp: "<<temp<<endl;
    cout<<"length: "<<list1->ListLength()<<endl;//取长度
    cout<<"size: "<<list1->PrintSize()<<endl;   //取大小
    list1->ListDelete(0,&temp); //删除元素
    cout<<"##"<<temp;
    if(!list1->ListEmpty())//判空
    {
        cout<<"not empty"<<endl;
    }
    list1->ClearList();//清除数组
    if(list1->ListEmpty())
    {
        cout<<"empty"<<endl;
    }
    list1->ListTraverse();//顺序表遍历


    return 0;
}
warning:contral reaches end of non-void function    原因是函数的返回忘写了,比如bool类型函数最后忘记写return true/false 或者int函数忘写return 0;

**************************************************************************************
下面是稍作修改 改变了数据类型

Coordinate.h
#ifndef COORDINATE_H
#define COORDINATE_H
#include<iostream>
using namespace std;
class Coordinate
{
    friend ostream &operator<<(ostream &out,Coordinate &coor);
public:
    Coordinate(int x=0,int y=0);
    void printCoordinate();
    bool operator==(Coordinate &coor);
private:
    int m_iX;
    int m_iY;
};
#endif // COORDINATE_H

Coordinate.cpp

using namespace std;
Coordinate::Coordinate(int x,int y)
{
    m_iX=x;
    m_iY=y;
}
ostream &operator<<(ostream &out,Coordinate &coor)
{
    cout<<"("<<coor.m_iX<<","<<coor.m_iY<<")"<<endl;
    return out;
}
void Coordinate::printCoordinate()
{
    cout<<"("<<m_iX<<","<<m_iY<<")"<<endl;
}
bool Coordinate::operator==(Coordinate &coor)
{
    if(this->m_iX==coor.m_iX&&this->m_iY==coor.m_iY)
    {
      return true;
    }
    return false;
}

List.h
#include<iostream>
#include"Coordinate.h"
class List
{
public:
    List(int size);
    ~List();
    void ClearList();
    bool ListInsert(int i,Coordinate *e);
    bool ListDelete(int i,Coordinate *e);
    int ListLocate(Coordinate *e);
    Coordinate GetElem(int i,Coordinate *e);
    bool ListPrior(Coordinate *current,Coordinate *prior);
    bool ListNext(Coordinate *current,Coordinate *next);
    int ListLength();
    bool ListEmpty();
    void ListTraverse();
private:
    Coordinate *m_pList;
    int m_iSize;
    int m_iLength;
};

List.cpp
#include<iostream>
#include"List.h"
#include"Coordinate.h"
using namespace std;
List::List(int size)
{
    m_iSize=size;
    m_pList=new Coordinate[m_iSize];
    m_iLength=0;
}
List::~List()
{
    delete[] m_pList;
    m_pList=NULL;
}
void List::ClearList()
{
    m_iLength=0;
}
bool List::ListInsert(int i,Coordinate *e)
{
    if(i<0||i>m_iLength)
    {
        return false;
    }
    else
    {
        for(int j=m_iLength-1;j>=i;j--)
        {
            m_pList[j+1]=m_pList[j];
        }
        m_pList[i]=*e;
        m_iLength++;
        return true;
    }
}
bool List::ListDelete(int i,Coordinate *e)
{
    *e=m_pList[i];
    if(i<0||i>m_iLength-1)
    {
        return false;
    }
    else
    {
        for(int j=i;j<m_iLength;j++)
        {
            m_pList[j]=m_pList[j+1];
        }
        m_iLength--;
        return true;
    }
}
int List::ListLocate(Coordinate *e)
{
    for(int i=0;i<m_iLength;i++)
    {
        if(m_pList[i]==*e)
        {
            return i;
        }
    }
    return -1;
}
Coordinate List::GetElem(int i,Coordinate *e)
{
    if(i<0||i>m_iLength)
    {
        return -1;
    }
    else
    {
        *e=m_pList[i];
        return *e;
    }
}
bool List::ListPrior(Coordinate *current,Coordinate *prior)
{
    for(int i=0;i<m_iLength;i++)
    {
        if(m_pList[i]==*current)
        {
            if(i==0)
            {
                return false;
            }
            *prior=m_pList[i-1];
            return true;
        }
    }
    return false;
}
bool List::ListNext(Coordinate *current,Coordinate *next)
{
    for(int i=0;i<m_iLength;i++)
    {
      if(m_pList[i]==*current)
      {
          if(i==m_iLength-1)
          {
              return false;
          }
          *next=m_pList[i+1];
          return true;
      }
    }
    return false;
}
int List::ListLength()
{
    return m_iLength;
}
bool List::ListEmpty()
{
    if(m_iLength==0)
        return true;
    return false;
}
void List::ListTraverse()
{
    for(int i=0;i<m_iLength;i++)
    {
        cout<<m_pList[i];
    }
}

mian函数
#include<iostream>
#include"List.h"
using namespace std;
int main()
{
    Coordinate e1(3,6);
    Coordinate e2(4,8);
    Coordinate e3(5,10);
    Coordinate e4(6,12);
    List *list1=new List(10);
    list1->ListInsert(0,&e1);
    list1->ListInsert(1,&e2);
    list1->ListInsert(2,&e3);
    list1->ListInsert(3,&e4);
    Coordinate temp(0,0);
    list1->ListTraverse();
    list1->ListPrior(&e2,&temp);
    cout<<temp<<endl;
    list1->ListNext(&e2,&temp);
    cout<<temp<<endl;
    list1->GetElem(0,&temp);
    cout<<temp<<endl;
    list1->ListDelete(0,&temp);
    cout<<temp<<endl;
    if(!list1->ListEmpty())
    {
        cout<<"not empty"<<endl;
    }
    list1->ClearList();
    if(list1->ListEmpty())
    {
        cout<<"empty"<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值