广义表Generalized List
广义表是在顺序表SeqList的基础上来的,其结点分为三种类型,如下图:
广义表的存储表示(大致是这个感觉)
主函数
#include "GenList.h"
#include <fstream>
#include <string>
#include <cassert>
using namespace std;
int main(){
ifstream fin1("data.txt");
assert(fin1);
string str;
fin1 >> str;
cout << "The genlist in the file is: \n" << str << endl;
GenList<char> gl1;
fin1.close();
ifstream fin("data.txt");
assert(fin);
fin >> gl1;
cout << "\nSome information about Genlist gl1:\n";
cout << "\nThe data of GenList is:" << endl;
cout << gl1 << endl;
cout << "\nThe depth of the GenList is: " << gl1.depth() << endl;
cout << "The length of the GenList is: " << gl1.Length() << endl;
cout << "The First Element is: ";
int temp = 0;
if (gl1.First()){
temp = gl1.First()->utype;
if (temp == 1){
cout << "AtomNode: " << gl1.First()->info.value << endl;
}
else if (temp == 0){
cout << "HeadNode." << endl;
}
else{
cout << "SubListNode." << endl;
}
}
else{
cout << "NULL!" << endl;
}
cout << "The Second Element is: ";
if (gl1.Next(gl1.First())){
temp = gl1.Next(gl1.First())->utype;
if (temp == 1){
cout << "AtomNode: " << gl1.First()->info.value << endl;
}
else if (temp == 0){
cout << "HeadNode! " << endl;
}
else{
cout << "SubListNode!" << endl;
}
}
else{
cout << "NULL!" << endl;
}
// GenList<char> gl2(gl1);//浅复制
GenList<char> gl2;
gl2.Copy(gl1);
cout << "\ngl2 is a copy of gl1:\n";
cout << "The data of gl2 is: " << endl;
cout << gl2 << endl;
if(gl1.equal(gl2)) cout<<"gl1 equals gl2.";
else cout<<"gl1 do not equals gl2.";
char rem;
cout << "\nInput the data that you want to Remove: " << endl;
cin >> rem;
gl2.delvalue(rem);
cout << "The current GenList gl2 is: " << endl;
cout << gl2<< endl;
cout << "\nInput the data that you want to Remove: " << endl;
cin >> rem;
gl1.delvalue(rem);
cout << "The current GenList gl1 is: " << endl;
cout << gl1<< endl;
return 0;
}
首先分析顺序表SeqList头文件
顺序表回顾
顺序表SeqList类定义
const int defaultSize = 100;
template <typename T>class SeqList
{
protected:
T *data;
int maxSize;
int last;
public:
SeqList(int sz = defaultSize);
SeqList(SeqList<T> &L);
~SeqList(){
delete []data;
}
void reSize(int newSize);
int Size() const{
return maxSize;
}
int Length()const{
return last+1;
}
int Search(T &x) const;
int Locate(int i) const;
T* getData(int i) const{
return (i>0 && i<=last+1)?&data[i-1]:NULL;
}
void setData(int i, T &x){
if (i>0 && i<=last+1){
data[i-1] = x;
//这里i是个数不是下标(这些示范程序老是变动i的意义。。)
}
}
bool Insert(int i, T &x);
bool Remove(int i, T &x);
bool IsEmpty(){
return (last == -1);
}
bool IsFull(){
return (last == maxSize-1);
}
void Sort();
void input();
void output();
SeqList<T> operator = (SeqList<T> &L);
friend istream& operator >> (istream &in, SeqList<T> &R){
R.last = -1;
while (!in.eof()){
R.last++; //last是最后一个元素的下标
if (R.last == R.maxSize){
//如果内存不够就分配两倍内存
R.reSize(2*R.maxSize);
}
assert(in >> R.data[R.last]);
}
return in;
}
friend ostream& operator << (ostream &out, SeqList<T> &R){
for (int i = 0; i <= R.last; i++){
cout << "#" << i+1 << ":\t" << R.data[i] << endl;
}
return out;
}
};
构造函数
template <typename T>SeqList<T>::SeqList(int sz){
if (sz > 0){
maxSize = sz;
last = -1;
data = new T[maxSize];
if (data == NULL){
cerr << "Memory allocating error!" << endl;
exit(1);
}
}
}
template <typename T>SeqList<T>::SeqList(SeqList<T> &L){
maxSize = L.Size();
last = L.Length() - 1;
data = new T[maxSize];
if (data == NULL){
cerr << "Memory allocating error!" << endl;
exit(1);
}
for (int i = 1; i <= last+1; i++){
data[i-1] = *(L.getData(i));
}
}
reSize函数
template<typename T>void SeqList<T>::reSize(int newSize){
if (newSize <= 0){
cerr << "Invalid array index!" << endl;
return;
}
if (newSize != maxSize){
T *newarray = new T[newSize];
if (newarray == NULL){
cerr << "Memory allocating error!" << endl;
exit(1);
}
int n = last + 1;
T *srcptr = data;
T *destptr = newarray;
while (n--){
*destptr++ = *srcptr++;
}
delete []data; //这里指针还在,空间被删除了
data = newarray;
maxSize = newSize;
}
}
Search和Locate函数
template<typename T>int SeqList<T>::Search(T &x)const{
for (int i = 0; i <= last; i++){
if (data[i] == x){
return i+1;
}
}
return 0;
}
template<typename T>int SeqList<T>::Locate(int i)const{
if (i >= 1 && i <= last+1){
return i;
}
else return 0;
}
Insert和Remove函数
template<typename T>bool SeqList<T>::Insert(int i, T &x){
if (last == maxSize-1) return false;
if (i < 0 || i > last+1) return false;
for (int j = last; j >= i; j--) data[j+1] = data[j];
data[i] = x;
last++;
return true;
}
template<typename T>bool SeqList<T>::Remove(int i, T &x){
if (last == -1){
return false;
}
if (i < 1 || i > last+1){
//这里i是个数,last是下标
return false;
}
x = data[i-1];
for (int j = i; j <= last; j++){
data[j-1] = data[j]; //这里j是下标,替换了i的意味
//删除第i=1个元素即删除第j=0个元素,所以这里首个被替换的是data[j-1]
}
last--;
return true;
}
Sort函数
template<typename T>void SeqList<T>::Sort(){
for (int i = 1; i <= last; i++){
for (int j = last; j >= i; j--){
if (data[j-1] > data[j]){
T tmp = data[j-1];
data[j-1] = data[j];
data[j] = tmp;
}
}
}
}
input函数
template<typename T>void SeqList<T>::input(){
cout << "Input the size of the list which will be created:";
while (1){
assert(cin >> last);
last--;
if (last < 0){
cout << "Input error, the size must be positive!\n";
cout << "Input the size again:";
}
else if (last > maxSize-1){
cout << "Input error, the size must be less than maxSize!\n";
cout << "Input the size again:";
}
else break;
}
cout