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

看了一下其他链接,真正的循序表应该支持随机索引的,也就是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();
}
相关推荐
咕咕嘎嘎10245 分钟前
C/C++内存对齐
java·c语言·c++
listhi52015 分钟前
激光雷达点云拟合中的ICP(迭代最近点)算法
算法
Octhexis17 分钟前
LC106 从中序与后序遍历序列构造二叉树
数据结构
持续学习的程序员+121 分钟前
强化学习阶段性总结
人工智能·算法
qq_4798754334 分钟前
C++ 模板元编程
java·开发语言·c++
爱装代码的小瓶子37 分钟前
【cpp知识铺子】map与set的底层AVL树
开发语言·数据结构·c++·b树·算法·链表
IT·小灰灰38 分钟前
腾讯HY2.0 Think推理模型深度解析:技术突破、应用场景与实践指南
开发语言·人工智能·python·深度学习·神经网络·算法·数据分析
修炼地1 小时前
代码随想录算法训练营第二十八天 | 动态规划理论基础、509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
c++·算法·动态规划
小南家的青蛙1 小时前
LeetCode第773题 - 滑动谜题
算法·leetcode·职场和发展
Felven1 小时前
C. Isamatdin and His Magic Wand!
c语言·数据结构·算法