线性表的顺序存储结构 顺序表(sequential list) C++

简介: 线性表的顺序存储结构 顺序表(sequential list) C++

SeqList.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__
template<class T>
class SeqList
{
public:
  SeqList();
  SeqList(T *_data, int _length);
  virtual ~SeqList();
public:
  int getLength();
  void Insert(int _pos, T _data);
  T Delete(int _pos);
  void setElement(int _pos, T _data);
  T getValue(int _pos);
  int getLocation(T _data);
  void Traverse();
private:
  static const int MaxSize = 100;
  T data[MaxSize];
  int length;
};
#endif // !__SEQLIST_H__


SeqList.cpp


#include <iostream>
#include "SeqList.h"
using namespace std;
/* Constructor && Distructor */
template <class T>
SeqList<T>::SeqList()
{
  length = 0;
}
template <class T>
SeqList<T>::SeqList(T *_data, int _length)
{
  if (_length > MaxSize || _length < 0)
  throw "Illegal Length";
  else
  {
  length = _length;
  for (int i = 0; i < length; i++)
    data[i] = *(_data + i);
  }
}
template <class T>
SeqList<T>::~SeqList()
{
}
/* Operating Function */
template <class T>
int SeqList<T>::getLength()
{
  return length;
}
template <class T>
void SeqList<T>::Insert(int _pos, T _data)
{
  if (_pos > length || _pos < 0)
  throw "Illegal Posistion";
  for (int i = length; i >= _pos; i--)
  data[i] = data[i - 1];
  data[_pos - 1] = _data;
  length += 1;
}
template <class T>
T SeqList<T>::Delete(int _pos)
{
  T tempData = data[_pos];
  if (_pos > length || _pos <= 0)
  throw "Illegal Posistion";
  for (int i = _pos - 1; i < length - 1; i++)
  data[i] = data[i + 1];
  length -= 1;
  return tempData;
}
template <class T>
void SeqList<T>::setElement(int _pos, T _data)
{
  if (_pos > length || _pos < 0)
  throw "Illegal Posistion";
  data[_pos - 1] = _data;
}
template <class T>
T SeqList<T>::getValue(int _pos)
{
  if (_pos > length || _pos < 0)
  throw "Illegal Posistion";
  else
  return data[_pos - 1];
}
template <class T>
int SeqList<T>::getLocation(T _data)
{
  for (int i = 0; i < length; i++)
  if (data[i] == _data)
    return i + 1;
}
template <class T>
void SeqList<T>::Traverse()
{
  for (int i = 0; i < length; i++)
  cout << data[i] << " ";
  cout << endl;
}


Sequential.cpp


#include <iostream>
#include "SeqList.h"
#include "SeqList.cpp"
using namespace std;
int main()
{
  int arr[5] = { 0, 0, 0, 0, 0 };
  SeqList<int> list(arr, 5);
  list.Traverse();
  list.Insert(1, 1);
  list.Traverse();
  list.Delete(1);
  list.Traverse();
  list.setElement(2, 2);
  list.Traverse();
  cout << list.getValue(2) << endl;
  cout << list.getLocation(2) << endl;
  cout << list.getLength() << endl;
  return 0;
}


输出结果:


0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0
0 2 0 0 0
2
2
5


相关文章
|
3天前
|
C++
【C++基础】程序流程结构详解
这篇文章详细介绍了C++中程序流程的三种基本结构:顺序结构、选择结构和循环结构,包括if语句、三目运算符、switch语句、while循环、do…while循环、for循环以及跳转语句break、continue和goto的使用和示例。
13 2
|
1月前
|
C++ 容器
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——AVL树
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——AVL树
25 5
|
1月前
|
C++
c++学习笔记03 程序流程结构
C++学习笔记,主要介绍了程序流程结构,包括顺序结构、选择结构和循环结构。选择结构中详细解释了if语句、三目运算符和switch语句的用法和注意事项。循环结构部分则涵盖了while循环、do-while循环和for循环的语法和使用技巧。此外,还介绍了跳转语句,包括break、continue和goto语句的用途和用法。
28 0
|
1月前
|
关系型数据库 C++ 容器
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——红黑树
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——红黑树
26 0
|
3月前
|
编译器 C++ 容器
【C++/STL】:list容器的深度剖析及模拟实现
【C++/STL】:list容器的深度剖析及模拟实现
33 2
|
3月前
|
存储 算法 C++
C++一分钟之-容器概览:vector, list, deque
【6月更文挑战第21天】STL中的`vector`是动态数组,适合随机访问,但插入删除非末尾元素较慢;`list`是双向链表,插入删除快但随机访问效率低;`deque`结合两者优点,支持快速双端操作。选择容器要考虑操作频率、内存占用和性能需求。注意预分配容量以减少`vector`的内存重分配,使用迭代器而非索引操作`list`,并利用`deque`的两端优势。理解容器内部机制和应用场景是优化C++程序的关键。
50 5
|
3月前
|
编译器 C语言 C++
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
|
3月前
|
存储 C++ 容器
【C++/STL】:list容器的基本使用
【C++/STL】:list容器的基本使用
27 1
|
2月前
|
存储 算法 程序员
C++基础知识(八:STL标准库(Vectors和list))
C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 stacks(栈)等. STL容器的提供是为了让开发者可以更高效率的去开发,同时我们应该也需要知道他们的底层实现,这样在出现错误的时候我们才知道一些原因,才可以更好的去解决问题。