Sequential table of linear tables
First, header file: SeqList.h
header files for sequential linear tables
#include <iostream>
Const int MaxSize = 100;
//define Order Table Seqlist template class
Template<class datatype>
class seqlist{
Public:
//Sequential Table parameterless constructor (Create an empty sequential table)
Seqlist () {length = 0}
//Order table has a parameter constructor (creates a sequential table of length n)
Seqlist (DataType array[], int n);
Sequential table destructor
~seqlist () {}
//The length of the order table
int GetLength () {return length;}
//Order table bitwise LOOKUP, return elements of I position
DataType getelement (int i);
The sequential table finds by value, returning the element's location
int getlocal (DataType x);
The sequential table inserts the specified element
void Insert (int i, DataType x) at the specified location;
The sequential table deletes the element, returning the deleted element
DataType Delete (int i);
Element
void Printseqlist () in the output order table;
Private:
//one-dimensional array, storing data elements
DataType data[maxsize];
The length of the sequential table
int length;
};
Implementing sequential table with parameter constructor
Template<class datatype>
Seqlist<datatype>::seqlist (DataType array[], int n)
{
if (n > MaxSize)
{
Throw "Incoming order table length is too long";
}
Assigning a value to an array of stored elements in a sequential table
for (int i = 0; i < n; i++)
{
Data[i] = Array[i];
}
Assigning a value to the length of a sequential table
length = n;
}
Implementing sequential Table bitwise lookups
Template<class datatype>
DataType seqlist<datatype>::getelement (int i)
{
Determine whether the position is reasonable
if (I < 1 | | I >length)
{
Throw "wrong position";
}
Else
{
Returns the element at the specified position
return data[i-1];
}
}
Implement sequential table lookup by value, return the location of the element
Template<class datatype>
int seqlist<datatype>::getlocal (DataType x)
{
Traversing elements of a sequential table
for (int i = 0; i < length; i++)
{
Determines whether the specified element is in the order table
if (data[i] = = x)
{
Returns the position of the specified element in the order table
return (i + 1);
}
}
If the specified element is not in the order table, the return position is 0
return 0;
}
//implement sequential Table Insert element
Template<class datatype>
void Seqlist<datatype>::insert (int index, DataType x)
{
//determine if the insertion position is reasonable
if (length >= MaxSize)
{
Throw "order table is full";
if (index<1 | | index>length + 1)
{
Throw ' inserted element is in the wrong position ";
}
//How to insert a reasonable position, the whole element of the order table from the last position to the specified interpolation position to move backward one position
for (int j = length; J >= Index; j--)
{
Data[j] = da TA[J-1];
}
//To place the inserted position into the specified element
Data[index-1] = x;
length++;
}
Implementing a sequential table deletes an element at a specified location
Template<class datatype>
DataType seqlist<datatype>::D elete (int index)
{
Declaring the element to be removed
DataType x;
Determine if the location you want to delete is reasonable
if (index<1 | | index>length)
{
Throw "deleted position is wrong";
}
Else
{
Remove the element at the specified position
x = Data[index-1];
Moves the element after the specified position all the way forward one position
for (int i = index; i < length; i++)
{
DATA[I-1] = Data[i];
}
After deleting an element in a sequential table, its length is reduced by 1
length--;
}
return x;
}
Elements in the Sequential output order table
Template<class datatype>
void Seqlist<datatype>::P rintseqlist ()
{
if (length < 1)
{
Throw "no elements in order table";
}
Else
{
Sequential output order table elements
for (int i = 0; i < length; i++)
{
cout << Data[i] << "";
}
cout << Endl;
}
}
Second, the Test Linear Table Order table: TestSeqList.cpp
#include <iostream>
#include "SeqList.h"
using namespace Std;
void Show ()
{
cout << "---------------------------------------" << Endl;
}
int main ()
{
int array[10] = {1,3,4,2,5,6,8,7,9,10};
seqlist<int> seqlist = seqlist<int> (array,10);
cout << "Order table:" << Endl;
Seqlist.printseqlist ();
Show ();
cout << "The length of the sequential table is:" << seqlist.getlength () << Endl;
cout << "The third position element is:" << seqlist.getelement (3) << Endl;
cout << the location of element 3 is: "<< seqlist.getlocal (3) << Endl;
Show ();
cout << "inserting elements in 5th place" << Endl;
Seqlist.insert (5, 22);
cout << "Order table:" << Endl;
Seqlist.printseqlist ();
cout << "The length of the sequential table is:" << seqlist.getlength () << Endl;
Show ();
cout << "Remove elements from 5th position" << Endl;
Seqlist.delete (5);
cout << "Order table:" << Endl;
Seqlist.printseqlist ();
cout << "The length of the sequential table is:" << seqlist.getlength () << Endl;
Show ();
return 0;
}
The sequential table of linear table C + + implementation