线性表定义
#ifndef LINEARLIST_H_INCLUDED
#define LINEARLIST_H_INCLUDED
#include <bits/stdc++.h>
using namespace std;
template<class T>
class LinearList {
public:
LinearList() {cout << "Constructing a LinearList in default way" << endl;}
~LinearList() {cout << "Desctructing a LinearList in default way" << endl;}
virtual int Size() const = 0; // Get the max length
virtual int Length() const = 0; // Get length
virtual int Search(T& x) const = 0; // Search the element x in LinearList
virtual int Locate(int i) const = 0; // Get the index of i-th element
virtual bool GetData(int i, T& x) const = 0; // Get the value of i-th element
virtual void SetData(int i, T& x) = 0; // Modify the value of i-th element
virtual bool Insert(int i, T& x) = 0; // Insert x in index i-1
virtual bool Remove(int i, T& x) = 0; // Delete i-th element
virtual bool IsEmpty() const = 0; // Whether the SequentialList is empty or not
virtual bool IsFull() const = 0; // Whether the SequentialList is full or not
virtual void Sort() = 0; // Sort all elements in SequentialList by non-decreasing
virtual void Input() = 0; // Input elements
virtual void Output() = 0; // Output all elements in SequentialList
//virtual LinearList<T> operator=(LinearList<T>& L) = 0; // Overloading =
};
#endif // LINEARLIST_H_INCLUDED
#ifndef SinglyLinkedListEDLIST_H_INCLUDED
#define SinglyLinkedListEDLIST_H_INCLUDED
#include <bits/stdc++.h>
using namespace std;
template<class T>
class LinkNode {
public:
LinkNode(LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by default way!" << endl; link = ptr;}
LinkNode(const T& item, LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by defined way!" << endl; data = item, link = ptr;}
~LinkNode() {cout << "Desctructing a LineNode!" << endl; delete link;}
T data; // Data Scope
LinkNode<T> *link; // Point Scope, Point to next Node
};
// SinglyLinkedListList with head node
template <class T>
class SinglyLinkedList : public LinkNode<T> {
public:
SinglyLinkedList() {cout << "Constructing a SinglyLinkedListList by default way" << endl; first = new LinkNode<T>;}
SinglyLinkedList(const T&x);
SinglyLinkedList(SinglyLinkedList<T>& L); // Deep copy constructor function
~SinglyLinkedList() {cout << "Destructing a SinglyLinkedListNode by default way" << endl; delete first;}
void makeEmpty(); // Delete all nodes in the SinglyLinkedList
int Length() const; // Get the length of the SinlyLinkedList
LinkNode<T> *GetHead() const {return first;} // Return first
LinkNode<T> *Search(T x); // x can only be a variable while T& x
LinkNode<T> *Locate(int i); // Get the i-th node's address
bool GetData(int i, T& x); // Get the data in i-th node and let x = data
void SetData(int i, T& x); // Make the data in i-th node equals x
bool Insert(int i, T& x); // Insert a node in i-th with its' data be x
bool Remove(int i, T& x); // Delete the i-th node and make x = node[i]->data
bool IsEmpty() const {return first->link == NULL ? true : false;}
bool IsFull() const {return false;}
void Sort(); // Sort all datas in the SinglyLinkedList by non-decreasing way
void Input(); // Input all elements
void Output(); // Out all elements
SinglyLinkedList<T>& operator=(SinglyLinkedList<T>& L); // Overloading operator =
protected:
LinkNode<T> *first; // Point to Head node
};
#endif // SinglyLinkedListEDLIST_H_INCLUDED
顺序表实现
#include <bits/stdc++.h>
#include "SequentialList.h"
using namespace std;
template<class T>
SequentialList<T>::SequentialList(int sz) { // Constructor
cout << "Constructing a SequentialList in defined way" << endl;
if(sz >= 0) {
maxSize = sz; last = -1;
data = new T[maxSize];
if(data == NULL) {
cerr << "Invalid allocation" << endl;
exit(1);
}
}
else {
cerr << "Invalid size" << endl;
exit(1);
}
}
template<class T>
SequentialList<T>::SequentialList(SequentialList<T>& L) { // Deep copy constructor function
maxSize = L.Size();
last = L.Length() - 1;
T value;
data = new T[maxSize];
if(data == NULL) {
cerr << "Invalid allocation" << endl;
exit(1);
}
for(int i = 1; i <= last+1; ++i) {
L.GetData(i, value);
data[i-1] = value;
}
}
template<class T>
int SequentialList<T>::Search(T& x) const {
for(int i = 0; i <= last; ++i) {
if(data[i] == x) {return i+1;} // Return the order of element x in SequentialList
}
return 0; // not found
}
template<class T>
int SequentialList<T>::Locate(int i) const {
if(i >= 1 && i <= last+1) {return i;}
return 0;
}
template<class T>
bool SequentialList<T>::GetData(int i, T& x) const {
if(i <= 0 || i > last+1) {return false;}
else {
x = data[i-1];
return true;
}
}
template<class T>
void SequentialList<T>::SetData(int i, T& x) {
if(i <= 0 || i > last+1) {
cerr << "Invalid order of element x" << endl;
exit(1);
}
data[i-1] = x;
}
template<class T>
bool SequentialList<T>::Insert(int i, T& x) {
if(i <= 0 || i > last+2) {
cerr << "Invalid order of element x" << endl;
return false;
}
else {
last++;
for(int pos = last; pos >= i; --pos) {
data[pos] = data[pos-1];
}
data[i-1] = x;
return true;
}
}
template<class T>
bool SequentialList<T>::Remove(int i, T& x) {
if(i <= 0 || i > last+1) {
cerr << "Invalid order of element x" << endl;
return false;
}
else {
x = data[i-1];
for(int pos = i-1; pos < last; ++pos) {
data[pos] = data[pos+1];
}
last--;
return true;
}
}
template<class T>
bool SequentialList<T>::IsEmpty() const {
return last == -1 ? true : false;
}
template <class T>
bool SequentialList<T>::IsFull() const {
return maxSize <= last ? true : false;
}
template <class T>
void SequentialList<T>::Sort() { // Sort SequentialList in non-decreasing way
for(int i = last; i > 0; --i) {
for(int j = i-1; j >= 0; --j) {
if(data[j] > data[i]) {
T tmp = data[j];
data[j] = data[i];
data[i] = tmp;
}
}
}
}
template <class T>
void SequentialList<T>::Input() {
cout << "Please enter you final index of element to construct a SequentialList " << endl;
while(true) {
cin >> last;
if(last < maxSize && last >= 0) {
break;
}
else {
cout << "Invalid index " << endl;
cout << "The number of elements in SequentialList not exceed " << maxSize << endl;
}
}
for(int i = 0; i <= last; ++i) {
cin >> data[i];
}
}
template <class T>
void SequentialList<T>::Output(){
cout << "The last index of the SequentialList is " << last << endl;
for(int i = 0; i <= last; ++i) {
cout << "#No." << i+1 << ": " << data[i] << endl;
}
}
template <class T>
SequentialList<T> SequentialList<T>::operator=(SequentialList<T>& L) {
int sz = L.Size();
T value;
SequentialList<T> newSequentialList = new SequentialList(sz);
newSequentialList.last = L.last;
for(int i = 0; i <= last; ++i) {
L.GetData(i+1, value);
newSequentialList.data[i] = value;
}
return newSequentialList;
}
int main() {
SequentialList<int> seqList(10);
seqList.Input();
seqList.Output();
SequentialList<int> seqListPlus = seqList;
seqListPlus.Sort();
seqListPlus.Output();
int a = 3;
int pos = seqListPlus.Search(a);
cout << pos << endl;
if(seqListPlus.GetData(3, a)){
cout << "Jesus Christ, Found it" << endl;
cout << a << endl;
}
cout << "size " << seqListPlus.Size() << endl;
if(seqListPlus.IsFull()) {
cout << "God like, is full" << endl;
}
else cout << "Oh, not full" << endl;
SequentialList<double> emptySequentialList(0);
if(emptySequentialList.IsEmpty()) {
cout << "God like, is empty" << endl;
}
double num = 0.1;
if(emptySequentialList.Insert(1, num)) {
cout << "Inserted successfully" << endl;
}
emptySequentialList.Output();
double val;
emptySequentialList.Remove(1, val);
emptySequentialList.Output();
cout << val << endl;
return 0;
}