线性表的顺序存储结构 顺序表(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


相关文章
|
算法 C++ 容器
模拟实现c++中的list模版
模拟实现c++中的list模版
|
机器学习/深度学习 存储 C++
【C++数据结构——线性表】顺序表的基本运算(头歌实践教学平台习题)【合集】
本文档介绍了线性表的基本运算任务,涵盖顺序表和链表的初始化、销毁、判定是否为空、求长度、输出、查找元素、插入和删除元素等内容。通过C++代码示例详细展示了每一步骤的具体实现方法,并提供了测试说明和通关代码。 主要内容包括: - **任务描述**:实现顺序表的基本运算。 - **相关知识**:介绍线性表的基本概念及操作,如初始化、销毁、判定是否为空表等。 - **具体操作**:详述顺序表和链表的初始化、求长度、输出、查找、插入和删除元素的方法,并附有代码示例。 - **测试说明**:提供测试输入和预期输出,确保代码正确性。 - **通关代码**:给出完整的C++代码实现,帮助完成任务。 文档
703 5
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
677 7
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
511 2
|
存储 编译器 C++
C++ initializer_list&&类型推导
在 C++ 中,`initializer_list` 提供了一种方便的方式来初始化容器和传递参数,而右值引用则是实现高效资源管理和移动语义的关键特性。尽管在实际应用中 `initializer_list&&` 并不常见,但理解其类型推导和使用方式有助于深入掌握现代 C++ 的高级特性。
334 4
|
存储 算法 C++
【C++打怪之路Lv10】-- list
【C++打怪之路Lv10】-- list
343 1
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
456 2
|
存储 C++ 容器
C++入门9——list的使用
C++入门9——list的使用
378 1
|
存储 缓存 C++
C++番外篇——list与vector的比较
C++番外篇——list与vector的比较
472 0
|
C++
C++番外篇——list的实现
C++番外篇——list的实现
256 0

热门文章

最新文章