C++数据结构--顺序表

基础知识

顺序表是用顺序存储结构来表示元素之间的线性关系。顺序表主要由数组和数组长度两部分构成。常用的操作:初始化、取值、查询、插入、删除、清空等操作。

线性表的顺序表在c++的STL库中可以用vector 来实现

#include<vector>

以下是自己定义的顺序表数据结构

1.头文件sqlist.h

#pragma once
#ifndef SQLIST
#define SQLIST
#define MAXLEN 100

struct sqlist
{
	int *elem;//首元的下标,int可以替换成其他数据结构
	int length;
};//定义一个顺序表

//顺序表的操作类
class sqList {
public:
	sqList() {
		sq.length = 0;
		sq.elem = new int[MAXLEN];//分配空间
	}//构造函数,初始化
	~sqList();//析构函数
	int GetElem(int i);//获取第i个位置上的值
	int LocateELem(int el);//获取el在顺序表中的位置
	int InseSert(int i, int el);//在第i个位置插入el
	int Delete(int i);//删除第i个元素
	void addToTail(int n);//初始化构造顺序表
	void print();//遍历

private:
	sqlist sq;
};
#endif // !CLIST


2.源文件sqList.cpp

#include<iostream>
#include"sqList.h"

using namespace std;

sqList::~sqList() {
	sq.length = 0;
}

int sqList::GetElem(int i) {
	if (i < 1 || i>sq.length)
		return false;
	else {
		cout << sq.elem[i - 1] << endl;
		return sq.elem[i - 1];
	}
}
int sqList::LocateELem(int el) {
	int i = 0;
	for (i; i < sq.length; i++) 
		if (sq.elem[i] == el)
			return i+1;
	return false;
}
int sqList::InseSert(int i, int el) {
	if (i < 1 || (i > sq.length + 1))
		return false;
	i = i - 1;
	int q = 0;
	for (i; sq.elem[i] != NULL; i++) {
		q= sq.elem[i];
		sq.elem[i] = el;
		el = q;
	}
	sq.length = sq.length + 1;
	return true;
}

int sqList::Delete(int i) {
	if (i < 1 || (i > sq.length + 1))
		return false;
	i = i - 1;
	int q = 0;
	for (i; i<sq.length-1; i++) {
		q = sq.elem[i+1];
		sq.elem[i] = q;
	}
	sq.length = sq.length - 1;
	return true;
}


void sqList::addToTail(int n) {
	int i = 0;
	for (i; i < n; i++)
		cin >> sq.elem[i];
	sq.length = n;
}
void sqList::print() {
	int i = 0;
	cout << "遍历结果" << endl;
	for (i; i < sq.length; i++) 
		cout << sq.elem[i] << endl;
	cout << "顺序表长度:";
	cout << sq.length << endl;
}


int main() {
	sqList L;
	cout << "构造一个顺序表" << endl;
	L.addToTail(6);
	L.print();
	cout << "删除第3个位置的值" << endl;
	L.Delete(3);
	L.print();
	cout << "获取第2个位置上的值:";
	L.GetElem(2);
	cout << "在第二个位置插入";
	L.InseSert(2, 8);
	L.print();
	cout << "查找2在顺序表位置" << endl;
	cout << L.LocateELem(2) << endl;;
	cout << "清空顺序表"<<endl;
	L.~sqList();
	L.print();
	return 0;
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Qt 中,可以通过自定义 QHeaderView 来实现在表头添加控件的功能。具体步骤如下: 1. 定义一个继承自 QHeaderView 的子类,例如 MyHeaderView。 2. 在 MyHeaderView 中重写 paintSection() 函数,该函数会在绘制表头时被调用。 3. 在 paintSection() 函数中,首先调用父类的 paintSection() 函数,然后在需要添加控件的位置进行控件的绘制。 以下是一个添加 QPushButton 控件的例子: ```cpp class MyHeaderView : public QHeaderView { public: MyHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent) { } protected: void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override { // 调用父类的 paintSection() 函数 QHeaderView::paintSection(painter, rect, logicalIndex); // 在第一列表头的右侧绘制一个 QPushButton if (logicalIndex == 0) { QPushButton button("Button", const_cast<QWidget*>(static_cast<const QWidget*>(parent()))); QRect buttonRect = QRect(rect.right() - 20, rect.top() + 2, 18, 18); // 控件的位置和大小 button.setGeometry(buttonRect); button.show(); } } }; ``` 使用时,将表格的水平表头和垂直表头分别设置为 MyHeaderView 类的实例即可: ```cpp QTableWidget table; table.setHorizontalHeader(new MyHeaderView(Qt::Horizontal, &table)); table.setVerticalHeader(new MyHeaderView(Qt::Vertical, &table)); ``` 注意:由于 QPushButton 控件是在 paintSection() 函数中创建的,因此需要使用 const_cast 和 static_cast 将 parent 指针转换为非 const 类型。同时,由于 QPushButton 控件是在 MyHeaderView 的生命周期内创建的,因此需要在 paintSection() 函数中设置其位置和大小,并在 paintSection() 函数之外手动调用其 show() 函数显示控件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值