此代码为笔者数据结构作业,仅供学习使用。
1、顺序表
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <ctime>
using namespace std;
const int defaultSize = 100;
template<class T>
class SeqList
{
private:
T* data;//指向动态内存分配数组的指针
int maxSize;//动态内存分配数组的容量
int last;//标识顺序表中最后一个元素的位置(反映顺序表中的元素个数)
void Resize(int newSize);
public:
SeqList(int sz=defaultSize);//构造函数
SeqList(const SeqList<T>& L);//拷贝构造函数
SeqList& operator=(const SeqList<T>& L);//赋值运算符函数
~SeqList();//析构函数
int Size() const;//返回顺序表的容量
int Length() const;//返回顺序表中元素的个数
bool IsEmpty() const;//顺序表是否为空
bool IsFull() const;//顺序表是否为满
bool Locate(int i) const;//检验顺序表中是否存在第i个元素
bool GetData(int i, T& x) const;//获取顺序表中第i个位置的元素
void SetData(int i, T& x);//将顺序表中第i个位置的元素赋值为x
int Search(T & x) const;//在顺序表中搜索x
bool Insert(int i, const T& x) ;//在顺序表的第i个元素的位置插入x
bool Remove(int i, T&x) ;//删除顺序表第i个位置的元素
void Sort() ;//对顺序表中元素进行排序
void Reverse ();//逆置顺序表中的元素
template<class _T>
friend istream& operator>>(istream& in, SeqList<_T> & L);//输入运算符重载
template<class _T>
friend ostream& operator<<(ostream& out, const SeqList<_T>& L);//输出运算符重载
T & operator[] ( int index );
const T & operator[] ( int index ) const;
void Push_Back ( const T& x);
void Pop_Back( );
typedef T * iterator;
typedef const T * const_iterator;
iterator Begin( )
{
return &data[0];
}
const_iterator Begin( ) const
{
return &data[0];
}
iterator End( )
{
return &data[ Length() ];
}
const_iterator End( ) const
{
return &data[ Length() ];
}
};
template<class T>
SeqList<T>::SeqList(int sz)
{
this->maxSize = sz;
this->data = new T[maxSize];
if(data==NULL)
{
exit(1);
}
else
{
cout<<"distribute successfully!"<<endl;
}
this->last = -1;
}
template<class T>
void SeqList<T>:: Resize(int newSize)
{
if(newSize<last+1)
return;
else
this->maxSize = newSize;
T tempdata = new T[last+1];
for(int i=0; i<=last; i++)
{
tempdata[i] = data[i];
}
this->data = new T[newSize];
if(data==NULL)
{
exit(1);
}
else
{
cout<<"distribute successfully!"<<endl;
}
for(int i=0; i<=last; i++)
{
this->data = tempdata[i];
}
}
template<class T>
SeqList<T>:: SeqList(const SeqList<T>& L)
{
this->maxSize = L.maxSize;
this->data = new T[maxSize];
this->last = L.last;
for(int i=0; i<=last; i++)
this->data[i] = L.data[i];
}
template<class T>
SeqList<T>& SeqList<T>::operator=(const SeqList<T>& L)
{
this->maxSize = L.maxSize;
delete[] this->data;
this->data = new T[maxSize];
if(data==NULL)
{
exit(1);
}
else
{
cout<<"distribute successfully!"<<endl;
}
this->last = L.last;
for(int i=0; i<=last; i++)
this->data[i] = L.data[i];
return *this;
}
template<class T>
SeqList<T>::~SeqList()
{
delete[] this->data;
this->last = -1;
this->maxSize=0;
}
template<class T>
int SeqList<T>:: Size() const
{
return this->maxSize;
}
template<class T>
int SeqList<T>::Length() const
{
return this->last+1;
}
template<class T>
bool SeqList<T>::IsEmpty() const
{
if(this->last==-1)
return true;
else
return false;
}
template<class T>
bool SeqList<T>::IsFull() const
{
if((this->last+1) == this->maxSize)
return true;
else
return false;
}
template<class T>
bool SeqList<T>::Locate(int i) const
{
if(i<=0||i>last+1)
return false;
else
return true;
}
template<class T>
bool SeqList<T>::GetData(int i, T& x) const
{
if(i<=0||i>this->last+1)
return false;
else
x = this->data[i-1];
return true;
}
template<class T>
void SeqList<T>::SetData(int i, T& x)
{
if(i>0&&i<=this->maxSize)
{
this->data[i-1] = x;
return;
}
else if(i==maxSize+1)
{
T* temp=new T[last];
if(temp==NULL)
{
exit(1);
}
else
{
cout<<"distribute successfully!"<<endl;
}
for(int j=0; j<last+1; j++)
{
temp[j] = this->data[j];
}
this->data = new T[maxSize*3];
if(data==NULL)
{
exit(1);
}
else
{
cout<<"distribute successfully!"<<endl;
}
this->maxSize = 3*maxSize;
for(int j=0; j<last+1; j++)
{
this->data[j] = temp[j];
}
this->data[i-1]=x;
last++;
return;
}
else
exit(1);
}
template<class T>
int SeqList<T>::Search(T & x) const
{
for(int i=0; i<=this->last; i++)
{
if(this->data[i]==x)
return i+1;
}
return false;
}
template<class T>
bool SeqList<T>::Insert(int i, const T& x)
{
if(i<=0||i>last+1)
return false;
else
{
for(int j=last; j>=i-1; j--)
{
this->data[j+1]=data[j];
}
this->data[i-1]=x;
last++;
return true;
}
}
template<class T>
bool SeqList<T>::Remove(int i, T&x)
{
if(i<=0||i>last+1)
return false;
x= this->data[i-1];
for(int j=i-1; j<last; j++)
this->data[j]=data[j+1];
last--;
return true;
}
template<class T>
void SeqList<T>::Sort()
{
for(int i=0; i<last+1; i++)
for(int j=0; j<last; j++)
{
if(data[j]>data[j+1])
{
T temp=data[j];
data[j] = data[j+1];
data[j+1]=temp;
}
}
}
template<class T>
void SeqList<T>::Reverse ()
{
int j=last;
for(int i=0; i<last/2+1; i++)
{
T temp = this->data[i];
data[i] = data[j-i];
data[j-i]=temp;
}
}
template<class _T>
istream& operator>>(istream& in, SeqList<_T> & L)
{
int i =1;
while(i++!=L.maxSize)
{
in>>L.data[++L.last];
if(getchar()=='\n')
break;
}
return in;
}
template<class _T>
ostream& operator<<(ostream& out, const SeqList<_T>& L)
{
for(int i=0; i<=L.last; i++)
{
out<<L.data[i]<<" ";
}
out<<endl;
return out;
}
int main()
{
SeqList<int> sList;
cin>>sList;
int i, val;
cin>>i>>val;
sList.Insert(i,val);
cout<<sList;
cin>>i;
sList.Remove(i,val);
cout<<sList;
cin>>val;
i=sList.Search(val);
if(i==0)
cout<<"Not found"<<endl;
else
cout<<i<<endl;
sList.Reverse();
cout<<sList;
}
2、链表
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef int T;
struct LinkNode //结点定义
{
T data;//数据域
LinkNode* next;//链域
LinkNode(const T& item, LinkNode* ptr=NULL)
{
data=item;
next=ptr;
}
LinkNode(LinkNode* ptr=NULL)
{
next=ptr;
}
};
class List
{
private:
LinkNode * first;
public:
List()//构造函数
{
this->first= new LinkNode();
}
List(const T& x)
{
this->first = new LinkNode();
this->first->next = new LinkNode(x);
}
// List(int n,);
List(const List& L)//拷贝构造函数
{
LinkNode* q = this->first =new LinkNode();
for(LinkNode *p=L.first->next; p!=NULL; p=p->next)
{
q ->next = new LinkNode(p->data);
q= q->next;
}
}
List& operator=(const List& L)//赋值运算符函数
{
LinkNode* q = this->first =new LinkNode();
for(LinkNode *p=L.first->next; p!=NULL; p=p->next)
{
q ->next = new LinkNode(p->data);
q= q->next;
}
return *this;
}
~List()//析构函数
{
this->MakeEmpty();
delete []first;
}
void InputFront(const T& elem)//头插法
{
LinkNode *pre = this->first->next;
LinkNode* newNode = new LinkNode(elem);
newNode->next = pre;
this->first->next = newNode;
}
void InputRear(const T& elem)//尾插法
{
LinkNode* p=NULL;
for( p=this->first->next; p!=NULL; p=p->next) {}
p=new LinkNode(elem);
}
void MakeEmpty()//清空链表
{
LinkNode*p = this->first;
while(p!=NULL)
{
LinkNode*q = p->next->next;
if(q==NULL)
break;
delete p->next;
p = q;
}
}
int Length() const//返回链表中结点个数
{
LinkNode *pre = first->next;
int num=0;
while(pre!=NULL)
{
++num;
pre=pre->next;
}
return num;
}
LinkNode* Search(const T& x)//在链表中查找元素x,找到返回它所在结点的地址,否则返回空
{
LinkNode * p =first->next;
while(p->data!=x)
{
p=p->next;
if(p==NULL)
return;
}
return p;
}
LinkNode* Locate(int i)//返回链表中第i个结点的地址,i取值不合法则返回空
{
if(i<=0||i>this->Length())
{
cout<<"输入非法!"<<endl;
return;
}
int j=0;
LinkNode* p = first->next;
for(j=1; j!=i; j++)
{
p=p->next;
}
return p;
}
bool GetData(int i, T& x)const//获取链表第i个元素,将它赋值给x
{
if(i<=0)
{
cout<<"输入非法!"<<endl;
return false;
}
int j=0;
LinkNode* p = first->next;
for(j=1; j!=i; j++)
{
p=p->next;
}
x = p->data;
return true;
}
void SetData(int i, const T& x)//设置链表第i个元素为x
{
if(i<=0||i>Length())
{
cout<<"输入非法!"<<endl;
return;
}
int j=0;
LinkNode* p = first->next;
for(j=1; j!=i; j++)
{
p=p->next;
}
p->data = x;
return;
}
bool Insert(int i, const T& x)//在链表的第i个位置上插入元素x
{
if(i<=0)
{
cout<<"输入非法!"<<endl;
return false;
}
int j=0;
LinkNode* p = first;
for(j=0; j<i-1; j++)
{
p=p->next;
}
LinkNode* newNode = new LinkNode(x,p->next);
if(NULL==newNode)
{
cout<<"无足够空间可分配"<<endl;
return false;
}
p->next = newNode;
return true;
}
bool Remove(int i, T& x)//删除链表第i个位置的元素,并将它的值赋值给x
{
if(i<=0||i>Length())
{
cout<<"输入非法!"<<endl;
return false;
}
int j=0;
LinkNode* p = first->next;
for(j=1; j!=i-1; j++)
{
p=p->next;
}
LinkNode *q = p->next->next;
x = p->next->data;
delete p->next;
p->next = q;
return true;
}
bool IsEmpty() const//返回链表是否为空
{
return first->next==NULL;
}
bool IsFull() const//返回链表是否为满
{
return (new LinkNode)==NULL;
}
void CopyList(const List& L)//复制链表
{
this->MakeEmpty();
LinkNode* pre = L.first;
LinkNode* prev = first;
while(pre!=NULL)
{
prev->next= new LinkNode(pre->next->data);
pre= pre->next;
}
}
void Sort()//对链表中结点进行排序
{
LinkNode * p =first->next;
LinkNode *q=NULL;
if(NULL==p)
return ;
int num = this->Length();
int maxIndex=1;
int Max =0;
for(int i=1; i<num; i++)
{
int j=1;
Max=0;
for(; j<num-i+2; j++)
{
if(Max<=p->data)
{
maxIndex = j;
Max = p->data;
}
q=p;
p=p->next;
}
SetData(maxIndex,q->data);
SetData(num,Max);
}
}
friend ostream& operator<<(ostream& out, const List& L)//输出流运算符重载
{
LinkNode * p =L.first->next;
while(p!=NULL)
{
out<<p->data<<" ";
p=p->next;
}
out<<endl;
return out;
}
friend istream& operator>>(istream& in, List& L)//输入流运算符重载
{
int x;
do{
in>>x;
L.InputRear(x);
}
while(getchar()!='\n');
return in;
}
};
int main()
{
List lst;
srand(time(NULL));
for(int i=1; i<=2; i++)
lst.Insert(i,rand()%50);
lst.Sort();
cout<<lst;
int val;
lst.Remove(2,val);
cout<<lst;
List lst1=lst;
lst.MakeEmpty();
lst=lst1;
cout<<lst;
}
3、循环链表
#include <iostream>
#include <cassert>
#include <fstream>
using namespace std;
enum InsMod {INF, INR};//头插还是尾插
template <typename T>class CircLinkNode{
public:
T data;
CircLinkNode<T> *link;
CircLinkNode(CircLinkNode<T> *ptr = NULL){//建立空结点
link = ptr;
}
CircLinkNode(const T &item, CircLinkNode<T> *ptr = NULL){//建立非空结点
data = item;
link = ptr;
}
};
template <typename T>class CircList{
public:
CircList()
{
this->first = new CircLinkNode<T>();
first->link=first;
last=first;
}
CircList(CircList<T> &L)//复制一个环链表
{
CircLinkNode<T>* p = L.first->link;
CircLinkNode<T>*q = this->first->link;
while(p!=first)
{
q= new CircLinkNode<T>(p->data);
q=q->link;
p=p->link;
}
last = q;
}
~CircList()
{
this->makeEmpty();
delete this->first;
}
void makeEmpty()
{
CircLinkNode<T>*p = this->first->link;
CircLinkNode<T>*q = p->link;
while(p!=first)
{
delete p;
p = q;
q=q->link;
}
}
int Length()const
{
int num =0;
CircLinkNode<T>* p = first->link;
while(p!=first)
{
++num;
p=p->link;
}
return num;
}
CircLinkNode<T> *Search(T x)
{
CircLinkNode<T>* p = first->link;
while(p->data!=x)
{
p=p->link;
if(p==first)
exit(1);
}
return p;
}
CircLinkNode<T> *Locate(int i)
{
if(i<1||i>Length())
{
exit(1);
}
int num=0;
CircLinkNode<T>*p = first->link;
while(p!=first)
{
++num;
if(num==i)
return p;
p=p->link;
}
return p;
}
bool getData(int i,T&x)const
{
if(i<1||i>Length())
{
return false;
}
int num=0;
CircLinkNode<T>*p = first->link;
while(p!=first)
{
++num;
if(num==i)
x = p->data;
p=p->link;
}
return true;
}
void setData(int i, T &x)
{
if(i<1||i>Length())
{
return;
}
int num=0;
CircLinkNode<T>*p = first->link;
while(p!=first)
{
++num;
if(num==i)
p->data=x;
p=p->link;
}
}
bool Insert(int i, T &x)
{
if(i<1||i>Length()+1)
{
return false;
}
CircLinkNode<T>* p =NULL;
if(i==1)
p = first;
else
p = Locate(i-1);
CircLinkNode<T>* q = p->link;
CircLinkNode<T> *newNode = new CircLinkNode<T>(x);
p->link = newNode;
newNode->link = q;
if(i==Length()+1)
{
last = newNode;
}
return true;
}
bool Remove(int i, T &x)
{
if(i<=0||i>Length())
{
return false;
}
int j=0;
CircLinkNode<T>* p = first;
for(j=1; j<i; j++)
{
p=p->link;
}
CircLinkNode<T> *q = p->link->link;
x = p->link->data;
if(i==Length())
last = p;
delete p->link;
p->link = q;
return true;
}
bool IsEmpty()const
{
return first->link==last;
}
bool IsFull()const
{
return (new CircLinkNode<T>() )==NULL;
}
void Sort()
{
CircLinkNode<T> * p =first->link;
CircLinkNode<T> *q=NULL;
if(NULL==p)
return ;
int num = this->Length();
int maxIndex=1;
int Max =p->data;
for(int i=1; i<num; i++)
{
int j=1;
for(; j<num-i+2; j++)
{
if(Max<=p->data)
{
maxIndex = j;
Max = p->data;
}
q=p;
p=p->link;
}
setData(maxIndex,q->data);
setData(num,Max);
}
}
void Inverse()//不要返回
{
int num = this->Length();
for(int i=1; i<num/2+1; i++)
{
int j=num-i+1;
CircLinkNode<T> * p = Locate(i);
CircLinkNode<T>* q = Locate(j);
T temp = p->data;
p->data = q->data;
q->data = temp;
}
}
void input(T endTag, InsMod im = INR)
{
if(im==INR)
{
Insert(Length()+1,endTag);
}
else{
Insert(1,endTag);
}
}
void output()
{
CircLinkNode<T>* p =first->link;
while(p!=first)
{
cout<<p->data<<" ";
p=p->link;
}
cout<<endl;
}
CircList<T> &operator = (CircList<T> &L)
{
this->makeEmpty();
CircLinkNode<T>* q = L.first->link;
while(q!=L.first)
{
input(q->data,INR);
q=q->link;
}
}
friend ostream& operator << (ostream &out, CircList<T> &L){
CircLinkNode<T> *current = L.first->link;
while (current != L.first){
out << current->data <<'\t';
current = current->link;
}
out<<endl;
return out;
}
friend istream& operator >> (istream &in, CircList<T> &L){//重新输入数据,向后生成
T val;
L.makeEmpty();//先清空
while (!in.eof()){
in >> val;
L.last->link = new CircLinkNode<T>(val);
assert(L.last->link);
L.last = L.last->link;
}
L.last->link = L.first;
in.clear();//当以ctrl_Z结束,流关闭,必须重新打开
return in;
}
protected:
CircLinkNode<T> *first, *last;
void inputFront(T endTag);
void inputRear(T endTag);
};
int main(){
CircList<int> list;
ifstream fin("D:\\cb代码\\Data&Structure\\list.txt");
//assert(fin);
fin >> list;
cout << "The initial list in the file is:\n" << list << endl;
list.Inverse();
cout << "Inverse the list, then the list is:\n" << list << endl;
cout << "========================================\n";
int i, elem;
cout << "Test the Insert, Remove and Search function:\n";
cout << "Each test will terminate by an invaid input.";
cout << "\n----------------------------------------\n";
cout << "1. Test the Insert(int i, T &elem):\n";
while (1){
cout << "Input the index i and data elem to insert: ";
cin >> i >> elem;
if (!cin){//流不正常
cin.clear();//恢复正常
cin.ignore(100,'\n');
break;
}
if (i < 0) break;
if (list.Insert(i, elem)) cout << "Insert successful!\n";
else cout << "Insert failed!\n";
}
cout << "\nAfter inserted\n" << list << endl;
cout << "----------------------------------------\n";
cout << "2. Test the Remove(int i, T &elem):\n";
while (1){
cout << "Input the index i in which you want to remove: ";
cin >> i;
if (!cin){
cin.clear();
cin.ignore(100,'\n');
break;
}
if (i < 0) break;
if (list.Remove(i, elem)) cout << "The element " << elem << " has been removed!\n";
else cout << "Remove failed!\n";
}
cout << "\nAfter removed\n" << list << endl;
cout << "----------------------------------------\n";
cout << "3. Test the Search(T &elem):\n";
while (1){
cout << "Input the element you want to search: ";
cin >> elem;
if (!cin){
cin.clear();
cin.ignore(100,'\n');
break;
}
if (elem < 0) break;
CircLinkNode<int> *p = list.Search(elem);
if (p) cout << "The element " << elem << " is in the list.\n";
else cout << "The element is not exist!\n";
}
cout << "\n----------------------------------------\n";
cout << "End test!" << endl;
return 0;
}
4、双向循环链表
#include <iostream>
using namespace std;
template<typename Type>
struct DoubleLinkNode
{
Type data;
DoubleLinkNode<Type>* lLink;
DoubleLinkNode<Type>* rLink;
DoubleLinkNode(DoubleLinkNode<Type>* pre=NULL,DoubleLinkNode<Type>*suc=NULL)
{
lLink=pre;
rLink=suc;
}
DoubleLinkNode(const Type& elem, DoubleLinkNode<Type>* pre=NULL,DoubleLinkNode<Type>* suc=NULL)
{
data=elem;
lLink=pre;
rLink=suc;
}
};
template<typename Type>
class CircleDoubleLinkedList
{
private:
DoubleLinkNode<Type>* first;
public:
CircleDoubleLinkedList()
{
first = new DoubleLinkNode<Type>();
first->rLink = first;
first->lLink = first;
}
void MakeEmpty()
{
DoubleLinkNode<Type>* p = first->rLink;
DoubleLinkNode<Type>* q = p;
while(p->rLink!=first)
{
q = p->rLink;
delete p;
p = q;
}
}
~CircleDoubleLinkedList()
{
this->MakeEmpty();
delete first;
}
int Length()
{
int num=0;
DoubleLinkNode<Type>* p = first->rLink;
while(p!=first)
{
++num;
p=p->rLink;
}
return num;
}
void InputRear(const Type& elem)
{
Insert(Length()+1,elem,1);
}
void CopyList(const CircleDoubleLinkedList<Type>& lst)
{
this->MakeEmpty();
DoubleLinkNode<Type>* p = lst->first->rLink;
DoubleLinkNode<Type>* q = first->rLink;
while(p!=first)
{
q = new DoubleLinkNode<Type>(p->data);
p=p->rLink;
q=q->rLink;
}
}
CircleDoubleLinkedList(const CircleDoubleLinkedList<Type>& lst)
{
DoubleLinkNode<Type>* p = lst->first->rLink;
DoubleLinkNode<Type>* q = first->rLink;
while(p!=first)
{
DoubleLinkNode<Type>* newNode = new DoubleLinkNode<Type>(p->data,q->lLink,q->rLink);
p=p->rLink;
q = newNode;
q=q->rLink;
}
}
CircleDoubleLinkedList<Type>& operator=(const CircleDoubleLinkedList<Type>& lst)
{
CopyList(lst);
return *this;
}
DoubleLinkNode<Type>* Locate(int loc, int sign)
{
if(loc<1||loc>Length()+1)
exit(1);
DoubleLinkNode<Type>* p = first;
int num=0;
if(sign)
{
p=p->rLink;
while(p!=first)
{
++num;
if(loc==num)
return p;
p=p->rLink;
}
}else
{
p=p->lLink;
while(p!=first)
{
++num;
if(loc==num)
return p;
p=p->lLink;
}
}
return p;
}
bool Insert(int loc, const Type& elem,int sign)
{
if(loc<1||loc>Length()+1)
return false;
DoubleLinkNode<Type>* p = Locate(loc,sign);
DoubleLinkNode<Type>* newNode = new DoubleLinkNode<Type>(elem);
newNode->lLink = p->lLink;
p->lLink->rLink = newNode;
newNode->rLink = p;
p->lLink = newNode;
return true;
}
bool Remove(int loc, Type& elem, int sign)
{
DoubleLinkNode<Type>* p = Locate(loc,sign);
p->lLink->rLink=p->rLink;
p->rLink->lLink = p->lLink;
delete p;
return true;
}
DoubleLinkNode<Type>* Search(const Type& elem, int sign) const
{
DoubleLinkNode<Type>* p = first;
if(sign)
{
p=p->rLink;
while(p!=first)
{
if(p->data ==elem)
return p;
p=p->rLink;
}
}
else
{
p=p->lLink;
while(p!=first)
{
if(p->data ==elem)
return p;
p=p->lLink;
}
}
}
bool GetData(int loc, Type& elem, int sign) const
{
if(loc<1)
return false;
DoubleLinkNode<Type>* p = Locate(loc,sign);
elem=p->data;
return true;
}
bool SetData(int loc, const Type& elem, int sign)
{
if(loc<1)
return false;
DoubleLinkNode<Type>* p = Locate(loc,sign);
p->data=elem;
return true;
}
void OutPut(int sign=0)
{
DoubleLinkNode<Type>* p = first;
if(sign)
{
p=p->rLink;
while(p!=first)
{
cout<<p->data<<" ";
p=p->rLink;
}
cout<<endl;
}
else{
p=p->lLink;
while(p!=first)
{
cout<<p->data<<" ";
p=p->lLink;
}
cout<<endl;
}
}
};
int main()
{
CircleDoubleLinkedList<int> lst;
for(int i=1;i<=10;i++)
{
lst.Insert(i,i,1);
}
lst.OutPut(0);
lst.OutPut(1);
if(lst.Search(5,0))
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
lst.SetData(10,100,0);
lst.OutPut();
int val;
for(int i=10;i>0;i--)
{
lst.Remove(i,val,1);
lst.OutPut(0);
}
return 0;
}
5、静态链表
#ifndef STATICList_H
#define STATICList_H
#include <cassert>
#include <iostream>
using namespace std;
const int defaultSize = 100;
template <typename T>struct SLinkNode{
T data;
int link;
};
template <typename T>class StaticList{
SLinkNode<T> *elem;
int maxSize;
int avil;//可用结点链链首下标
int *freeIndex;
public:
StaticList(int sz = defaultSize)
{
elem = new SLinkNode<T>[sz];
freeIndex = new int[sz];
for(int i=0;i<sz;i++)
{
elem[i].link = -1;
elem[i].data = 0;
freeIndex[i] = 1;
}
maxSize = sz;
avil = -1;
}
~StaticList()
{
delete[] elem;
delete[] freeIndex;
maxSize =0;
avil = -1;
}
int Length()//返回表长
{
int pre = avil;
int num=0;
while(pre!=-1)
{
++num;
pre=elem[pre].link;
}
return num;
}
int Search(T x)//搜索x
{
int pre = avil;
while(pre!=-1)
{
if(elem[pre].data==x)
return pre+1;
pre=elem[pre].link;
}
return -1;
}
int Locate(int i)//定位第i个元素
{
if(i<1||i>Length())
return -1;
int pre = avil;
int num=0;
while(pre!=-1)
{
++num;
if(num==i)
return pre;
pre = elem[pre].link;
}
return -1;
}
bool getelem(int i, T &x)//获取第i个元素的值
{
if(i<1||i>Length())
return false;
int num=0;
int pre = avil;
while(pre!=-1)
{
++num;
if(num==i)
{
x = elem[pre].data;
return true;
}
pre=elem[pre].link;
}
return false;
}
bool Append(T x) //在表尾添加新结点
{
if(maxSize==Length())
return false;
int pre=avil;
if(avil!=-1)
{
while(elem[pre].link!=-1)
{
pre=elem[pre].link;
}
int i=0;
for(;i<maxSize;i++)
if(freeIndex[i]==1)
{
freeIndex[i] = 0;
break;
}
elem[pre].link=i;
elem[i].data = x;
}else
{
elem[0].data=x;
avil=0;
freeIndex[0]=0;
}
return true;
}
bool Insert(int i, T x) //在第i个位置上插入元素x
{
int j=0;
for(;j<maxSize;j++)
if(freeIndex[j]==1)
{
freeIndex[j]=0;
break;
}
int ind1 = Locate(i);
int ind2 = Locate(i-1);
if(i==1)
{
elem[j].link=ind1;
elem[j].data=x;
avil=j;
}
else{
elem[j].link=ind1;
elem[ind2].link=j;
elem[j].data=x;
}
}
bool Remove(int i)//删除第i个位置上元素
{
if(IsEmpty())
return false;
int ind1 = Locate(i);
int ind2 = Locate(i-1);
elem[ind1].data=0;
elem[ind2].link=elem[ind1].link;
elem[ind1].link = -1;
freeIndex[ind1]=1;
return true;
}
bool IsEmpty()//判断表是否为空
{
return avil==-1;
}
void output(int type = 0)//按指定格式输出
{
int pre = avil;
if(type==0)
{
while(pre!=-1)
{
cout<<elem[pre].data<<" ";
pre=elem[pre].link;
}
cout<<endl;
}
else
{
for(int i=0;i<maxSize;i++)
{
if(elem[i].data>0)
{
cout<<elem[i].data<<" ";
}
}
cout<<endl;
}
}
friend istream& operator >> (istream& in, StaticList<T> &stl){
T elem;
while (!in.eof()){//在原链表后添加,与其他线性表不同
in >> elem;
stl.Append(elem);
}
return in;
}
friend ostream & operator<<(ostream &out, StaticList <T> &stl){
int p = stl.elem[0].link;//elem[0]为附加头结点
while(p != -1){
cout << stl.elem[p].elem << endl;
p = stl.elem[p].link;
}
cout << endl;
return out;
}
};
#endif
int main(){
StaticList<int> List(10);
List.Append(25);
List.Append(92);
List.Append(57);
List.Append(36);
List.Append(78);
List.Insert(3, 11);
List.Insert(1, 49);
// Print the List
List.output();
List.output(1);
cout << "search 36: " << List.Search(36) << endl;
cout << "search 25: " << List.Search(25) << endl;
cout << "search 78: " << List.Search(78) << endl;
cout << "search 50: " << List.Search(50) << endl;
cout << endl;
if (List.Remove(5)){
cout << "Remove the 5th elem:\n";
List.output();
List.output(1);
}
List.Append(11);
cout << "After Insert 11 in the rear:\n";
List.output();
List.output(1);
return 0;
}
6、循环链表
#include <iostream>
#include <cassert>
#include <fstream>
using namespace std;
enum InsMod {INF, INR};//头插还是尾插
template <typename T>class CircLinkNode{
public:
T data;
CircLinkNode<T> *link;
CircLinkNode(CircLinkNode<T> *ptr = NULL){//建立空结点
link = ptr;
}
CircLinkNode(const T &item, CircLinkNode<T> *ptr = NULL){//建立非空结点
data = item;
link = ptr;
}
};
template <typename T>class CircList{
public:
CircList()
{
this->first = new CircLinkNode<T>();
first->link=first;
last=first;
}
CircList(CircList<T> &L)//复制一个环链表
{
CircLinkNode<T>* p = L.first->link;
CircLinkNode<T>*q = this->first->link;
while(p!=first)
{
q= new CircLinkNode<T>(p->data);
q=q->link;
p=p->link;
}
last = q;
}
~CircList()
{
this->makeEmpty();
delete this->first;
}
void makeEmpty()
{
CircLinkNode<T>*p = this->first->link;
CircLinkNode<T>*q = p->link;
while(p!=first)
{
delete p;
p = q;
q=q->link;
}
}
int Length()const
{
int num =0;
CircLinkNode<T>* p = first->link;
while(p!=first)
{
++num;
p=p->link;
}
return num;
}
CircLinkNode<T> *Search(T x)
{
CircLinkNode<T>* p = first->link;
while(p->data!=x)
{
p=p->link;
if(p==first)
exit(1);
}
return p;
}
CircLinkNode<T> *Locate(int i)
{
if(i<1||i>Length())
{
exit(1);
}
int num=0;
CircLinkNode<T>*p = first->link;
while(p!=first)
{
++num;
if(num==i)
return p;
p=p->link;
}
return p;
}
bool getData(int i,T&x)const
{
if(i<1||i>Length())
{
return false;
}
int num=0;
CircLinkNode<T>*p = first->link;
while(p!=first)
{
++num;
if(num==i)
x = p->data;
p=p->link;
}
return true;
}
void setData(int i, T &x)
{
if(i<1||i>Length())
{
return;
}
int num=0;
CircLinkNode<T>*p = first->link;
while(p!=first)
{
++num;
if(num==i)
p->data=x;
p=p->link;
}
}
bool Insert(int i, T &x)
{
if(i<1||i>Length()+1)
{
return false;
}
CircLinkNode<T>* p =NULL;
if(i==1)
p = first;
else
p = Locate(i-1);
CircLinkNode<T>* q = p->link;
CircLinkNode<T> *newNode = new CircLinkNode<T>(x);
p->link = newNode;
newNode->link = q;
if(i==Length()+1)
{
last = newNode;
}
return true;
}
bool Remove(int i, T &x)
{
if(i<=0||i>Length())
{
return false;
}
int j=0;
CircLinkNode<T>* p = first;
for(j=1; j<i; j++)
{
p=p->link;
}
CircLinkNode<T> *q = p->link->link;
x = p->link->data;
if(i==Length())
last = p;
delete p->link;
p->link = q;
return true;
}
bool IsEmpty()const
{
return first->link==last;
}
bool IsFull()const
{
return (new CircLinkNode<T>() )==NULL;
}
void Sort()
{
CircLinkNode<T> * p =first->link;
CircLinkNode<T> *q=NULL;
if(NULL==p)
return ;
int num = this->Length();
int maxIndex=1;
int Max =p->data;
for(int i=1; i<num; i++)
{
int j=1;
for(; j<num-i+2; j++)
{
if(Max<=p->data)
{
maxIndex = j;
Max = p->data;
}
q=p;
p=p->link;
}
setData(maxIndex,q->data);
setData(num,Max);
}
}
void Inverse()//不要返回
{
int num = this->Length();
for(int i=1; i<num/2+1; i++)
{
int j=num-i+1;
CircLinkNode<T> * p = Locate(i);
CircLinkNode<T>* q = Locate(j);
T temp = p->data;
p->data = q->data;
q->data = temp;
}
}
void input(T endTag, InsMod im = INR)
{
if(im==INR)
{
Insert(Length()+1,endTag);
}
else{
Insert(1,endTag);
}
}
void output()
{
CircLinkNode<T>* p =first->link;
while(p!=first)
{
cout<<p->data<<" ";
p=p->link;
}
cout<<endl;
}
CircList<T> &operator = (CircList<T> &L)
{
this->makeEmpty();
CircLinkNode<T>* q = L.first->link;
while(q!=L.first)
{
input(q->data,INR);
q=q->link;
}
}
friend ostream& operator << (ostream &out, CircList<T> &L){
CircLinkNode<T> *current = L.first->link;
while (current != L.first){
out << current->data <<'\t';
current = current->link;
}
out<<endl;
return out;
}
friend istream& operator >> (istream &in, CircList<T> &L){//重新输入数据,向后生成
T val;
L.makeEmpty();//先清空
while (!in.eof()){
in >> val;
L.last->link = new CircLinkNode<T>(val);
assert(L.last->link);
L.last = L.last->link;
}
L.last->link = L.first;
in.clear();//当以ctrl_Z结束,流关闭,必须重新打开
return in;
}
protected:
CircLinkNode<T> *first, *last;
void inputFront(T endTag);
void inputRear(T endTag);
};
int main(){
CircList<int> list;
ifstream fin("D:\\cb代码\\Data&Structure\\list.txt");
//assert(fin);
fin >> list;
cout << "The initial list in the file is:\n" << list << endl;
list.Inverse();
cout << "Inverse the list, then the list is:\n" << list << endl;
cout << "========================================\n";
int i, elem;
cout << "Test the Insert, Remove and Search function:\n";
cout << "Each test will terminate by an invaid input.";
cout << "\n----------------------------------------\n";
cout << "1. Test the Insert(int i, T &elem):\n";
while (1){
cout << "Input the index i and data elem to insert: ";
cin >> i >> elem;
if (!cin){//流不正常
cin.clear();//恢复正常
cin.ignore(100,'\n');
break;
}
if (i < 0) break;
if (list.Insert(i, elem)) cout << "Insert successful!\n";
else cout << "Insert failed!\n";
}
cout << "\nAfter inserted\n" << list << endl;
cout << "----------------------------------------\n";
cout << "2. Test the Remove(int i, T &elem):\n";
while (1){
cout << "Input the index i in which you want to remove: ";
cin >> i;
if (!cin){
cin.clear();
cin.ignore(100,'\n');
break;
}
if (i < 0) break;
if (list.Remove(i, elem)) cout << "The element " << elem << " has been removed!\n";
else cout << "Remove failed!\n";
}
cout << "\nAfter removed\n" << list << endl;
cout << "----------------------------------------\n";
cout << "3. Test the Search(T &elem):\n";
while (1){
cout << "Input the element you want to search: ";
cin >> elem;
if (!cin){
cin.clear();
cin.ignore(100,'\n');
break;
}
if (elem < 0) break;
CircLinkNode<int> *p = list.Search(elem);
if (p) cout << "The element " << elem << " is in the list.\n";
else cout << "The element is not exist!\n";
}
cout << "\n----------------------------------------\n";
cout << "End test!" << endl;
return 0;
}
7、双向循环链表
#include <iostream>
using namespace std;
template<typename Type>
struct DoubleLinkNode
{
Type data;
DoubleLinkNode<Type>* lLink;
DoubleLinkNode<Type>* rLink;
DoubleLinkNode(DoubleLinkNode<Type>* pre=NULL,DoubleLinkNode<Type>*suc=NULL)
{
lLink=pre;
rLink=suc;
}
DoubleLinkNode(const Type& elem, DoubleLinkNode<Type>* pre=NULL,DoubleLinkNode<Type>* suc=NULL)
{
data=elem;
lLink=pre;
rLink=suc;
}
};
template<typename Type>
class CircleDoubleLinkedList
{
private:
DoubleLinkNode<Type>* first;
public:
CircleDoubleLinkedList()
{
first = new DoubleLinkNode<Type>();
first->rLink = first;
first->lLink = first;
}
void MakeEmpty()
{
DoubleLinkNode<Type>* p = first->rLink;
DoubleLinkNode<Type>* q = p;
while(p->rLink!=first)
{
q = p->rLink;
delete p;
p = q;
}
}
~CircleDoubleLinkedList()
{
this->MakeEmpty();
delete first;
}
int Length()
{
int num=0;
DoubleLinkNode<Type>* p = first->rLink;
while(p!=first)
{
++num;
p=p->rLink;
}
return num;
}
void InputRear(const Type& elem)
{
Insert(Length()+1,elem,1);
}
void CopyList(const CircleDoubleLinkedList<Type>& lst)
{
this->MakeEmpty();
DoubleLinkNode<Type>* p = lst->first->rLink;
DoubleLinkNode<Type>* q = first->rLink;
while(p!=first)
{
q = new DoubleLinkNode<Type>(p->data);
p=p->rLink;
q=q->rLink;
}
}
CircleDoubleLinkedList(const CircleDoubleLinkedList<Type>& lst)
{
DoubleLinkNode<Type>* p = lst->first->rLink;
DoubleLinkNode<Type>* q = first->rLink;
while(p!=first)
{
DoubleLinkNode<Type>* newNode = new DoubleLinkNode<Type>(p->data,q->lLink,q->rLink);
p=p->rLink;
q = newNode;
q=q->rLink;
}
}
CircleDoubleLinkedList<Type>& operator=(const CircleDoubleLinkedList<Type>& lst)
{
CopyList(lst);
return *this;
}
DoubleLinkNode<Type>* Locate(int loc, int sign)
{
if(loc<1||loc>Length()+1)
exit(1);
DoubleLinkNode<Type>* p = first;
int num=0;
if(sign)
{
p=p->rLink;
while(p!=first)
{
++num;
if(loc==num)
return p;
p=p->rLink;
}
}else
{
p=p->lLink;
while(p!=first)
{
++num;
if(loc==num)
return p;
p=p->lLink;
}
}
return p;
}
bool Insert(int loc, const Type& elem,int sign)
{
if(loc<1||loc>Length()+1)
return false;
DoubleLinkNode<Type>* p = Locate(loc,sign);
DoubleLinkNode<Type>* newNode = new DoubleLinkNode<Type>(elem);
newNode->lLink = p->lLink;
p->lLink->rLink = newNode;
newNode->rLink = p;
p->lLink = newNode;
return true;
}
bool Remove(int loc, Type& elem, int sign)
{
DoubleLinkNode<Type>* p = Locate(loc,sign);
p->lLink->rLink=p->rLink;
p->rLink->lLink = p->lLink;
delete p;
return true;
}
DoubleLinkNode<Type>* Search(const Type& elem, int sign) const
{
DoubleLinkNode<Type>* p = first;
if(sign)
{
p=p->rLink;
while(p!=first)
{
if(p->data ==elem)
return p;
p=p->rLink;
}
}
else
{
p=p->lLink;
while(p!=first)
{
if(p->data ==elem)
return p;
p=p->lLink;
}
}
}
bool GetData(int loc, Type& elem, int sign) const
{
if(loc<1)
return false;
DoubleLinkNode<Type>* p = Locate(loc,sign);
elem=p->data;
return true;
}
bool SetData(int loc, const Type& elem, int sign)
{
if(loc<1)
return false;
DoubleLinkNode<Type>* p = Locate(loc,sign);
p->data=elem;
return true;
}
void OutPut(int sign=0)
{
DoubleLinkNode<Type>* p = first;
if(sign)
{
p=p->rLink;
while(p!=first)
{
cout<<p->data<<" ";
p=p->rLink;
}
cout<<endl;
}
else{
p=p->lLink;
while(p!=first)
{
cout<<p->data<<" ";
p=p->lLink;
}
cout<<endl;
}
}
};
int main()
{
CircleDoubleLinkedList<int> lst;
for(int i=1;i<=10;i++)
{
lst.Insert(i,i,1);
}
lst.OutPut(0);
lst.OutPut(1);
if(lst.Search(5,0))
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
lst.SetData(10,100,0);
lst.OutPut();
int val;
for(int i=10;i>0;i--)
{
lst.Remove(i,val,1);
lst.OutPut(0);
}
return 0;
}