线性表就是字面上的意思,

顺序表是线性表基于数组的一种实现,

“顺序”这个名字怎么来的并不清楚,可以勉强解释为“存储地址是连续、顺序的”。

另外两种线性表实现分别是“基于链表”和“散列存储”。

顺序表可以是动态的,也可以是静态的,

“静态”就是一开始就知道表容量,并且这个容量在之后无法被更改;

“动态”就是能够动态的改变表的容量,实现上基于动态内存分配。

基于数组的静态版本:

#ifndef SEQLIST_H
#define SEQLIST_H const int MAX_SIZE = ;
template <class T>
class SeqList {
private:
T node[MAX_SIZE];
int size = ;
public:
int getSize() const { return size; }
bool add(const T& x);
bool insert(int i, const T& x);
bool remove(int i);
int search(T& x);
T& getNode(int i);
}; #endif template <class T>
bool SeqList<T>::add(const T& x) {
if (size <= MAX_SIZE) {
++size;
node[size-] = x;
return true;
}
return false;
} template <class T>
bool SeqList<T>::insert(int i, const T& x) {
if (size <= MAX_SIZE && i >= && i <= size) {
++size;
for (int k = size-; k != i; --k) {
node[k] = node[k-];
}
node[i] = x;
return true;
}
return false;
} template <class T>
bool SeqList<T>::remove(int i) {
if (i >= && i <= size) {
for (int k = i; k != size; ++k) {
node[k] = node[k+];
}
--size;
return true;
}
return false;
} template <class T>
int SeqList<T>::search(T& x) {
for (int i = ; i != size; ++i) {
if (node[i] == x) return i;
}
return -;
} template <class T>
T& SeqList<T>::getNode(int i){
if (i >= && i < size) {
return
node[i];
}
};

#ifndef STUDENT_H
#define STUDENT_H #include <string>
#include <stdio.h> struct Student {
int no = ;
std::string name;
std::string grade;
double goal = 0.0;
Student() = default;
Student(int arg1, const std::string& arg2,
const std::string& arg3, double arg4): no(arg1), name(arg2), grade(arg3), goal(arg4) {}
void display() const {
printf("%d %s %s %.2f\n", no, name.c_str(), grade.c_str(), goal);
}
bool operator==(const Student &c1) {
if (c1.name == this->name || c1.no == this->no && c1.grade == this->grade) return true;
return false;
}
}; #endif #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string> using namespace std; int main()
{
SeqList<Student> list; Student s1(, "张三", "", );
Student s2(, "李四", "", );
Student s3(, "王五", "", );
Student s4(, "马六", "", );
Student s5(, "黄荣", "", ); list.add(s1);
list.add(s2); printf("step-a\n");
for (int i = ; i != list.getSize(); ++i) {
list.getNode(i).display();
} list.insert(, s3);
list.insert(, s4);
list.insert(, s5); printf("step-b\n");
for (int i = ; i != list.getSize(); ++i) {
list.getNode(i).display();
} list.remove();
list.remove(); printf("step-c\n");
for (int i = ; i != list.getSize(); ++i) {
list.getNode(i).display();
} printf("step-d\n");
if (list.search(s5) == -) {
printf("不存在\n");
} else {
s5.display();
}
Student s6(, "郭靖", "", );
if (list.search(s6) == -) {
printf("不存在\n");
} else {
s6.display();
} printf("step-e\n");
for (int i = ; i != list.getSize(); ++i) {
list.getNode(i).display();
} printf("step-f\n");
system("pause");
while (true) {
system("cls");
cout << "#####WINDOWS##EDITION#####################" << endl;
for (int i = ; i != list.getSize(); ++i) {
list.getNode(i).display();
}
cout << "########################MAXSIZE=512#########" << endl;
cout << "请选择您的操作1增2删3改4查:" << endl;
int order = ;
cin >> order;
if (order < || order > ) {
cout << "无效命令!\n" << endl;
} else {
switch (order) {
case : {
cout << "请输入学生的学号和姓名:" << endl;
int no = ;
string name;
cin >> no >> name;
cout << "请输入学生的学级和分数:" << endl;
string grade;
double goal = 0.0;
cin >> grade >> goal;
Student s(no, name, grade, goal);
list.add(s);
break;
}
case : {
cout << "请输入学生座号:" << endl;
int num;
cin >> num;
if (!list.remove(num)) {
cout << "不好意思,这个学生不存在" << endl;
}
break;
}
case : {
cout << "请输入需要修改的学生的座号:" << endl;
int num;
cin >> num;
cout << "您要修改的是" << list.getNode(num).name << "...,请输入新数据以覆盖原始数据:" << endl;
cin >> list.getNode(num).no
>> list.getNode(num).name
>> list.getNode(num).grade
>> list.getNode(num).goal;
break;
}
case : {
cout << "请输入需要查找的学生姓名:" << endl;
string name;
cin >> name;
Student s;
s.name = name;
if (list.search(s) == -) {
cout << "抱歉,没找到这名同学。" << endl;
} else {
cout << "该同学信息如下:" << endl;
list.getNode((list.search(s))).display();
}
break;
}
}
}
system("pause");
} return ;
}

常量成员函数的设置有些小问题待修改。。

C++ 顺序表实现的更多相关文章

  1. jdk顺序表笔记

    一.AbstractCollection 提供了集合的最大实现 继承该类,必须实现size()和iterator(),因为该类操作集合都是通过iterator 二.fail-fast策略 该策略在集合 ...

  2. c++顺序表基本功能

    头文件 #define LIST_MAX_SIZE 5#define LISTINCREMENT 2#include<assert.h>#include<string>temp ...

  3. 数据结构:顺序表(python版)

    顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...

  4. 《数据结构》2.2顺序表(sequence list)

    //顺序表节点的定义 typedef struct { datatype data[MAXSIZE]; //数组容量的上限 int len; //记录最后一个元素的位置,相当于一个指针,表空时len= ...

  5. c数据结构 顺序表和链表 相关操作

    编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...

  6. java顺序表和树的实现

    一.顺序表 1.线性表 //java顺序表的实现,如ArrayList就是用线性表实现的,优点是查找快,缺点是添加或删除要移动很多元素,速度慢 public class SequenceList { ...

  7. 数据结构顺序表删除所有特定元素x

    顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...

  8. 顺序表C语言版

    #include <stdio.h> /* * 顺序表最多输入N个数 */ #define N 10 #define OK 1 #define ERROR -1 struct sequeu ...

  9. C#线性表之顺序表

    线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这种一对一的关系指的是数据元素之间的位置关系,即: ...

  10. C语言 线性表 顺序表结构 实现

    一个能够自动扩容的顺序表 ArrList (GCC编译). #include <stdio.h> #include <stdlib.h> #include <string ...

随机推荐

  1. Zabbix-3.0.3实现微信(WeChat)告警

    导读 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告 ...

  2. ios 将图片做成圆形

    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oiuyfdsa.png ...

  3. Caused by: java.lang.ClassNotFoundException[android的终极解决错误]

    from:http://blog.csdn.net/changemyself/article/details/7861525 08-13 18:29:22.924: E/AndroidRuntime( ...

  4. 单台centos7.3 虚拟机实现主从复制和哨兵集群

    环境: centos7.3一台 部署图: 从服务器配置: slaveof 哨兵配置: port sentinel monitor m1 127.0.0.1 6379 2 sentinel monito ...

  5. Spring 拦截器的使用

    一.Web.xml配置 在Web.xml 配置Spring核心控制器DispatcherServlet接收所有请求 <servlet> <servlet-name>spring ...

  6. CLR via 笔记4.2 类型转换 is 与 as 区别

    is 和 as 操作符是用来进行强制类型转换的 is : 检查一个对象是否兼容于其他指定的类型,并返回一个Bool值,永远不会抛出异常 object o = new object(); if (o i ...

  7. windows server 2008 R2域中的DC部署 分类: AD域 Windows服务 2015-06-06 21:09 68人阅读 评论(0) 收藏

    整个晚上脑子都有点呆滞,想起申请注册好的博客还从来都不曾打理,上来添添生机.从哪里讲起呢,去年有那么一段时间整个人就陷在域里拔不出来,于是整理了一些文档,害怕自己糊里糊涂的脑子将这些东西会在一觉醒来全 ...

  8. Linux系统CPU核数等信息查看

    版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. https://blog.csdn.net/fgf00/article/details/52584 ...

  9. Python程序员鲜为人知但你应该知道的16个问题(转)

    add by zhj: 没找到原文出处,只能找到转载的,文中说有17个坑,其实是16个 全文如下 这篇文章主要介绍了Python程序员代码编写时应该避免的16个“坑”,也可以说成Python程序员代码 ...

  10. Linux 下的同步机制

    2017-03-10 回想下最初的计算机设计,在单个CPU的情况下,同一时刻只能由一个线程(在LInux下为进程)占用CPU,且2.6之前的Linux内核并不支持内核抢占,当进程在系统地址运行时,能打 ...