数据结构:C++ 实现顺序表

参考:https://blog.csdn.net/ebowtang/article/details/43094041

//SeqList.h定义结构以及需要实现的方法
#ifndef SEQLIST_H
#define SEQLIST_H

#include<iostream>
using namespace std;

class SeqList{
public:
    SeqList(int size=defaultSize){
        if(size>0){
            maxSize=size;
            length=0;
            elements=new int[maxSize];
            for(int i=0;i<maxSize;i++){
                elements[i]=NULL;
            }
        }else{
            cout<<"Error Length of SeqList!"<<endl;
        }
    }

    //在列表尾部插入数据
    bool InsertElement(int data);
    //得到指定位置的数据
    int GetElement (int location);
    //删除指定位置的数据
    bool DelElement(int location);
    //更改指定位置的数据
    bool ChangeElement(int locatioon,int data);
    //打印列表
    bool PrintList();
    //查找数据,返回位置
    int FindElement(int data);
    //判断列表是否为空
    bool isEmpty(SeqList *L);
    //初始化列表
    bool InitList(int nlen);
    //清除列表
    void ClearList(SeqList *L);
    //销毁列表
    void DestroyList();
    //颠倒列表元素
    void ConverseList();
    //得到列表长度
    int GetLength() const {return length;}

private:
    static const int defaultSize=10;
    int *elements;
    int maxSize;
    int length;
};

#endif
//SeqList.cpp实现方法
#include<iostream>
#include"SeqList.h"
using namespace std;

bool SeqList::InsertElement(int data){
    int curIndex=length;
    if(length>maxSize){
        return false;
    }else{
        elements[curIndex]=data;
        length++;
        return true;
    }
}

bool SeqList::InitList(int nlen){
    int nint=0;
    for(int i=0;i<nlen;i++){
        InsertElement(nint++);
    }
    return true;
}

int SeqList::GetElement(int location){
    if(location<=0 || location>length){
        cout<<"参数无效"<<endl;
        return 0;
    }else{
        return elements[--location];
    }
}

bool SeqList::DelElement(int location){
    if(location<=0 || location>length){
        cout<<"参数无效"<<endl;
        return false;
    }else{
        for(int i=--location;i<length-1;i++){
            int j=i+1;
            elements[i]=elements[j];
        }
        --length;
        return true;
    }
    
}

bool SeqList::ChangeElement(int location,int data){
    if(location<=0 || location>length){
        cout<<"参数无效"<<endl;
        return false;
    }else{
        elements[--location]=data;
        return true;
    }
}

bool SeqList::PrintList(){
    for(int i=0;i<length;++i){
        cout<<elements[i]<<" ";
    }
    cout<<endl;
    return true;
}

int SeqList::FindElement(int data){
    for(int i=0;i<length;i++){
        if(elements[i]==data){
            return ++i;
        }
    }
    cout<<"没有更改元素"<<endl;
    return 0;
}

bool SeqList::isEmpty(SeqList *L){
    if(L->length==0){
        return true;
    }else{
        return false;
    }
}

void SeqList::ClearList(SeqList *L){
    for(int i=0;i<length;++i){
        elements[i]=0;
    }
    L->length=0;
    L->maxSize=0;
}

void SeqList::DestroyList(){
    length=0;
    maxSize=0;
    delete[] elements;
    elements=NULL;
}

void SeqList::ConverseList(){
    for(int i=0;i<length/2;i++){
        int temp;
        temp=elements[i];
        elements[i]=elements[length-1-i];
        elements[length-i-1]=temp;
    }
}
//main.cpp 调用方法,实现结构功能化访问
#include"SeqList.cpp"
#include<iostream>
using namespace std;

void deleteElement(SeqList *list){
    int nPos=0;
    cout<<"您想要删除的那个元素的位置:";
    cin>>nPos;
    while(1){
        if(nPos>list->GetLength()){
            cout<<"输入过大,重新输入";
            cin>>nPos;
        }else{
            break;
        }
    }
    list->DelElement(nPos);
    list->PrintList();
    cout<<"现在顺序表的长度为:"<<list->GetLength()<<endl;
}

void changeElement(SeqList *list){
    int nPos1=0,data=10;
    char ans='n';
    do{
        cout<<"请输入您想要改变的数据和所在位置,亲,先输入位置哟:";
        cin>>nPos1>>data;
        list->ChangeElement(nPos1,data);
        cout<<"继续修改?(Y/N)";
        cin>>ans;
    }while(ans=='y' || ans=='Y');
    cout<<"更改后的顺表为:";
    list->PrintList();
}

void sortElement(SeqList *list){
    int sdata=0;
    cout<<"请输入您查找的元素:";
    cin>>sdata;
    int location=list->FindElement(sdata);
    cout<<"您所查找元素的位置:"<<location<<endl;
}

void clearElement(SeqList *list){
    list->ClearList(list);
    if(list->isEmpty(list)){
        cout<<"顺序表已被清空"<<endl;
    }else{
        cout<<"顺序表还原元素"<<endl;
    }
}

void menu(){
    cout<<"Please choice number:"<<endl;
    cout<<"1.Delete element"<<endl;
    cout<<"2.Change element"<<endl;
    cout<<"3.Sort element"<<endl;
    cout<<"4.Clear element"<<endl;
    cout<<"5.Exit system"<<endl;
}

int main(){
    int nlen=0;
    cout<<"请输入顺序表的长度:";
    cin>>nlen;
    SeqList list(nlen);
    list.InitList(nlen);
    cout<<"初始化后的内容为:"<<endl;
    list.PrintList();
    int choice=0;
    while(1){
        menu();
        cin>>choice;
        switch(choice){
            case 1:deleteElement(&list);break;
            case 2:changeElement(&list);break;
            case 3:sortElement(&list);break;
            case 4:clearElement(&list);break;
        }
        if(choice==5){
            cout<<"Welcome use again!"<<endl;
            break;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值