C++数据结构学习之顺序表
顺序表是数据结构中最基本也是应用相当广泛的一种数据结构类型。它通常包含三个私有成分,即指向数据数组的头指针、当前表长以及表的实际容量。表的头指针通常指向数据数组的基地址,通过数组的形式进行访问数据数组中的每个元素。其基本结构类型如下图所示:
从上图可以看到,其基本结构包含一个指向数据数组的头指针,当前表长为5,但是该顺序表实际表的容量有7。
下面附上实现该数据结构的各项操作代码
在C.h文件中存放可能要用到的C++库:
#ifndef _C_H_
#define _C_H_
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
using namespace std;
#endif // _C_H_
在SequenceTable.h存放对应的数据结构类型:
#ifndef _SEQUENCETABLE_H_
#define _SEQUENCETABLE_H_
template<typename T>class STL{
private:
T *elem; //save the base address of STL
int length; //save the current length of STL;
int listsize; //save the opacity of STL
public:
//a function to create k length STL, if k doesn't exist, it can use default function to create 1 length STL
STL(int k=){
elem = new T[k];
length = ;
listsize = k;
}
//destruct STL
~STL(){
delete[]elem;
}
int getLength();
int getListsize();
void Insert(int k, int data); //a function to insert elem in the specific location
void Delete(int k, int data); //a function to delete elem in the specific location
int getElem(int k); //get elem in the specific location
int getLocation(int data);
void ListEmpty();
void showSTL();
}; template<typename T>
int STL<T>::getListsize(){
return listsize;
} template<typename T>
int STL<T>::getLength(){
return length;
} template<typename T>
void STL<T>::Insert(int k, int data){
//confirm whether the k is reasonable
if(k <= || k > (length+)){
cout<<"k is unreasonable!"<<endl;
}
int t; //an empty bottle
//insert data while satisfy this situation
while(length<listsize && k<=length){
if(k<length){
t = elem[k];
elem[k] = data;
data = elem[k+];
k++;
}
else{
length++;
t = elem[k];
elem[k] = data;
data = elem[k+];
k++;
}
}
if(k==(length+)){
if(length<listsize){
length++;
elem[k] = data;
}
else{
listsize++;
length++;
elem[k] = data;
}
}
} template<typename T>
void STL<T>::Delete(int k, int data){
//confirm whether the k is reasonable
if(k <= || k > (length+)){
cout<<"k is unreasonable!"<<endl;
}
//insert data while satisfy this situation
if(elem[k]==data){
while(k<=length){
if(k<length){
elem[k] = elem[k+];
k++;
}
else{
k++;
}
}
length--;
}
else{
cout<<"delete error!"<<endl;
}
} template<typename T>
int STL<T>::getLocation(int data){
int i = ; //consider when the length is 0 but the listsize is 1
while(i<=length){
if(elem[i] == data){
return i;
}
i++;
}
} template<typename T>
int STL<T>::getElem(int k){
if(k<= || k>=(length+)){
cout<<"k is unreasonable!"<<endl;
}
else{
return elem[k];
} } template<typename T>
void STL<T>::ListEmpty(){
length = ;
} template<typename T>
void STL<T>::showSTL(){
for(int i=;i<=length; i++){
cout<<elem[i]<<endl;
}
}
#endif
然后再main.cpp文件中实现对该数据结构的调用:
#include "C.h"
#include "SequenceTable.h"
typedef int T;
int main(){
STL<T> L;
for(int i=; i<;i++){
L.Insert(i+, i+);
}
cout<<L.getLocation()<<endl;
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
L.showSTL();
int a = L.getElem();
L.Delete(,);
L.showSTL();
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
L.Delete(,);
L.showSTL();
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
}
运行实现:
C++数据结构学习之顺序表的更多相关文章
- C++ 数据结构学习一(顺序表)
//SequentialList.h 顺序表模板类 #ifndef SEQUENTIAL_LIST_HXX#define SEQUENTIAL_LIST_HXX using std::cout; us ...
- 【数据结构】之顺序表(Java语言描述)
之前总结过使用C语言描述的顺序表数据结构.在C语言类库中没有为我们提供顺序表的数据结构,因此我们需要自己手写,详细的有关顺序表的数据结构描述和C语言代码请见[我的这篇文章]. 在Java语言的JDK中 ...
- 【数据结构】之顺序表(C语言描述)
顺序表是线性表的一种,它将元素存储在一段连续的内存空间中,表中的任意元素都可以通过下标快速的获取到,因此,顺序表适合查询操作频繁的场景,而不适合增删操作频繁的场景. 下面是使用 C语言 编写的顺序表的 ...
- C语言学习笔记-顺序表
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include "coni ...
- 数据结构 单链表&顺序表
顺序表: 一般使用数组(C语言中的数组采用顺序存储方式.即连续地址存储)来描述. 优点:在于随机访问元素, 缺点:插入和和删除的时候,需要移动大量的元素. 链表: 优点:插入或删除元素时很方便,使用灵 ...
- 数据结构——Java实现顺序表
一.分析 什么是顺序表?顺序表是指用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表.一个标准的顺序表需要实现以下基本操作: 1.初始化顺序表 ...
- 数据结构之线性顺序表ArrayList(Java实现)
一.ListMe接口: import java.util.ArrayList; //实现线性表(顺序表和链表)的接口://提供add get isEmpty size 功能public interfa ...
- 数据结构之动态顺序表(C实现)
线性表有2种,分为顺序表和链表. 顺序表: 采用顺序存储方式,在一组地址连续的存储空间上存储数据元素的线性表(长度固定) 链表: 有3种,单链表.双向链表.循环链表(长度不固定) seqList.h ...
- 【c++版数据结构】之顺序表的实现
SeqList.h #ifndef SEQLIST_H #define SEQLIST_H #include<iostream> using namespace std; typedef ...
随机推荐
- node中定时器, process.nextTick(), setImediate()的区别与联系
1.定时器 setTimeout()和setInterval()与浏览器中的API是一致的,定时器的问题在于,他并非精确的(在容忍范围内).尽管事件循环十分快,但是如果某一次循环占用的时间较多,那么下 ...
- jQuery中获取文档的高度、可视区域高度以及滚动条距页面顶部的高度
在写页面的时候,经常会碰到这样的情况,就是要获取文档的高度.可视区域高度或者滚动条距页面顶部的高度等情况. 但我总是有些爱搞混淆了,这里还是简单做个笔记吧,这里只限于使用jQuery来获取. 1.获取 ...
- N的N次方
题目描述 现给你一个正整数N,请问N^N的最左边的数字是什么? 输入 输入包含多组测试数据.每组输入一个正整数N(N<=1000000). 输出 对于每组输入,输出N^N的最左边的数字. 样例输 ...
- github网站介绍、并使用git命令管理github(详细描述)
本章学习: 1)熟悉github网站 2)通过git命令远程管理github, 3)git命令使用ssh key密钥无需输入账号密码 1.首先我们来熟悉github网站 1.1 注册github 登录 ...
- 数据库 MySQL基础知识
(关于MySQL的安装,具体见下面博客:http://www.cnblogs.com/wj-1314/p/7573242.html) 一.什么是数据库 ? 数据库是按照数据结构来组织,存储和管理数据的 ...
- VIM命令模式与输入模式切换
vi编辑器 vi是UNIX和类UNIX环境下的可用于创建文件的屏幕编辑器.vi有两种工作模式:命令模式和文本输入模式.启动vi需要输入vi,按[Spacebar]键并输入文件名后回车. 切换模式键 ...
- javaScript中关于字符串的操作函数和方法
1.字符串转换 toString():可以将任何类型的数据都转换为字符串 var num= 19; //19 var myStr = num.toString(); //"19" ...
- jstl 的判断使用
JSTL 是JSP的标准标记库 1.必须引入的头部标签 <%@ taglib uri="http://java.sun.com/jstl/core_rt"prefix=&q ...
- python初识 - day4
一.集合(set) 1.集合的定义 set 是一个无序的元素集合,支持并.交.差及对称差等数学运算, 但由于 set 不记录元素位置, 因此不支持索引.分片等类序列的操作. 2.集合的创建 大括号或 ...
- CentOS7 配置花生壳开机启动
在家安装服务器,外地可以随时登陆,感觉花生壳特别方便,具体路由器配置请参考http://service.oray.com/question/2486.html. 我使用的操作系统是 [root@loc ...