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

看了一下其他链接,真正的循序表应该支持随机索引的,也就是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();
}
相关推荐
墨雪不会编程1 小时前
数据结构—排序算法篇二
数据结构·算法·排序算法
ShineWinsu2 小时前
对于数据结构:堆的超详细保姆级解析—上
数据结构·c++·算法·计算机·二叉树·顺序表·
im_AMBER2 小时前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
QX_hao2 小时前
【Go】--文件和目录的操作
开发语言·c++·golang
卡提西亚2 小时前
C++笔记-20-对象特性
开发语言·c++·笔记
努力学算法的蒟蒻2 小时前
day09(11.6)——leetcode面试经典150
算法·leetcode·职场和发展
2301_796512523 小时前
Rust编程学习 - 内存分配机制,如何动态大小类型和 `Sized` trait
学习·算法·rust
三掌柜6663 小时前
C++ 零基础入门与冒泡排序深度实现
java·开发语言·c++
卿言卿语4 小时前
CC23-最长的连续元素序列长度
java·算法·哈希算法
沐怡旸4 小时前
【穿越Effective C++】条款15:在资源管理类中提供对原始资源的访问——封装与兼容性的平衡艺术
c++·面试