C++实现线性表的分析与实现(图形展示)

  数据结构课程设计抽到这个题目,实现线性表的分析与设计。历时一周,实现的比较简陋但最终完成,采用Qt实现课设。

  这是课设的基本要求:

①   设计并实现线性表

②   线性表分别采取数组(公式化描述)、单链表、双向链表、间接寻址存储方式

③   针对随机产生的线性表实例,实现线性表的插入、删除、搜索操作动态演示(图形演示)。

先展示一下我的效果图



这里我只展示数组的界面实现。

首先是基本的方法。这里是.h文件

#ifndef LINEARLIST_H
#define LINEARLIST_H


class LinearList//公式化描述
{
public:
    LinearList(int MaxListSize=20);
    ~LinearList();
    LinearList &create();//随机产生一个长度为5的线性表
    bool IsEmpty() const {return length==0;}
    int Length()const{return length;}
    bool Find(int k,int &x)const;//返回第k个元素到x中
    int Search(const int &x);//删除x所在的位置
    LinearList &Delete(int k,int &x);//删除第k个元素并将它返回到x中
    LinearList &Insert(int k,const int &x);//在第k个元素之后插入x
    LinearList &OutPut();


    int length;
    int MaxSize;
    int *element;
 
};

#endif // LINEARLIST_H

下面是.cpp文件

#include "linearlist.h"
#include <iostream>
#include <Qt>
#include <QtDebug>
#include<cmath>
#include <QMessageBox>
LinearList::LinearList(int MaxListSize)
{
        MaxSize=MaxListSize;
        element=new int[MaxSize];
        length=0;
   
}

LinearList::~LinearList(){
    int x;
    for(int i=0;i<length;i++){
       Delete((i+1),x);
    }
    length=0;

}
LinearList &LinearList::create(){

     srand( (unsigned)time( NULL ) );
    for(int i=0;i<5;i++){
        int a = 1+rand() % 1000;
        this->Insert(i,a);

    }
    QMessageBox message(QMessageBox::NoIcon, "Title", "We have create a linearlist for you.");
    message.exec();
    return *this;
}

bool LinearList::Find(int k, int &x) const{
    if(k<1||k>length)return false;
    x=element[k-1];
    return true;

}

int LinearList::Search(const int &x) {
    for(int i=0;i<length;i++){
        if(element[i]==x){
            //获取数组的大小

           return ++i;
        }
    }
    return 0;
}
LinearList& LinearList::Insert(int k,const int& x)
{

        if(k<0||k>length)
        {
            QMessageBox message(QMessageBox::NoIcon, "Title", "The position that you input is wrong.");
            message.exec();
            return *this;
        }

        if(length==MaxSize) {
            QMessageBox message(QMessageBox::NoIcon, "Title", "The array is full.");
            message.exec();

            exit(0);
        }

        for(int i=length-1;i>=k;i--){
            element[i+1]=element[i];
        }
        element[k]=x;
        length++;
        return *this;
}

LinearList &LinearList::Delete(int k,int &x){
    if(Find(k,x)){
        for(int i=k;i<length;i++){
            element[i-1]=element[i];
        }
            length--;
            return *this;

    }
    else{

        QMessageBox message(QMessageBox::NoIcon, "Title", "The number that you input is not found.");
        message.exec();
        return *this;
    }
}

LinearList & LinearList:: OutPut(){
    std::cout<<"OutPut"<<std::endl;
    for(int i=0;i<Length();i++){

         std::cout<<element[i]<<std::endl;
    }
}

在下面就是用QT实现的界面了,这里是mainwindow.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H



#include <QListWidget>
#include <vector>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QStatusBar>
#include <QLineEdit>
#include <QPushButton>
#include <QPainter>
#include "linearlist.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
     QListWidget *qw;
       std::vector<QListWidgetItem> v;//储存部件
       //制作一个部件
       QWidget* makepart(int num,int id);
       //向qw中添加一个组件,num是组件的值,id是箭头的种类,0是normal,1是double,2是search
       void addWidget(int num,int id);
       void develop(int *array,int siz,int p);
       //把array里的第i个(从0开始)的箭头变为第id
       void change(int *array,int i,int id);
       void deleteAll();
       void LinearPaint(int p);
      

    void initMenu();
    void initInput();
    void setInputVisual(bool a,int i);
    void showWindow(QString str,int num);
    int queryNumber;
    int queryPosition;
    int currentNumber;


    LinearList linear;
   
   


private:
    Ui::MainWindow *ui;
    QMenu* menu[4];
    QAction *action[16];
    QMenuBar* menuBar ;
    QStatusBar* status ;
    QLineEdit* posi[12];
    QLineEdit* number[12];
    QPushButton* ok[12];
    QPainter* painter;
public slots:
    void initConnect();


    void One();
    void Two();
    void TwoRespond();
    void Three();
    void ThreeRespond();
    void Four();
    void FourRespond();

};

#endif // MAINWINDOW_H
还有cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <Qt>
#include <QtDebug>
#include <QMessageBox>
#include <iostream>
#include <QLCDNumber>
#include <QLabel>
#include "form.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qw=ui->listWidget;
       // int a[]={1,2,3,4,5,6};
       // develop(a,6);
       // qw->setFlow(QListView::TopToBottom);
       // v.clear();
    status = new QStatusBar(this);
    initMenu();
    menuBar->setGeometry(0,0,this->width(),30);
    initInput();
    initConnect();



}

//画一个一个的小item
QWidget* MainWindow::makepart(int num,int id)
{
    QWidget *w=new QWidget();
    QVBoxLayout *layout=new QVBoxLayout(w);
    QLCDNumber *lcd=new QLCDNumber();
    lcd->display(num);
    QLabel *label=new QLabel();
    QImage *img;
    if(id==0)
        img=new QImage("E://picture/normal.png");
    else if(id==1)
        img=new QImage("E://picture/double.png");
    else img=new QImage("E://picture/search.png");

    label->setPixmap(QPixmap::fromImage(*img));

    int wii=label->width();
    int h=(rect().y()+rect().height())/2;
    img->scaled(wii,h,Qt::KeepAspectRatio);
   // img->scaled(40,40,Qt::KeepAspectRatio);
    layout->addWidget(lcd);
    layout->addWidget(label);
    w->setLayout(layout);

    return w;
}

void MainWindow::showWindow(QString str,int num){
    Form *f=new Form();
    f->lable->setText(str);
    f->number->display(num);
    f->show();
}

void MainWindow::addWidget(int num, int id)
{
    QListWidgetItem *item=new QListWidgetItem();
    qw->addItem(item);
    item->setSizeHint(QSize(50,100));
    qw->setItemWidget(item,makepart(num,id));
    v.push_back(*item);
}
void MainWindow::develop(int *array,int siz,int p)
{
    for(int i=0;i<siz;i++)
    {
        if(i==p-1){
            addWidget(array[i],0);
        }else{
            addWidget(array[i],-1);
        }

    }
}
void MainWindow::change(int *array,int i,int id)
{

    qw->setItemWidget(&v[i],makepart(array[i],id));
}


void MainWindow::initMenu(){
    menu[0] = new QMenu("公式化描述");
    action[0]=new QAction("生成",this);
    menu[0]->addAction(action[0]);
    action[1]=new QAction("插入",this);
    menu[0]->addAction(action[1]);
    action[2]=new QAction("删除",this);
    menu[0]->addAction(action[2]);
    action[3]=new QAction("搜索",this);
    menu[0]->addAction(action[3]);


    menu[1] = new QMenu("单链表");
    action[4]=new QAction("生成",this);
    menu[1]->addAction(action[4]);
    action[5]=new QAction("插入",this);
    menu[1]->addAction(action[5]);
    action[6]=new QAction("删除",this);
    menu[1]->addAction(action[6]);
    action[7]=new QAction("搜索",this);
    menu[1]->addAction(action[7]);

    menu[2] = new QMenu("双向链表");
    action[8]=new QAction("生成",this);
    menu[2]->addAction(action[8]);
    action[9]=new QAction("插入",this);
    menu[2]->addAction(action[9]);
    action[10]=new QAction("删除",this);
    menu[2]->addAction(action[10]);
    action[11]=new QAction("搜索",this);
    menu[2]->addAction(action[11]);

    menu[3] = new QMenu("间接寻址");
    action[12]=new QAction("生成",this);
    menu[3]->addAction(action[12]);
    action[13]=new QAction("插入",this);
    menu[3]->addAction(action[13]);
    action[14]=new QAction("删除",this);
    menu[3]->addAction(action[14]);
    action[15]=new QAction("搜索",this);
    menu[3]->addAction(action[15]);

    menuBar = new QMenuBar(this);
    menuBar->addMenu(menu[0]);
    menuBar->addMenu(menu[1]);
    menuBar->addMenu(menu[2]);
    menuBar->addMenu(menu[3]);



}

void MainWindow::initInput(){
 QHBoxLayout *layout=new QHBoxLayout();
    for(int i=0;i<12;i++){

        posi[i]=new QLineEdit();
        layout->addWidget(posi[i]);
        posi[i]->setPlaceholderText("位置");
       // QIntValidator v( 0, 100,this  );
        //posi[i]->setValidator(&v);
      //  posi[i]->setGeometry(10,440,200,50);

        number[i]=new QLineEdit();
        number[i]->setPlaceholderText("数值");
        layout->addWidget(number[i]);
      //  number[i]->setValidator(&v);
       // number[i]->setGeometry(230,440,200,50);

        ok[i]=new QPushButton("确定");
       // ok[i]->setGeometry(460,440,200,50);
        layout->addWidget(ok[i]);
        setInputVisual(false,i);
        ui->widget->setLayout(layout);
    }
      currentNumber=0;
}

void MainWindow::setInputVisual(bool a,int i){
    posi[i]->setVisible(a);
    number[i]->setVisible(a);
    ok[i]->setVisible(a);
    currentNumber=i;
   /*
    if(a){
         QHBoxLayout *layout=new QHBoxLayout();
          layout->addWidget(posi[i]);
            layout->addWidget(number[i]);
             layout->addWidget(ok[i]);

              ui->widget->setLayout(layout);
    }*/




}

void MainWindow::initConnect()
{

   connect(action[0],SIGNAL(triggered()),this,SLOT(One()));
    connect(action[1],SIGNAL(triggered()),this,SLOT(Two()));
    connect(action[2],SIGNAL(triggered()),this,SLOT(Three()));
    connect(action[3],SIGNAL(triggered()),this,SLOT(Four()));
 
  

     connect(ok[0],SIGNAL(clicked(bool)),this,SLOT(TwoRespond()));
      connect(ok[1],SIGNAL(clicked(bool)),this,SLOT(ThreeRespond()));
      connect(ok[2],SIGNAL(clicked(bool)),this,SLOT(FourRespond()));
  
}

void MainWindow::deleteAll()
 {
     int cnt=qw->count();
     v.clear();
     QListWidgetItem *item;
     for(int i=0;i<cnt;i++)
     {
         item=qw->takeItem(1);
         delete item;
     }
     item=qw->takeItem(0);
     delete item;
 }
void MainWindow::LinearPaint(int p){
    deleteAll();
    int a[linear.length];
    for(int i=0;i<linear.Length();i++){
        a[i]=linear.element[i];
   }
    develop(a,linear.length,p);
    /*
    if(p!=0){
        std::cout<<"This change"<<std::endl;
        change(a,p-1,0);
    }*/
    qw->setFlow(QListView::TopToBottom);

}
void MainWindow::One(){
    //创建公式化描述
    if(linear.length!=0){
        linear.~LinearList();
        std::cout<<"again"<<std::endl;

    }
    if(chain.Length()!=0){
        chain.~Chain();
    }
    if(doublechain.Length()!=0){
        doublechain.~Double();
    }
    if(directlist.length!=0){
        directlist.~IndirectList();
    }
        linear.create();
        LinearPaint(0);


        //将图形打印出来

}
void MainWindow::Two(){
    //公式化插入
    setInputVisual(false,currentNumber);
    setInputVisual(true,0);
   // connect(ok[0],SIGNAL(clicked(bool)),this,SLOT(TwoRespond()));
}
void MainWindow::TwoRespond(){
    if(!ok[0]->isVisible()){
        return;
    }
    QString p=number[0]->text();
    if(!p.isEmpty()){
        queryNumber=p.toInt();
        //std::cout<<queryNumber<<std::endl;
    }
    else{
        std::cout<<"MainWindow the number is wrong."<<std::endl;
        exit(0);
    }

    QString p1=posi[0]->text();

    if(!p1.isEmpty()){
        queryPosition=p1.toInt();
        // std::cout<<queryPosition<<std::endl;
    }
    else{
        std::cout<<"MainWindow the posi is wrong."<<std::endl;
        exit(0);
    }
    linear.Insert(queryPosition-1,queryNumber);

    //在这里对图形进行更新,修改
     LinearPaint(queryPosition);



}
void MainWindow::Three(){
    //公式化删除
    setInputVisual(false,currentNumber);
    setInputVisual(true,1);

   // connect(ok[1],SIGNAL(clicked(bool)),this,SLOT(ThreeRespond()));

}
void MainWindow::ThreeRespond(){
    if(!ok[1]->isVisible()){
        return;
    }
   //删除位置。将num设为不可输入
    if(linear.length==0){
         QMessageBox message(QMessageBox::NoIcon, "Title", "The linearlist is empty.");
         message.exec();
         exit(0);
    }

    QString p1=posi[1]->text();

    if(!p1.isEmpty()){
        queryPosition=p1.toInt();
       //  std::cout<<queryPosition<<std::endl;
    }
    else{
        QMessageBox message(QMessageBox::NoIcon, "Title", "MainWindow the posi is wrong.");
        message.exec();

        exit(0);
    }
    int returnNumber;
    linear.Delete(queryPosition,returnNumber);
  //  number[1]->setText(QString(""+returnNumber));
    //linear.OutPut();
    LinearPaint(0);

}
void MainWindow::Four(){
    //公式化搜索
    setInputVisual(false,currentNumber);
    setInputVisual(true,2);

   // connect(ok[2],SIGNAL(clicked(bool)),this,SLOT(FourRespond()));

}
void MainWindow::FourRespond(){

    if(!ok[2]->isVisible()){
        return;
    }
    //公式化搜索

    if(linear.length==0){
         QMessageBox message(QMessageBox::NoIcon, "Title", "The linearlist is empty.");
         message.exec();
         exit(0);
    }
    QString p1=number[2]->text();

    if(!p1.isEmpty()){
        queryNumber=p1.toInt();

    }
    else{
        QMessageBox message(QMessageBox::NoIcon, "Title", "MainWindow the posi is wrong.");
        message.exec();

        exit(0);
    }

     int returnNumber=linear.Search(queryNumber);
     QString str="The linearlist search position is";
     showWindow(str,returnNumber);
}MainWindow::~MainWindow(){    delete ui;}
 


 


  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值