MyList.h
MyList.h
//Abstract data type for List
template <class t>
class List {
void Clear (); Null linear table
bool IsEmpty (); Returns True
bool Append (T value) when the linear table is empty; Add an element value at the end of the table with the length of the table increased by 1
bool Insert (int p, T value); Inserts an element value on position p, and the length of the table increases by 1
bool (int p); Deletes the element on position p, the length of the table minus 1
T getValue (int p); Returns the element value T setValue of position p
(int p, t value); Modifies the element value of position p with value
};
ArrList.h
ArrList.h #include <iostream> #include <string> #include "myList.h" using namespace std; Template <class t>//assumes the element type of the sequential table is T class Arrlist:public list<t> {//sequential table, vector private: The type of the linear table and the value space int maxSize; A private variable, the maximum length of an instance of a sequential table int curlen; The private variable, the current length of the ordinal table instance int position; Private variable, current processing position T *alist;
Private variables, storing instances of sequential tables public://Sequential table arrlist (const int size) {//Create a new sequential table, parameter is the maximum length of the table instance
maxSize = size;
Alist = new T[maxsize];
Curlen = Position = 0;
~arrlist () {//destructor, to eliminate the table instance Delete [] alist;
void Clear () {//Clears the contents of the sequential table, becoming an empty table delete [] alist;
Curlen = Position = 0;
Alist = new T[maxsize]; int length (); Returns the current actual length of this order table bool Append (T value); Add an element value at the end of the table with the length of the table increased by 1 bool Insert (int p, T value); Inserts an element value on position p, and the length of the table increases by 1 bool (int p); Deletes the element on position p, the length of the table minus 1 int getpos (const T value); Finds the element with value in the linear table, and returns the position void print () that appears for the 1th time.
Print Linear table};
Template <class t>//assumed the element type of the sequential table is T int arrlist<t>:: GetPos (const T value) {
int i;
for (i=0;i<n;i++) {if (Value==arrlist[i]) {p=i;
return true;
return false; }//Set the type of the element is T, the alist is the array that stores the order table, the MAXSIZE is its maximum length;//P is the insertion position for the new element value,//Insert Success returns TRUE, or false template <class t>
Assume that the element type of the sequential table is T bool arrlist<t>:: Insert (int p, const T value) {int i;
if (curlen>=maxsize) {cout<< "The List is Overflow" <<endl;
return false; } if (p<0| | P>curlEN) {cout<< "insertion point is illegal" <<endl;
return false;
for (i=curlen;i>p;i--) {alist[i]=alist[i-1];
} Alist[p]=value;
curlen++;
return true;
Template <class t>//assumed the element type of the sequential table is T bool arrlist<t>:: del (int p) {int i;
if (curlen<=0) {cout<< "No element to delete\n" <<endl;
return false; } if (p<0| |
p>curlen-1) {cout<< "deletion is illegal\n" <<endl;
return false;
for (i=p;i<curlen-1;i++) {alist[i]=alist[i+1];
} curlen--;
return true; Template <class t>//assumes the element type of the sequential table is T void arrlist<t>:: print () {for (int i = 0; i < Curlen;
i++) cout << alist[i]<< ""; cout << Endl;
Start each element from position p to move left until Curlen,}
Main.cpp
Main.cpp
#include <cstdlib>
#include <iostream>
#include "arrList.h"
using namespace STD;
int main (int argc, char *argv[])
{
Arrlist<int>arr (8);
Arr.insert (0,1);
Arr.insert (1,2);
Arr.insert (2,3);
Arr.insert (3,4);
Arr.insert (4,5);
Arr.insert (5,6);
Arr.insert (6,7);
cout<< "Print elem:\n";
Arr.print ();
cout<< "Insert a elem 15:\n";
Arr.insert (4,15);
cout<< "Print elem:\n";
Arr.print ();
cout<< "Delete a elem:\n";
Arr.del (4);
cout<< "Print elem:\n";
Arr.print ();
return 0;
}