Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32590
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 360
  • 用 户 组: 普通用户
  • 注册时间: 2021-09-17 18:39
文章分类

全部博文(35)

文章存档

2021年(35)

我的朋友

分类: C/C++

2021-09-24 18:27:39

#include
using namespace std;
const int maxsize =20;
class sqlist
{
    private:
        int date[maxsize]; // 存放顺序表中的元素
        int length; // 存放顺序表的长度
    public:
        sqlist(){length=0;}
        sqlist(int a[],int n); // 构造函数
        void display(); // 输出顺序表L中的所有元素
        int llength(); // 求顺序表的长度
        int get(int i); // 求顺序表中某序列号的元素值
        int locate(int i); // 按元素查找其第一个序号位置
        void insert(int i,int e); // 在位置i插入数据元素e
        void deleted(int i); // 在位置i删除数据元素
};
//类的实现
sqlist::sqlist(int a[],int n)
{
    for(int i=0;i     {
        date[i]=a[i];
    }
    length=n;
}
void sqlist::display()
{
    cout<<"此时的所有数据排列:";
    for(int i=0; i     {
        cout<     }
    cout< }
int sqlist::llength()
{
    cout<<"表长:";
    return length;
}
int sqlist::get(int i)
{
    if(i<0||i>maxsize) throw"溢出";
    cout<<"该位置的数据为:";
    return date[i-1];
}
int sqlist::locate(int i)
{
    for(int x=0;x     {
        if(date[x]==i)
        {
            return x+1;
            break;
        }
    }
    return 0;
}
void sqlist::insert(int i,int e)
{
    if(i<0||i>maxsize) throw"溢出";
    for(int j=length; j>=i; j--)
    {
        date[j]=date[j-1];
    }
    date[i-1]=e;
    length++;
}
void sqlist::deleted(int i)
{
    if(i<0||i>length) throw"溢出";
    for(int j=i-1;j     {
        date[j]=date[j+1];
    }
    length--;
}
//主函数部分
int main()
{
    int i,n;
    int a[5]={1,2,3,4,5};
    sqlist std;
    sqlist(a[],5);
    cout<<"输出数据:"<     cout<<"此时表长:"<     cout<<"请输入序列号:";
    cin>>i;
    cout<     cout<     cout<<"请输入所求值:";
    cin>>i;
    cout<     cout<<"此时表长:"<     cout<<"请输入插入位点和数值:";
    cin>>i>>" ">>n;
    std.insert(i,n);
    cout<<"此时表长:"<     cout<<"请输入删除位点:";
    cin>>i;
    std.deleted(i);
    cout<<"此时表长:"<     return 0;
}

阅读(623) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~