• 基于顺序表实现的线性表
  • 基于链表实现的线性表
  • 实验中遇到的问题(&DevC++中的骚操作)

一、基于顺序表实现的线性表

List.h

#include <iostream>
using namespace std;
#ifndef _List
#define _List
namespace wangzhe
{template <typename E> class List{private:void operator =(const List&) {}List(const List&) {}public:List() {}virtual ~List() {}virtual void clear() =0;virtual void insert(const E& item) =0;virtual void append(const E& item) =0;virtual E remove() =0;virtual void moveToStart() =0;virtual void moveToEnd() =0;virtual void prev() =0;virtual void next() =0;virtual int length() const =0;virtual int currPos() const =0;virtual void moveToPos(int pos) =0;virtual const E& getValue() const =0;           };}
#endif

AList.h

#include<iostream>
using namespace std;
#include"List.h"
#ifndef _AList
#define _AList
namespace wangzhe
{template <typename E> class AList:public List<E>{private:int maxSize;int listSize;int curr;E* listArray;public:AList(int size);~AList();void clear();void insert(const E& item);void append(const E& item);E remove();void moveToStart();void moveToEnd();void prev();void next();int length() const;int currPos() const;void moveToPos(int pos) ;const E& getValue() const ;          };
}
#endif

AList.cpp

#include<iostream>
using namespace std;
#include"AList.h"
namespace wangzhe
{template <typename E>AList<E>::AList(int size){maxSize=size;listSize=curr=0;listArray =new E[maxSize];}template <typename E>AList<E>::~AList(){delete [] listArray;}template <typename E>void AList<E>::clear(){delete [] listArray;listSize=curr=0;listArray=new E[maxSize]; }template <typename E>void AList<E>::insert(const E& item){if(listSize>=maxSize){cout<<"List capacity exceeded"<<endl;return;}for(int i=listSize;i>curr;i--){listArray[i]=listArray[i-1];}listArray[curr]=item;listSize++;}template <typename E>void AList<E>::append(const E& item){if(listSize>=maxSize){cout<<"List capacity exceeded"<<endl;return;}listArray[listSize++]=item; } template <typename E>E AList<E>::remove(){if(curr<0||curr>=listSize){cout<<"No element"<<endl;return 0;}E it=listArray[curr];for(int i=curr;i<listSize-1;i++)listArray[i]=listArray[i+1];listSize--;return it;}template <typename E>void AList<E>::moveToStart(){curr=0;}template <typename E>void AList<E>::moveToEnd(){curr=listSize;}template <typename E>void AList<E>::prev(){if(curr!=0) curr--;}template <typename E>void AList<E>::next(){if(curr<listSize) curr++;}template <typename E>int AList<E>::length() const{return listSize;}template <typename E>int AList<E>::currPos() const{return curr;}template <typename E>void AList<E>::moveToPos(int pos) {if(pos<0||pos>=listSize){cout<<"No current element"<<endl;return;}curr=pos; }template <typename E>const E& AList<E>::getValue() const  {if(curr<0||curr>=listSize){cout<<"No current element"<<endl;return 0;}return listArray[curr];}
} 

main.cpp

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include"AList.h"
#include"AList.cpp"
using namespace wangzhe;
int main(int argc, char** argv)
{for(int i=1;i<=50;i++) cout<<'-';cout<<"\n"<<"基于顺序表实现的线性表基本操作演示\n";for(int i=1;i<=50;i++) cout<<'-';cout<<endl;AList<int> a(10010);cout<<"请输入对应的数字实现相关操作:\n";cout<<" 0、退出操作\n"; cout<<" 1、在当前位置插入一个元素\n";cout<<" 2、在末尾插入一个元素\n";cout<<" 3、删除当前位置的元素并输出其值\n";cout<<" 4、将当前位置置于第一个元素\n";cout<<" 5、将当前位置置于最后一个元素\n";cout<<" 6、将当前位置向前移一个\n";cout<<" 7、将当前位置向后移一个\n";cout<<" 8、输出当前元素个数\n";cout<<" 9、输出当前位置\n";cout<<"10、将当前位置置于给定位置处\n";cout<<"11、输出当前位置的元素\n";cout<<"12、输出当前所有元素\n";cout<<"13、清空所有元素\n"; for(int i=1;i<=50;i++) cout<<'-';cout<<endl;int order,temp;while(1){cin>>order;if(!order) break;switch (order){case 1:{cout<<"输入要插入的元素:\n";cin>>temp;a.insert(temp); break;}case 2:{cout<<"输入要插入的元素:\n";cin>>temp;a.append(temp); break;} case 3:{cout<<a.remove()<<endl;break;}case 4:{a.moveToStart();break;}case 5:{a.moveToEnd();break;}case 6:{a.prev();break;}case 7:{a.next();break;}case 8:{cout<<a.length()<<endl;break;}case 9:{cout<<a.currPos()<<endl;break;}case 10:{cout<<"输入给定位置:\n";cin>>temp;a.moveToPos(temp);break; }case 11:{cout<<a.getValue()<<endl;break;}case 12:{for(a.moveToStart();a.currPos()<a.length();a.next())cout<<a.getValue()<<' ';cout<<endl;break;}case 13:{a.clear();break;}default: break;} }return 0;}

二、基于链表实现的线性表

List.h

同顺序表

Link.h

#include <iostream>
using namespace std;
#ifndef _Link
#define _Link
namespace wangzhe
{template<typename E> class Link{public:E element;Link *next;Link(const E& elemval,Link* nextval =NULL){element=elemval;next=nextval;  }   Link(Link* nextval=NULL){next=nextval;}};
}
#endif

LList.h

#include<iostream>
using namespace std;
#include"List.h"
#include"Link.h"
#ifndef _LList
#define _LList
namespace wangzhe
{template<typename E>class LList:public List<E>{private:Link<E>* head;Link<E>* tail;Link<E>* curr;int cnt;void init();void removeall();public:LList(int size);~LList();void print() const;void clear();void insert(const E& it); void append(const E& it);E remove();void moveToStart();void moveToEnd();void prev();void next();int length() const;int currPos() const;void moveToPos(int pos);const E& getValue() const;};
}
#endif

LList.cpp

#include<iostream>
using namespace std;
#include"LList.h"
namespace wangzhe
{template<typename E>void LList<E>::init(){curr=tail=head=new Link<E>;head->next=NULL;cnt=0;}template<typename E>void LList<E>::removeall(){while(head!=NULL){curr=head;head=head->next;delete curr;}}template<typename E>LList<E>::LList(int size){init();}template<typename E>LList<E>::~LList(){removeall();}template<typename E>void LList<E>::print() const{Link<E>* temp=head->next;while(temp!=NULL){cout<<temp->element<<' ';temp=temp->next;}}template<typename E>void LList<E>::clear(){removeall();init();}template<typename E>void LList<E>::insert(const E& it){curr->next=new Link<E>(it,curr->next);if(tail==curr) tail=curr->next;cnt++;}template<typename E>void LList<E>::append(const E& it){tail=tail->next=new Link<E>(it,NULL);cnt++;}template<typename E>E LList<E>::remove()//删除当前指向的下一个结点 {if(curr->next==NULL){cout<<"No element"<<endl;return 0;}E it=curr->next->element;Link<E>* temp=curr->next;if(tail==curr->next) tail=curr;curr->next=curr->next->next;delete temp;cnt--;return it;}template<typename E>void LList<E>::moveToStart(){curr=head;}template<typename E>void LList<E>::moveToEnd(){curr=tail;}template<typename E>void LList<E>::prev(){if(curr==head){cout<<"curr is the head\n";return;} Link<E>* temp=head;while(temp->next!=curr) temp=temp->next;curr=temp;}template<typename E>void LList<E>::next(){if(curr==tail){cout<<"curr is the tail\n";return;}curr=curr->next;}template<typename E>int LList<E>::length() const{return cnt;}template<typename E>int LList<E>::currPos() const{Link<E>* temp=head;int i;for(i=0;temp!=curr;i++)temp=temp->next;return i;}template<typename E>void LList<E>::moveToPos(int pos){if(pos<0||pos>cnt){cout<<"Position out of tange\n";}curr=head;for(int i=0;i<pos;i++) curr=curr->next;}template<typename E>const E& LList<E>::getValue() const{if(curr->next==NULL){cout<<"No value\n";return -1;}return curr->next->element;}
}

main.cpp

参考顺序表

三、实验中遇到的问题(&DevC++中的骚操作)

1、注意函数声明与定义的区别;

2、有#include"XXX.cpp"的骚操作(类模板的定义和声明是能在.h和.cpp分离的!!,只不过在main.cpp中除了需要包括.h之外还要包括相应的.cpp!!!);

3、某个.h里的构造函数名与类名中的一个字母大小写不同,devC++竟然不报错,着实可恶,因此应该注意大小写

参考文献

【1】Clifford A.Shaffer.数据结构与算法分析【M】.北京:电子工业出版社,2013:62-68.

实验一 线性表的物理实现(顺序表+单向链表)相关推荐

  1. 线性表(一)——顺序表

    线性表(一) 线性表(linear list),也称有序表(ordered list),是线性结构的典型代表.数据元素之间仅具有单一的前驱和后继关系. 一.线性表的逻辑结构 1.线性表的定义 线性表简 ...

  2. 用Java描述数据结构之线性表的顺序存储(顺序表),ArrayList及其方法的介绍

    我们先来想一想什么是线性表? 线性表是最基本.最简单.也是最常用的一种数据结构.线性表(linear list)是数据结构的一种,一个线性表是n个具有相同特性的数据元素的有限序列. 线性表中数据元素之 ...

  3. 线性表:2.线性表的顺序存储结构--顺序表及C语言实现

    逻辑结构上呈线性分布的数据元素在实际的物理存储结构中也同样相互之间紧挨着,这种存储结构称为 线性表的顺序存储结构 . 也就是说,逻辑上具有线性关系的数据按照前后的次序全部存储在一整块连续的内存空间中, ...

  4. 线性表的顺序存储结构——顺序表

    什么是线性表? 线性表简称表,是n(n>=0)个具有相同类型的数据元素的有限序列,线性表中数据元素的个数称为线性表的长度,长度为0的表称为空表. 什么是顺序表? 线性表的顺序存储结构称为顺序表. ...

  5. 构建线性表的c语言代码,数据结构严蔚敏C语言版—线性表顺序存储结构(顺序表)C语言实现相关代码...

    1.运行环境 这里说明一下这里所有的C语言代码都是基于code::blocks 20.03编译运行的.当然一些其他集成开发环境应该也是可以的,个人不太喜欢功能太过强大的IDE,因为那同样意味着相关设置 ...

  6. 数据结构严蔚敏C语言版—线性表顺序存储结构(顺序表)C语言实现相关代码

    数据结构严蔚敏C语言版-线性表顺序存储结构(顺序表)C语言实现相关代码 1.运行环境 2.准备工作 1)项目构建 1>新建一个SeqList项目 2>新建两个文件Sources和Heade ...

  7. 顺序表的实现和顺序表相关OJ题

    前言: 从本篇博客开始,我们会逐渐接触一些数据结构,并且我们将会用C语言实现这些数据结构.我希望从这篇博客开始,读者能够学会画好图在写代码,代码运行出错进行自主调试来分析错误,如果你能够养成这两个良好 ...

  8. 线性表之简介及顺序表

    线性表的本质 线性表的定义:线性表是具有相同类型的 n( ≥0)个数据元素的有限序列.((a1, a2, -, an)ai 是表项,n 是表长度.) 线性表(List)是零个或多个数据元素的集合. 线 ...

  9. 数据结构与算法(2-1)线性表之顺序存储(顺序表)

    顺序表用数组存储数据元素(可以是结构体数组,也可以是结构体内的元素数组),插入和删除等等也是类似数组的操作. 顺序表优势: 1.无须为表示表中元素之间的逻辑关系而增加额外的存储关系,就是直接的顺序: ...

  10. 线性表:4.结合顺序表和链表——静态链表及C语言实现

    另外一种链式表示--静态链表,之前两篇说的都是动态表. 逻辑结构上相邻的数据元素,存储在指定的一块内存空间中,数据元素只允许在这块内存空间中随机存放,这样的存储结构生成的链表称为静态链表. 静态链表和 ...

最新文章

  1. 《算法技术手册》一2.4.6 二次方的算法性能
  2. IDEA下用freemarker热更新的问题
  3. Xamarin Essentials教程语音播报TextToSpeech
  4. win7下.NET 2.0未在web服务器上注册的问题
  5. K8S部署工具:KubeOperator主要概念
  6. python实现redis分布式锁
  7. dynamic web module 2.5与2.4
  8. mongodb命令基础知识点
  9. 电脑桌面便签_在电脑桌面使用敬业签怎么操作退出团队便签?
  10. python中定义一个空的字符串_04python—15种字符串操作
  11. Mysql学习总结(60)——并发量大、数据量大的互联网业务数据库设计规范总结
  12. 关于主函数main(int argc,char *argv[])
  13. IIS访问要求输入用户名密码
  14. 【转贴】二节棍精典棍花动作详解
  15. WinForm程序利用sqlhelp连接SQLserver数据库
  16. 牛顿法求解方程的根(C语言)
  17. 若干tif文件转换成pdf
  18. Android集成讯飞语音、百度语音、阿里语音识别
  19. idea activation code记录
  20. 使用cordova调用相机在相机中添加蒙版

热门文章

  1. Python类方法、静态方法、全局变量的使用
  2. 如何在Excel中选择单元格时自动高亮显示整行和整列
  3. 行列式计算(编程实现)
  4. MWC 2019新品汇总:5G+折叠屏开启的新时代?
  5. Linux 新手学习任务
  6. xss跨站攻击html转义,前端攻击和防御(一)XSS跨站脚本攻击
  7. 仿微信手机端支付页面代码可改余额
  8. IEEE 802简介
  9. Linux系统之安装PHP环境
  10. Windows系统安装PHP