// 顺序表.cpp -- 最基本的顺序表(经典顺序表)
// 完整的class.
// List abstract class -- 线性表的C++抽象类声明
template<class Elem> class List()
{
public:
// Reinitialize the list. the client is responsible for
// reclaiming the storange used by the list elements.
virtual void clear() = 0;
// Insert an element at the front of the right partition.
// Return true if successful, false if the list is full.
virtual bool insert(const Elem&) = 0;
// Append an element at the end of the right partition.
// Return true if successful, false if the list is full.
virtual bool append(const Elem&) = 0;
// Remove the first element of right partition. Return
// true if successful, false if the list is empty.
// The element removed is returned in the parameter.
virtual bool remove(Elem&) = 0;
// Place fence at list start, making left partition empty.
virtual void setStart() = 0;
// Place fence at list end, making right partition empty.
virtual void setEnd() = 0;
// Move fence one step left; no change if already at start.
virtual void prev() = 0;
// Move fence one step right; no change if already at end
virtual void next() = 0;
// Return length of left partition
virtual int leftLength() const = 0;
// Return length of right partition
virtual int rightLength() const = 0;
// If pos or more elements are in the list, set the size
// of left partition to pos and return true. Otherwise,
// do nothing and return false.
virtual bool setPos(int pos) = 0;
// Return in first parameter the first element of the
// right partition. Return true if successful, false
// if the right partition is empty.
virtual bool getValue(Elem&) const = 0;
// print the contents of the list
virtual void print() const = 0;
};
// Array-based list implementation -- 线性表的实现
template <class Elem>
class Alist: public List<Elem> //继承
{
private:
int maxSize; // Maximum size of list
int listSize; // Actual number of elements in list
int fence; // Position of fence
Elem* listArray; // Array holding list elementss
public:
AList(int size = DefaultListSize) // constructor
{
maxSize = size;
lastSize = fence = 0;
listArray = new Elem[maxSize];
}
~AList() { delete [] listArray;} // Destructor
void clear()
{
delete [] listArray;
listSize = fence = 0;
listArray = new Elem[maxSize];
}
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart() { fence = 0;}
void setEnd() { fence = listSize;}
void prev() { if (fence != 0) fence--;}
void next() { if (fence <= listSize) fence++;}
int leftLength()const {return fence;}
int rightLength()const {return lastsize - fence;}
bool setPos(int pos)
{
if ((pos >= 0) && (pos <= listSize)) fence = pos;
return (pos >= 0) && (pos <= listSize);
}
bool getValue(Elem& it)const()
{
if (rightLength() == 0) return flase;
else { it = listArray[fence]; reutrn true;}
}
void print()const
{
int temp = 0;
cout << " < ";
while (temp < fence) cout << listArray[temp++] << " ";
cout << " | ";
while (temp < ListSize) cout << listarray[temp++] << " ";
cout << " >\n";
}
};
template <class Elem> // Insert at front of right partition
bool Alist <Elem>::insert(const Elem& item)
{
if (listsize == maxSize) return flase; // List is full
for (int i = listsize; i > fence; i--) // Shift Elem up
listArray[i] = listArray[i-1]; // to make room
listArray[fence] = item;
listSize++; // Increment list size
return true;
}
// Remove and return first Elem in right partition
template <class Elem> bool AList <Elem>::remove(Elem& it)
{
if (rightLength() == 0) return false; // nothing in right
it = listArray[fence]; // copy removed elem
for (int i = fence; i < listSize -1; i++) // Shift them down
listArray[i] = listArray[i + 1];
listSize--; // Decrement Size
return true;
}
// 看完以后就觉得,原来掌握顺序表是很简单的事,只需要掌握几个点;
// 其中最重要的思想是确定位置。
// 知道现在的位置,开始的位置,结束的位置,数组的大小,该位置左右的大小。
分享到:
相关推荐
顺序表基本操作 顺序表实现 顺序表
顺序表的基本操作C语言 顺序表是一种基本的数据结构,在计算机科学中广泛应用。以下是关于顺序表的基本操作的知识点: 1. 初始化 顺序表的初始化是指创建一个空的顺序表,并分配所需的内存空间。在C语言中,可以...
数据结构顺序表的基本操作 本实验报告旨在介绍顺序表的基本操作,包括顺序表的生成、插入、删除、输出等基本运算。实验要求编写一个完整的程序,实现顺序表的基本操作,并设计一个简单的菜单,分别测试上述算法。 ...
根据给定的文件标题“顺序表的各种基本运算”以及描述中的功能需求,本文将详细介绍顺序表的基本操作及其具体实现。 ### 一、顺序表的概念 顺序表是一种线性表的数据结构,它通过连续的内存空间来存储数据元素,每...
实验一顺序表基本操作 实验一顺序表基本操作是大学生实验作业的一部分,旨在让学生掌握线性表中元素的前驱、后继的概念,以及顺序表的建立、插入元素、删除表中某元素的算法。在该实验中,学生需要完成并实现顺序表...
### 实验报告1.1顺序表基本操作的设计与实现 #### 实验背景 在计算机科学领域,线性表是基础且重要的数据结构之一,而顺序表作为线性表的一种实现方式,具有简单直观的特点,因此在教学实践中常被用来帮助学生理解...
在本实验“实验一 顺序表基本操作的实现”中,我们将深入探讨数据结构的核心概念——顺序表。数据结构是计算机科学中一个至关重要的领域,它研究如何有效地组织和存储数据,以便进行高效的检索、更新和删除等操作。...
【顺序表】是一种基本的数据结构,它在计算机科学中被广泛使用。顺序表的特点是它的元素按照线性的顺序存储在一块连续的内存区域中,因此访问任何位置的元素的时间复杂度为O(1),但插入和删除操作可能涉及到大量的...
本实验是关于**顺序表基本操作的应用实验**,旨在通过具体的操作来加深对顺序表这一数据结构的理解与掌握。顺序表是一种常见的线性表,其特点是元素在内存中连续存放,可以通过下标直接访问到任意位置的元素。本实验...
在数据结构的领域里,线性表有多种实现方式,其中顺序表是最简单也是最直观的一种。顺序表是指表中的元素在内存中是连续存放的,就像数组一样,可以通过下标直接访问任意位置的元素。 顺序表的优点在于其访问速度快...
(1)初始化顺序表L (2)从标准输入(键盘)逐个数据输入a,b,c,d,e元素 ,建立顺序表 (3)输出顺序表L (4)输出顺序表L的长度 (5)判断顺序表L是否为空 (6)输出顺序表L的第3个元素 (7)输出元素a的位置...
在计算机科学中,顺序表是一种基本的数据结构,它是由一系列元素组成的有序集合。在本节中,我们将讨论顺序表的建立和基本操作。 顺序表的定义 顺序表是一种线性表,它的元素是按顺序排列的。每个元素都有一个唯一...
本文将深入探讨顺序表的基本概念、操作以及实现方式。 顺序表,顾名思义,是数据元素按线性顺序排列的一种数据结构。在内存中,顺序表通常表现为一个一维数组,每个元素在数组中的位置对应其在表中的顺序。顺序表的...
1. **顺序表的基本操作**: - **初始化**:创建一个空的顺序表,通常涉及分配一段连续的内存空间。 - **插入操作**:在指定位置插入元素,可能需要移动后继元素以腾出空间。 - **删除操作**:移除指定位置的元素...
在顺序表中增加元素通常有两种情况:一是向表尾添加元素,这是最常见的情况,只需将新元素放到数组的最后一个位置即可。另一种是在特定位置插入元素,这需要移动插入位置之后的所有元素。例如,在Python中,`list....
在本主题中,我们将重点关注“顺序表”,这是一种简单但基础的数据结构。 顺序表是一种线性数据结构,其中元素按照它们被添加的顺序存储在一块连续的内存空间里。这种表的操作通常包括创建(建立)、插入、删除、...
【顺序表】是一种基本的线性数据结构,它将元素按照线性顺序依次存储在一块连续的内存区域中。在顺序表中,每个元素都有一个固定的位置,可以通过索引来访问。顺序表的操作主要包括初始化、插入数据、删除数据、获取...
在这个文档中,我们看到了使用C语言实现的顺序表(Sequential List)的基本操作。顺序表是一种数据结构,它以数组的形式存储数据,可以进行插入、删除、查找等操作。文档中的代码定义了一个名为`SqList`的结构体,...
本文将详细介绍如何在C++中使用模板方式来实现顺序表的基本操作,并通过一个具体的示例来演示如何合并两个顺序表。 #### 二、顺序表类的设计与实现 在本节中,我们将深入探讨顺序表类`SeqList`的实现细节。 #####...