数据结构——线性表与链表

看了一下其他链接,真正的循序表应该支持随机索引的,也就是new的时候就将内存分配好。

这里是像链表一样申请了,当做链表看吧

参考链接:数据结构与算法------顺序表和链表的优缺点(区别、特点)详解_顺序表和链表的优缺点(区别、特点)详解-CSDN博客

代码

cpp 复制代码
#include<iostream>
#include<cmath>
using namespace std;
// 具体的元素 ,这里以书本为例 
struct element {
	element * next;
	int index; // 书本编号 
	int value;	// 价格 
	string name; // 书本名称 
};
// 代表序列整体信息 
struct sequence{
	element * next; // 指向下一个序列的指针
	int size; // 当前元素树木个数
	int maxIndex; // 最大编号 
	int sum; 
	// 寻找最后一个元素指針 
	element* findLast() {
		element* it = this->next;
		while(it->next) {
			it = it->next;
//			cout<<"寻找下一个"<<endl;
		}
		return it;	
	} 
	// 最后一个值 
	int last() {
		element* it = this->findLast();
		return it->value;
	}
	// 第一个值 
	int front() {
		element* it = this->next->next;
		return it->value;
	}
	// 向顺序表添加末尾一个元素 
	void push(int price,string name ) {
		element* newEle = new element();
//		cout<<"断点1"<<endl; 
		newEle->index = this->maxIndex;
		newEle->value = price;
		newEle->name = name;
//		cout<<"断点2"<<endl; 
		// 将新节点加入最后 
		element* last = this->findLast();
		last->next = newEle;
//		cout<<"断点3"<<endl; 
		// 更新数据
		this->size ++; 
		this->maxIndex ++; // 确保每本书的Id不一样 
		this->sum += newEle->value;
	} 
	// 向顺序表删除第一个元素 ,返回被删除的值 ,假设一切操作合理 
	int pop() {
//		cout<<"断点4"<<endl; 
		element* ele = this->next->next;
		this->next->next = ele->next;
//		cout<<"断点5"<<endl; 
		// 更新数据 
		this->size --;
		this->sum -= ele->value;
		return ele->value;
	}
	// 打印順序表状态
	void printSate()  {
		cout<<"序列长度:"<<this->size<<endl;
		cout<<"序列总和:"<<this->sum<<endl;
	}
	// 清空
	void reset() {
		// 后续加上回收 
		this->next = new element();
		this->size = 0;
		this->sum = 0;
	} 
	// 获取第x个元素,如果超出则显示最后一个元素 
	int getElement(int num) {
		int len = min(num,this->size); 
		element* it = this->next;
		for(int i=0 ;i<len;i++) {
			it = it->next;
		}
		return it->value;
	} 
	// 按照name字段查找 返回index 
	int findByName(string name){
		element* it = this->next;
		while(it->next) {
			it = it->next;
			if(it->name == name) {
				return it->index;
			} 
		}
		return -1;// 没找到 
	}
	// 遍历元素 打印书本价格 
	void printELementsValue() {
		element* it = this->next;
		while(it->next) {
			it = it->next;
			cout<<it->value<<" ";
		}
		cout<<endl;
	} 
	// 遍历元素 打印书本名称 
	void printELementsName() {
		element* it = this->next;
		while(it->next) {
			it = it->next;
			cout<<it->name<<" ";
		}
		cout<<endl;
	} 
}; 
// 思考:
// 1.无效的操作如何阻止,比如在没有元素时的pop和front和last操作? 
// 2. 如何将指针->引用,使用属性.引用 代替? 

// 初始化一个空顺序表
sequence* initSequence() {
	sequence* a = new sequence();
    a->size = 0;
    a->sum = 0;
    a->maxIndex = 0; 
    a->next = new element(); // 确认必须会有一个element 
    return a;
}


// 测试样例 1 (Accept)只有push 
void test1(sequence *seq);
// 测试样例 2 (Accept)push + pop
void test2(sequence *seq);
// 测试样例 3 (Accept)push + reset
void test3(sequence *seq);
// 测试样例 4 (Accept)push + pop + front + last
void test4(sequence *seq);
// 测试样例 5 (Accept)push + pop + printELementsValue + printELementsName
void test5(sequence *seq);

signed main() {
	cout<<"断点start"<<endl; 
    sequence* seq = initSequence(); 
	test1(seq);
	test2(seq);
	test3(seq);
	test4(seq);
	test5(seq);
	
	cout<<"断点end"<<endl; 
    return 0;
}

void test1(sequence *seq) {
	seq->push(1,"Alice");
	seq->push(2,"Bob");
	seq->push(3,"安徒生");
	seq->push(2,"Alice");
	seq->push(7,"格林语言");
	seq->push(5,"中华上下五千年");
	seq->printSate();
}

void test2(sequence *seq) {
	seq->push(3,"钢铁是怎样炼成的");
	seq->push(2,"A");
	seq->push(1,"B");
	seq->push(5,"V");
	seq->push(7,"D");
	seq->printSate();
	seq->pop();
	seq->pop();
	seq->printSate();
}
void test3(sequence *seq) {
	test1(seq);
	seq->reset();
	seq->printSate();
}
void test4(sequence *seq){
	test2(seq);
	cout<<seq->front()<<endl;
	cout<<seq->last()<<endl;
}
void test5(sequence *seq){
	test1(seq);
	test2(seq);
	seq->printELementsValue();
	seq->printELementsName();
}

这里的想法是,线性表是一个独立的个体,但是它又由多个元素组成

于是将线性表设计成单独的结构体,此外的元素对象单独再设计另一个结构体

cpp 复制代码
// 具体的元素 ,这里以书本为例 
struct element {
	element * next;
	int index; // 书本编号 
	int value;	// 价格 
	string name; // 书本名称 
};
// 代表序列整体信息 
struct sequence{
	element * next; // 指向下一个序列的指针
	int size; // 当前元素树木个数
	int maxIndex; // 最大编号 
	int sum; 
}

我们创建好结构体之后,使用 new()就可以创建一个该对象。

为了能够检测到sequence对象是否为空,我们会再第一个元素设置一个空的元素对象,当做虚拟头结点,放在初始化的时候

cpp 复制代码
// 初始化一个空顺序表
sequence* initSequence() {
	sequence* a = new sequence();
    a->size = 0;
    a->sum = 0;
    a->maxIndex = 0; 
    a->next = new element(); // 确认必须会有一个element 
    return a;
}

在结构体下面写一些函数,使用this指代当时实例化的对象,其再运行时确定具体值,用来引用自身属性。

cpp 复制代码
// 寻找最后一个元素指針 
	element* findLast() {
		element* it = this->next;
		while(it->next) {
			it = it->next;
//			cout<<"寻找下一个"<<endl;
		}
		return it;	
	} 
	// 最后一个值 
	int last() {
		element* it = this->findLast();
		return it->value;
	}
	// 第一个值 
	int front() {
		element* it = this->next->next;
		return it->value;
	}
	// 向顺序表添加末尾一个元素 
	void push(int price,string name ) {
		element* newEle = new element();
//		cout<<"断点1"<<endl; 
		newEle->index = this->maxIndex;
		newEle->value = price;
		newEle->name = name;
//		cout<<"断点2"<<endl; 
		// 将新节点加入最后 
		element* last = this->findLast();
		last->next = newEle;
//		cout<<"断点3"<<endl; 
		// 更新数据
		this->size ++; 
		this->maxIndex ++; // 确保每本书的Id不一样 
		this->sum += newEle->value;
	} 
	// 向顺序表删除第一个元素 ,返回被删除的值 ,假设一切操作合理 
	int pop() {
//		cout<<"断点4"<<endl; 
		element* ele = this->next->next;
		this->next->next = ele->next;
//		cout<<"断点5"<<endl; 
		// 更新数据 
		this->size --;
		this->sum -= ele->value;
		return ele->value;
	}
	// 打印順序表状态
	void printSate()  {
		cout<<"序列长度:"<<this->size<<endl;
		cout<<"序列总和:"<<this->sum<<endl;
	}
	// 清空
	void reset() {
		// 后续加上回收 
		this->next = new element();
		this->size = 0;
		this->sum = 0;
	} 
	// 获取第x个元素,如果超出则显示最后一个元素 
	int getElement(int num) {
		int len = min(num,this->size); 
		element* it = this->next;
		for(int i=0 ;i<len;i++) {
			it = it->next;
		}
		return it->value;
	} 
	// 按照name字段查找 返回index 
	int findByName(string name){
		element* it = this->next;
		while(it->next) {
			it = it->next;
			if(it->name == name) {
				return it->index;
			} 
		}
		return -1;// 没找到 
	}
	// 遍历元素 打印书本价格 
	void printELementsValue() {
		element* it = this->next;
		while(it->next) {
			it = it->next;
			cout<<it->value<<" ";
		}
		cout<<endl;
	} 
	// 遍历元素 打印书本名称 
	void printELementsName() {
		element* it = this->next;
		while(it->next) {
			it = it->next;
			cout<<it->name<<" ";
		}
		cout<<endl;
	} 

测试样例使用方法进行模拟,传入具体的顺序表的实例对象,即可进行添加操作

cpp 复制代码
void test1(sequence *seq) {
	seq->push(1,"Alice");
	seq->push(2,"Bob");
	seq->push(3,"安徒生");
	seq->push(2,"Alice");
	seq->push(7,"格林语言");
	seq->push(5,"中华上下五千年");
	seq->printSate();
}

void test2(sequence *seq) {
	seq->push(3,"钢铁是怎样炼成的");
	seq->push(2,"A");
	seq->push(1,"B");
	seq->push(5,"V");
	seq->push(7,"D");
	seq->printSate();
	seq->pop();
	seq->pop();
	seq->printSate();
}
void test3(sequence *seq) {
	test1(seq);
	seq->reset();
	seq->printSate();
}
void test4(sequence *seq){
	test2(seq);
	cout<<seq->front()<<endl;
	cout<<seq->last()<<endl;
}
void test5(sequence *seq){
	test1(seq);
	test2(seq);
	seq->printELementsValue();
	seq->printELementsName();
}
相关推荐
H.莓飛23 分钟前
【数据结构】队列
linux·数据结构·centos
汉克老师28 分钟前
GESP2026年9月认证C++二级( 第一部分选择题(1~7题)精讲
c++·gesp·小学生·学c++编程
Ivanqhz30 分钟前
层归一化、残差、前馈网络与激活函数简述
服务器·数据库·人工智能·深度学习·算法
沫璃染墨1 小时前
《从零入门Linux系统篇(五十四):线程篇·七——互斥锁底层原理:从原子交换到线程竞争与锁实现》
linux·运维·服务器·开发语言·c++·驱动开发·系统架构
不会就选b1 小时前
算法日常・每日刷题--<动态规划>3
数据结构·算法
H.莓飛2 小时前
【数据结构】队列_OJ题
linux·数据结构·centos
-dzk-2 小时前
【堆】LC 347.前 K 个高频元素
算法·堆
H.莓飛2 小时前
【数据结构】栈_OJ题
linux·数据结构·算法·centos
Zootopia6262 小时前
多架 eVTOL集群排班调度方案思考
人工智能·算法·数学建模·matlab·动态规划·无人机·evtol
可乐ea2 小时前
GitHub Security Lab 开源 Fuzzing Taskflow:让 LLM 智能体自己跑完 C/C++ 模糊测试
jvm·c++·github·模糊测试·mcp·代码agent·智能体自动化