数据结构课程设计抽到这个题目,实现线性表的分析与设计。历时一周,实现的比较简陋但最终完成,采用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
#include
#include
#include
#include
LinearList::LinearList(int MaxListSize)
{
MaxSize=MaxListSize;
element=new int[MaxSize];
length=0;
}
LinearList::~LinearList(){
int x;
for(int i=0;iInsert(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;ilength)
{
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
在下面就是用QT实现的界面了,这里是mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "linearlist.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QListWidget *qw;
std::vector 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
#include
#include
#include
#include
#include
#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;isetItemWidget(&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;itakeItem(1);
delete item;
}
item=qw->takeItem(0);
delete item;
}
void MainWindow::LinearPaint(int p){
deleteAll();
int a[linear.length];
for(int i=0;isetFlow(QListView::TopToBottom);
}
void MainWindow::One(){
//创建公式化描述
if(linear.length!=0){
linear.~LinearList();
std::cout<<"again"<isVisible()){
return;
}
QString p=number[0]->text();
if(!p.isEmpty()){
queryNumber=p.toInt();
//std::cout<text();
if(!p1.isEmpty()){
queryPosition=p1.toInt();
// std::cout<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<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;}