【数据结构】实现顺序表(c++)

头文件:

#pragma once

#include <iostream>
using namespace std;

template <class Type>
class SeqList
{

	public:
		SeqList(size_t sz = INIT_SIZE);
		~SeqList();
	public:
		bool isfull()const
		{
			return size > capacity;
		}
		bool isempty()const
		{
			return size == 0;
		}
	public:
		void show_list();
		void tail_insert(const Type &x);
		void head_insert(const Type &x);
		void sort();
		void head_delete();
		void tail_delete();
		void pos_insert(int pos,const Type &x);
		void val_insert(const Type &x);
		void pos_delete(int pos);
		int find(const Type &x);
		void val_delete(const Type &x);
		int length();
		void reverse();
		void clear();
		void destroy();
		void quit_system(Type &x);
	private:
		enum{ INIT_SIZE = 8 };
		Type *base;
		size_t size;
		size_t capacity;
};

// 构造函数
template<class Type>
SeqList<Type>::SeqList(size_t sz = INIT_SIZE)
{
	capacity = sz > INIT_SIZE ? sz : INIT_SIZE;
	base = new Type[capacity];
	size = 0;
}

// 析构函数
template<class Type>
SeqList<Type>::~SeqList()
{
	delete base;
}

// 显示
template<class Type>
void SeqList<Type>::show_list()
{
	if (size == 0)
	{
		cout << "the table is empty!" << endl;
		return;
	}
	for (int i = 0; i < size; ++i)
	{
		cout << base[i] << "  ";
	}
	cout << endl;
}

// 尾插
template<class Type>
void SeqList<Type>::tail_insert(const Type &x)
{
	// 判断表是否满
	if (isfull())
	{
		cout << "table is full,can not insert!" << endl;
		return;
	}
	// 进行尾插
	base[size] = x;
	size++;
}

// 头插
template<class Type>
void SeqList<Type>::head_insert(const Type &x)
{
	// 判断表是否满
	if (isfull())
	{
		cout << "the table is full,can not insert!" << endl;
		return;
	}
	// 把元素往后移
	for (int i = size; i > 0; --i)
	{
		base[i] = base[i - 1];
	}
	// 进行头插
	base[0] = x;
	size++;
}

// 排序
template<class Type>
void SeqList<Type>::sort()
{
	for (int i = 1; i < size; ++i)
	{
		for (int j = 0; j < size - i; ++j)
		{
			if (base[j]>base[j + 1])
			{
				int temp = base[j];
				base[j] = base[j + 1];
				base[j + 1] = temp;
			}
		}
	}
}

// 头删
template<class Type>
void SeqList<Type>::head_delete()
{
	if (isempty())
	{
		cout << "the table is empty,can not delete!" << endl;
		return;
	}
	for (int i = 0; i < size; ++i)
	{
		base[i] = base[i + 1];
	}
	size--;
}

// 尾删
template<class Type>
void SeqList<Type>::tail_delete()
{
	if (isempty())
	{
		cout << "the table is empty,can not delete!" << endl;
		return;
	}
	size--;
}

// 按位插入
template<class Type>
void SeqList<Type>::pos_insert(int pos,const Type &x)
{
	if (isfull())
	{
		cout << "the table is full,can not insert!" << endl;
		return;
	}
	if (pos<0 || pos>size)
	{
		cout << "the position is illegal!" << endl;
		return;
	}
	else
	{
		for (int i = size; i > pos; --i)
		{
			base[i] = base[i - 1];
		}
		base[pos] = x;
		size++;
	}
}

// 按值插入
template<class Type>
void SeqList<Type>::val_insert(const Type &x)
{
	if (isfull())
	{
		cout << "the table is full,can not insert!" << endl;
		return;
	}
	tail_insert(x);
	sort();
}

// 按位删除
template<class Type>
void SeqList<Type>::pos_delete(int pos)
{
	if (isempty())
	{
		cout << "the table is empty,can not delete!" << endl;
		return;
	}
	if (pos<0 || pos>size - 1)
	{
		cout << "the position is illegal!" << endl;
		return;
	}
	else
	{
		for (int i = pos; i < size; ++i)
		{
			base[i] = base[i + 1];
		}
		size--;
	}
}

// 查找
template<class Type>
int SeqList<Type>::find(const Type &x)
{
	for (int i = 0; i < size; ++i)
	{
		if (base[i] == x)
			return i;
	}
	cout << "the value is not exist!" << endl;
	return 0;
}

// 按值删除
template<class Type>
void SeqList<Type>::val_delete(const Type &x)
{
	if (isempty())
	{
		cout << "the table is empty,can not delete!" << endl;
		return;
	}
	int pos = find(x);
	for (int i = pos; i < size; ++i)
	{
		base[i] = base[i + 1];
	}
	size--;
}

// 求表长
template<class Type>
int SeqList<Type>::length()
{
	return size;
}

// 反转表内容
template<class Type>
void SeqList<Type>::reverse()
{
	if (isempty())
	{
		cout << "the table is empty, can not operate!" << endl;
			return;
	}
	int i = 0;
	int j = size - 1;
	while (i < j)
	{
		int temp = base[i];
		base[i] = base[j];
		base[j] = temp;
		++i;
		--j;
	}
}

// 清除表
template<class Type>
void SeqList<Type>::clear()
{
	size = 0;
}

// 摧毁表
template<class Type>
void SeqList<Type>::destroy()
{
	base = NULL;
}

// 退出系统
template<class Type>
void SeqList<Type>::quit_system(Type &x)
{
	x = 0;
}



主函数:

<pre name="code" class="cpp">// 用c++实现顺序表

#include "SeqList.h"

int main()
{
	SeqList<int> mylist;
	int input = 1;
	int insert;
	int pos;
	while (input)
	{
		cout << "*********************************************************************" << endl;
		cout << "*     [1] show_list                       [2] tail_insert           *" << endl;
		cout << "*     [3] head_insert                     [4] sort                  *" << endl;
		cout << "*     [5] head_delete                     [6] tail_delete           *" << endl;
		cout << "*     [7] pos_insert                      [8] val_insert            *" << endl;
		cout << "*     [9] pos_delete                      [10] find                 *" << endl;
		cout << "*     [11] val_delete                     [12] length               *" << endl;
		cout << "*     [13] reverse                        [14] clear                *" << endl;
		cout << "*     [15] destroy                        [16] quit_system          *" << endl;
		cout << "*********************************************************************" << endl;
		cout << "please choose:";
		cin >> input;
		switch (input)
		{
		case 1:
			mylist.show_list();
			break;
		case 2:
			cout << "please enter the number to be inserted(-1 end):" << endl;
			while (cin >> insert, insert != -1)
			{
				mylist.tail_insert(insert);
			}
			break;
		case 3:
			cout << "please enter the number to be inserted(-1 end):" << endl;
			while (cin >> insert, insert != -1)
			{
				mylist.head_insert(insert);
			}
			break;
		case 4:
			mylist.sort();
			break;
		case 5:
			mylist.head_delete();
			break;
		case 6:
			mylist.tail_delete();
			break;
		case 7:
			cout << "please enter the position you want to insert:" << endl;
			cin >> pos;
			cout << "please enter the number to be inserted:" << endl;
			cin >> insert;
			mylist.pos_insert(pos, insert);
			break;
		case 8:
			cout << "please enter the number to be inserted:" << endl;
			cin >> insert;
			mylist.val_insert(insert);
			break;
		case 9:
			cout << "please enter the position you want to delete:" << endl;
			cin >> pos;
			mylist.pos_delete(pos);
			break;
		case 10:
			cout << "please enter the number to be found:" << endl;
			cin >> insert;
			cout << mylist.find(insert) << endl;
			break;
		case 11:
			cout << "please enter the number you want to delete:" << endl;
			cin >> insert;
			mylist.val_delete(insert);
			break;
		case 12:
			cout << mylist.length() << endl;
			break;
		case 13:
			mylist.reverse();
			break;
		case 14:
			mylist.clear();
			break;
		case 15:
			mylist.destroy();
			break;
		case 16:
			mylist.quit_system(input);
			break;
		default:
			break;

		}
	}

	return 0;
}

show功能:



<img src="http://img.blog.csdn.net/20150518174746615?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

尾插:

<img src="http://img.blog.csdn.net/20150518174917630?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

头插:

<img src="http://img.blog.csdn.net/20150518175227435?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

<img src="http://img.blog.csdn.net/20150518175212221?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

排序:

<img src="http://img.blog.csdn.net/20150518175244326?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


尾删:


<img src="http://img.blog.csdn.net/20150518175438179?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

头删:


<img src="http://img.blog.csdn.net/20150518175509176?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

<img src="http://img.blog.csdn.net/20150518175436381?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

定位插入:

<img src="http://img.blog.csdn.net/20150518175510374?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

按值插入:

<img src="http://img.blog.csdn.net/20150518175620718?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

按位删除:

<img src="http://img.blog.csdn.net/20150518175606721?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

查找:

<img src="http://img.blog.csdn.net/20150518175639809?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

按值删除:

<img src="http://img.blog.csdn.net/20150518175803195?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

求长度:

<img src="http://img.blog.csdn.net/20150518175830354?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

反转:

<img src="http://img.blog.csdn.net/20150518175816451?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

清除:

<img src="http://img.blog.csdn.net/20150518175923160?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

退出系统:

<img src="http://img.blog.csdn.net/20150518175908446?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

时间: 2024-11-07 00:37:35

【数据结构】实现顺序表(c++)的相关文章

数据结构。顺序表

#include <stdio.h>#include <stdlib.h> #define LIST_INIT_SIZE 100#define LIST_INCREMENT 10typedef struct Point   //element type{    int x;    int y;}Point;typedef Point ElemType;typedef struct          //list type{    ElemType *elem;     //data

Windows 已在 数据结构_顺序表.exe 中触发一个断点——new和delete注意事项

实现数据结构的顺序表的类时,输入,改,删,查都可以,但是最后析构函数时持续出错 错误提示"Windows 已在 数据结构_顺序表.exe 中触发一个断点" int *elem=new int(LIST_INIT_SIZE); if(!elem)cout<<"overflow"<<endl; if(leng>LIST_INIT_SIZE) cout<<"error"; else {    length=le

数据结构学习---顺序表

在准备考研的时候就想发学习笔记,想来已经过了多时.现在在培训班又要展开学习,说明一件事:408是指导学习研究计算机的基础!对于编写程序而言,数据结构与算法,是关键!我想其他的组成原理,计算机网络,操作系统也很重要,这是一个system,有必要有需要学习认真的学习之.希望这个是好的开始! ---------------------------------------------------------------- 昨天晚上看浙大在网易云课上的视频,没有上过浙大的我还是非常激动,哈哈,三个短视频看

数据结构之顺序表

好不容易linux的课程结束了,下面就进入了数据结构的课程,对于没学过这本书的我,只能弱弱的说一句,数据结构真的好难,在学习的过程中,觉得最经典的一句话便是,数据结构+算法=程序.我只想说理解数据结构真的好难,太富有逻辑性了,有时候真的还需要发挥自己的空间想象能力,几天的学习,其实还算可以吧,下面我就给大家写一点线性表的基本操作吧,我尽可能的讲解的清楚一些,方便看的人有兴趣能有自己实现. #include <stdio.h> #include <stdlib.h> #include

C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用

摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动态数组)STL vector进行分析和总结. 引言 因为前段时间对台大的机器学习基石和技法课程进行了学习,发如今详细的实现中经常涉及到各种类型的数据结构,比方线性表.二叉树.图等,在使用这些数据结构时感到有些吃力.主要是对一些主要的数据结构理解的不够.所以趁着暑假假期.近期一段时间总会抽出时间复习一

【数据结构】顺序表和链表

一.顺序表 顺序表定义 :顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素.使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表.顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中. 顺序表可以分为静态顺序表和动态顺序表,静态较为简单,本文提供全部动态顺序表基本操作的代码. 顺序表的基本操作:

数据结构复习---顺序表和单链表

1.前言: 最近比较浮躁,想学习一门新的技术却总是浅尝辄止,遇到不懂的地方就想跳过去,时间长了,心态就有点崩了.静下心来,去喝了几碗心灵鸡汤.鸡汤博主感动到了我:"无专注,无风景.不要太贪心,一次只做一件事,而且只做最重要的事.".于是乎,我把家里翻了个底朝天,找到了我垫在床底下的<数据结构>这本书,觉得自己是时候静下心来好好复习一下基础了.今天刚看到顺序表和链表,把我的学习心得记录在这里.也希望自己能坚持,老老实实的把这本书复习完. 2.数据结构的重要性: 讲一门知识之前

javascript数据结构之顺序表

关于线性表的概念这里就不赘述了,可以自行百度和查阅资料,线性表按照存储(物理)结构分为顺序存储和链式存储,每种存储方式的不同决定了它的实现代码是不同的: 顺序存储的特点就是在内存中选一块连续的地址空间,然后将线性表放入其中,这样做便于线性表的存取,但是不利于插入和删除,而且在事先无法确定线性表长度的前提下可能会造成内存浪费或溢出. 这篇我是用javascript来实现线性表中的顺序表. 下面上代码: 1 var orderList = function(){ 2 var items = [];

数据结构:顺序表的基本操作

顺序表作业: #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; typedef int T; class SeqList{ private: T *data; int MaxSize; ///顺序表最多可以存放的元素个数. int last; ///顺序表最后一个元素的下标,初始值为-1. public: SeqList(int sz); void Input();

计蒜客课程数据结构(顺序表)

1.线性表是由相同数据类型的 n 个数据元素a0,a1,......,an-1 组成的有限序列.一个数据元素可以由若干个数据项组成.若用 L 命名线性表,则其一般表示如下: L=(a0,a1,......,an-1) 其中, a0?? 是唯一的“第一个”数据元素,又称为表头元素:an-1?? 是唯一的“最后一个”数据元素,又称为表尾元素. 线性表按照存储结构,可以分为顺序表和链表两种类型. 2.顺序表是在计算机内存中以数组形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构.