链表仅实现基本功能(插入、删除、查找、遍历、翻转)
顺序表、栈、队列仅实现基本功能(插入/入栈/入队、删除/出栈/出队)
本文提供的代码不是最优写法,欢迎批评指正。
//顺序表 #初步验证ok
#include <iostream>
using namespace std;
template <class ElemType>
class SeqList{
private:
ElemType* list;
int len;
int maxlen;
public:
SeqList(int m) //InitList
{ list=new ElemType[m];len=0;maxlen=m;}
~SeqList() //DestroyList
{ delete []list;}
bool ListEmpty()
{ return len==0;}
void ClearList()
{ len=0;}
int ListLength()
{ return len;}
bool visit(ElemType &e);
bool GetElem(int pos,ElemType &e);
bool SetElem(int pos,ElemType e);
bool FindElem(int &pos,ElemType e);
bool PriorElem(ElemType cur_e,ElemType &pre_e);
bool NextElem(ElemType cur_e,ElemType &suc_e);
bool Insert(int pos, ElemType e);
bool Delete(int pos,ElemType &e);
bool Traverse(bool (*visit)(ElemType &e));
};
template <class ElemType>
bool SeqList<ElemType>::visit(ElemType &e){
cout<<e<<" ";
return 1;
}
template <class ElemType>
bool SeqList<ElemType>::GetElem(int pos,ElemType &e){
if(pos<0||pos>=len) return 0;
e=list[pos];
return 1;
}
template <class ElemType>
bool SeqList<ElemType>::SetElem(int pos,ElemType e){
if(pos<0||pos>=len) return 0;
list[pos]=e;
return 1;
}
//查找值为e的首个数据元素
template <class ElemType>
bool SeqList<ElemType>::FindElem(int &pos,ElemType e){
pos=0;
while(pos<len && list[pos]!=e) pos++;
return pos<len; //found or not
}
template <class ElemType>
bool SeqList<ElemType>::PriorElem(ElemType cur_e,ElemType &pre_e){
int pos=0;
if(FindElem(pos,cur_e)){
if(pos){
pre_e=list[pos-1];
return 1;
}
}
return 0;
}
template <class ElemType>
bool SeqList<ElemType>::NextElem(ElemType cur_e,ElemType &suc_e){
int pos=0;
if(FindElem(pos,cur_e)){
if(pos<len-1){
suc_e=list[pos+1];
return 1;
}
}
return 0;
}
template <class ElemType>
bool SeqList<ElemType>::Insert(int pos,ElemType e){
if(pos<0||pos>=maxlen) return 0;
if(len==maxlen) return 0;
for(int i=len;i>pos;i--)
list[i]=list[i-1];
list[pos]=e;
len++;
return 1;
}
template <class ElemType>
bool SeqList<ElemType>::Delete(int pos,ElemType &e){
if(pos<0||pos>=len) return 0;
e=list[pos];
len--;
for(int i=pos;i<len;i++)
list[i]=list[i+1];
return 1;
}
template <class ElemType>
bool SeqList<ElemType>::Traverse(bool (*visit)(ElemType &e)){
for(int i=0;i<len;i++)
visit(list[i]);
return i==len;
}
//单向链表,不带表头(head结点存储数据)#测试ok
#include <iostream>
using namespace std;
template <class ElemType>
class LinkList;
template <class ElemType>
class Node{
public:
ElemType data;
Node<ElemType>* next;
friend class Linklist;
};
template <class ElemType>
class LinkList{
private:
Node<ElemType>* head;
Node<ElemType>* rear;
int len;
public:
LinkList()
{ head=NULL;rear=NULL;len=0;}
~LinkList()
{ ClearList();}
bool ListEmpty()
{ return len==0;}
void ClearList();
int ListLength();
bool GetElem(int pos,ElemType &e);
bool SetElem(int pos,ElemType e);
bool FindElem(int &pos,ElemType e);
bool PriorElem(ElemType cur_e,ElemType &pre_e);
bool NextElem(ElemType cur_e,ElemType &suc_e);
bool Insert(int pos,ElemType e);
bool Delete(int pos,ElemType &e);
void Traverse();
void Reverse();
};
template <class ElemType>
void LinkList<ElemType>::ClearList(){
Node<ElemType> *current,*del;
current=head;
while(current!=NULL){
del=current;
current=current->next;
delete del;
del=NULL;
}
head=rear=NULL;
len=0;
}
template <class ElemType>
int LinkList<ElemType>::ListLength(){
return len;
}
template <class ElemType>
bool LinkList<ElemType>::GetElem(int pos,ElemType &e){
if(pos<0||pos>=len) return 0;
Node<ElemType>* temp;
temp=head;
for(int i=0;i<pos;i++)
temp=temp->next;
e=temp->data;
return 1;
}
template <class ElemType>
bool LinkList<ElemType>::SetElem(int pos,ElemType e){
if(pos<0||pos>=len) return 0;
Node<ElemType>* temp;
temp=head;
for(int i=0;i<pos;i++)
temp=temp->next;
temp->data=e;
return 1;
}
//查找值为e的首个数据元素
template <class ElemType>
bool LinkList<ElemType>::FindElem(int &pos,ElemType e){
Node<ElemType>* temp;
temp=head;
pos=-1;
while(temp!=rear->next){
pos++;
if(temp->data==e) return 1;
temp=temp->next;
}
return 0;
}
template <class ElemType>
bool LinkList<ElemType>::PriorElem(ElemType cur_e,ElemType &pre_e){
Node<ElemType>* temp;
temp=head;
while(temp!=rear){
if(temp->next->data==cur_e){
pre_e=temp->data;
return 1;
}
temp=temp->next;
}
return 0;
}
template <class ElemType>
bool LinkList<ElemType>::NextElem(ElemType cur_e,ElemType &suc_e){
Node<ElemType>* temp;
temp=head;
while(temp!=rear){
if(temp->data==cur_e){
suc_e=temp->next->data;
return 1;
}
temp=temp->next;
}
return 0;
}
template <class ElemType>
bool LinkList<ElemType>::Insert(int pos,ElemType e){
if(pos<0||pos>len) return 0;
Node<ElemType>* insertnode=new Node<ElemType>[1];
insertnode->data=e;
insertnode->next=NULL;
if(pos==0){ //head
insertnode->next=head;
head=insertnode;
len++;
return 1;
}
Node<ElemType> *temp;
temp=head;
for(int cur_pos=0;cur_pos<pos-1;cur_pos++)
temp=temp->next;
insertnode->next=temp->next;
temp->next=insertnode;
len++;
if(pos==len-1) rear=insertnode;
return 1;
}
template <class ElemType>
bool LinkList<ElemType>::Delete(int pos,ElemType &e){
if(pos<0||pos>=len) return 0;
Node<ElemType> *del;
if(pos==0){ //head
e=head->data;
del=head;
head=head->next;
delete del;
del=NULL;
len--;
return 1;
}
Node<ElemType> *pre_del;
pre_del=head;
for(int cur=0;cur<pos-1;cur++)
pre_del=pre_del->next;
del=pre_del->next;
e=del->data;
pre_del->next=del->next;
delete del;
del=NULL;
len--;
if(pos==len) rear=pre_del;
return 1;
}
template <class ElemType>
void LinkList<ElemType>::Traverse(){
Node<ElemType>* current;
current=head;
while(current!=NULL){
cout<<current->data<<" ";
current=current->next;
}
}
template <class ElemType>
void LinkList<ElemType>::Reverse(){
Node<ElemType> *pre,*cur,*suc;
pre=head;
cur=pre->next;
suc=cur->next;
for(int i=1;i<=len-2;i++){ //between head and rear
cur->next=pre;
pre=cur;
cur=suc;
suc=suc->next;
}
cur->next=pre;
Node<ElemType> *temp=head;
head=cur;
rear=temp;
rear->next=NULL;
}
//双向链表(不带表头)#ok
#include <iostream>
using namespace std;
template <class ElemType>
class DlkList;
template <class ElemType>
class DNode{
public:
ElemType data;
DNode<ElemType> *next,*pre;
friend class DlkList<ElemType>;
};
template <class ElemType>
class DlkList{
private:
DNode<ElemType> *head;
int len;
public:
DlkList()
{ head=NULL;len=0;}
~DlkList()
{ ClearList();}
bool ListEmpty()
{ return len==0;}
void ClearList();
int ListLength();
bool GetElem(int pos,ElemType &e);
bool SetElem(int pos,ElemType e);
bool FindElem(int &pos,ElemType e);
bool PriorElem(ElemType cur_e,ElemType &pre_e);
bool NextElem(ElemType cur_e,ElemType &suc_e);
bool Insert(int pos,ElemType e);
bool Delete(int pos,ElemType &e);
void Traverse();
void Reverse();
};
template <class ElemType>
void DlkList<ElemType>::ClearList(){
DNode<ElemType> *cur,*del;
cur=head;
for(int pos=0;pos<len;pos++){
del=cur;
cur=cur->next;
delete del;
del=NULL;
}
head=NULL;
len=0;
}
template <class ElemType>
int DlkList<ElemType>::ListLength(){
return len;
}
template <class ElemType>
bool DlkList<ElemType>::GetElem(int pos,ElemType &e){
if(pos<0||pos>=len) return 0;
DNode<ElemType> *cur;
cur=head;
for(int i=0;i<pos;i++)
cur=cur->next;
e=cur->data;
return 1;
}
template <class ElemType>
bool DlkList<ElemType>::SetElem(int pos,ElemType e){
if(pos<0||pos>=len) return 0;
DNode<ElemType> *cur;
cur=head;
for(int i=0;i<pos;i++)
cur=cur->next;
cur->data=e;
return 1;
}
template <class ElemType>
bool DlkList<ElemType>::FindElem(int &pos,ElemType e){
DNode<ElemType> *cur;
cur=head;
for(pos=0;pos<len;pos++){
if(cur->data==e) return 1;
cur=cur->next;
}
return 0;
}
template <class ElemType>
bool DlkList<ElemType>::PriorElem(ElemType cur_e,ElemType &pre_e){
int pos=0;
if(FindElem(pos,cur_e)){
DNode<ElemType> *cur;
cur=head;
for(int i=0;i<pos;i++)
cur=cur->next;
pre_e=cur->pre->data;
return 1;
}
return 0;
}
template <class ElemType>
bool DlkList<ElemType>::NextElem(ElemType cur_e,ElemType &suc_e){
int pos=0;
if(FindElem(pos,cur_e)){
DNode<ElemType> *cur;
cur=head;
for(int i=0;i<pos;i++)
cur=cur->next;
suc_e=cur->next->data;
return 1;
}
return 0;
}
template <class ElemType>
bool DlkList<ElemType>::Insert(int pos,ElemType e){
if(pos<0||pos>len) return 0;
DNode<ElemType> *insertnode=new DNode<ElemType>[1];
insertnode->data=e;
insertnode->next=NULL;
insertnode->pre=NULL;
if(pos==0){ //自己指向自己的单结点双向链表
insertnode->next=insertnode;
insertnode->pre=insertnode;
head=insertnode;
len++;
return 1;
}
DNode<ElemType> *cur;
cur=head;
for(int i=0;i<pos;i++)
cur=cur->next;
insertnode->pre=cur->pre;
cur->pre->next=insertnode;
insertnode->next=cur;
cur->pre=insertnode;
len++;
return 1;
}
template <class ElemType>
bool DlkList<ElemType>::Delete(int pos,ElemType &e){
if(pos<0||pos>=len) return 0;
if(len==0) return 0;
DNode<ElemType> *del;
del=head;
for(int i=0;i<pos;i++)
del=del->next;
del->pre->next=del->next;
del->next->pre=del->pre;
e=del->data;
if(len==1) head=NULL;
if(del==head) head=del->next;
delete del;
del=NULL;
len--;
return 1;
}
template <class ElemType>
void DlkList<ElemType>::Traverse(){
int pos=0;
DNode<ElemType> *cur;
cur=head;
while(pos<=len-1){
cout<<cur->data<<" ";
pos++;
cur=cur->next;
}
}
template <class ElemType>
void DlkList<ElemType>::Reverse(){
if(len==0) return;
DNode<ElemType> *cur,*temp;
cur=head;
for(int pos=0;pos<len;pos++){
temp=cur->pre;
cur->pre=cur->next;
cur->next=temp;
cur=cur->pre;
}
head=cur->next;
return;
}
//顺序栈 #ok
#include <iostream>
using namespace std;
template <class ElemType>
class SeqStack{
private:
ElemType *stack;
int MaxSize;
int top;
public:
SeqStack(int size)
{ stack=new ElemType[size];MaxSize=size;top=-1;}
~SeqStack()
{ delete []stack;}
void ClearStack()
{ top=-1;}
bool StackEmpty()
{ return top==-1;}
bool StackFull()
{ return top==(MaxSize-1);}
int StackLength()
{ return top+1;}
bool Push(ElemType e);
bool Pop(ElemType &e);
bool GetTop(ElemType &e);
};
template <class ElemType>
bool SeqStack<ElemType>::Push(ElemType e){
if(StackFull()) return 0;
stack[++top]=e;
return 1;
}
template <class ElemType>
bool SeqStack<ElemType>::Pop(ElemType &e){
if(StackEmpty()) return 0;
e=stack[top--];
return 1;
}
template <class ElemType>
bool SeqStack<ElemType>::GetTop(ElemType &e){
if(StackEmpty()) return 0;
e=stack[top];
return 1;
}
//链式栈#ok
#include <iostream>
using namespace std;
template <class ElemType>
class Node{
public:
ElemType data;
Node<ElemType> *next;
};
template <class ElemType>
class LinkStack{
private:
Node<ElemType> *top;
int len;
public:
LinkStack()
{ top=NULL;len=0;}
~LinkStack()
{ ClearStack();}
void ClearStack();
bool StackEmpty()
{ return len==0;}
int StackLength()
{ return len;}
bool Push(ElemType e);
bool Pop(ElemType &e);
bool GetTop(ElemType &e);
};
template <class ElemType>
void LinkStack<ElemType>::ClearStack(){
ElemType temp;
while(Pop(temp));
}
template <class ElemType>
bool LinkStack<ElemType>::Push(ElemType e){
Node<ElemType> *insert=new Node<ElemType>[1];
insert->next=NULL;
insert->data=e;
if(len==0){
top=insert;
len++;
return 1;
}
insert->next=top;
top=insert;
len++;
return 1;
}
template <class ElemType>
bool LinkStack<ElemType>::Pop(ElemType &e){
if(len==0) return 0;
e=top->data;
Node<ElemType> *pop;
pop=top;
top=top->next;
delete pop;
pop=NULL;
len--;
return 1;
}
template <class ElemType>
bool LinkStack<ElemType>::GetTop(ElemType &e){
if(len==0) return 0;
e=top->data;
return 1;
}
//顺序队列(存在假溢出问题)#ok
#include <iostream>
using namespace std;
template <class ElemType>
class SeqQueue{
private:
int MaxSize;
int front,rear;
ElemType *queue;
public:
SeqQueue(int size)
{ MaxSize=size;front=rear=0;queue=new ElemType[size];}
~SeqQueue()
{ ClearQueue();}
bool QueueEmpty()
{ return front==rear;}
bool QueueFull()
{ return rear==(MaxSize-1);}
int QueueLength()
{ return rear-front;}
bool GetFront(ElemType &e);
void ClearQueue();
bool EnQueue(ElemType e);
bool DeQueue(ElemType &e);
void PrintQueue();
};
template <class ElemType>
bool SeqQueue<ElemType>::GetFront(ElemType &e){
if(QueueEmpty()) return 0;
e=queue[front+1];
return 1;
}
template <class ElemType>
void SeqQueue<ElemType>::ClearQueue(){
ElemType temp;
while(!QueueEmpty()) DeQueue(temp);
}
template <class ElemType>
bool SeqQueue<ElemType>::EnQueue(ElemType e){
if(QueueFull()) return 0;
queue[++rear]=e;
return 1;
}
template <class ElemType>
bool SeqQueue<ElemType>::DeQueue(ElemType &e){
if(QueueEmpty()) return 0;
e=queue[++front];
return 1;
}
template <class ElemType>
void SeqQueue<ElemType>::PrintQueue(){
int temp=front;
while(temp!=rear)
cout<<queue[++temp]<<" ";
}
//循环队列(解决顺序队列假溢出问题)#ok
#include <iostream>
using namespace std;
template <class ElemType>
class CirQueue{
private:
int front,rear,len,MaxSize;
ElemType *queue;
public:
CirQueue(int size)
{ front=rear=len=0;MaxSize=size;queue=new ElemType[size];}
~CirQueue()
{ ClearQueue();}
bool QueueEmpty()
{ return len==0;}
bool QueueFull()
{ return len==MaxSize;}
int QueueLength()
{ return len;}
void ClearQueue();
bool EnQueue(ElemType e);
bool DeQueue(ElemType &e);
void Print();
};
template <class ElemType>
void CirQueue<ElemType>::ClearQueue(){
ElemType temp;
while(!QueueEmpty()) DeQueue(temp);
len=0;
}
template <class ElemType>
bool CirQueue<ElemType>::EnQueue(ElemType e){
if(QueueFull()) return 0;
rear=(rear+1)%MaxSize;
queue[rear]=e;
len++;
return 1;
}
template <class ElemType>
bool CirQueue<ElemType>::DeQueue(ElemType &e){
if(QueueEmpty()) return 0;
front=(front+1)%MaxSize;
e=queue[front];
len--;
return 1;
}
template <class ElemType>
void CirQueue<ElemType>::Print(){
int temp=0,temp2=front;
while(temp<len){
temp2=(temp2+1)%MaxSize;
cout<<queue[temp2]<<" ";
temp++;
}
}
//链式队列#ok
#include <iostream>
using namespace std;
template <class ElemType>
class Node{
public:
ElemType data;
Node<ElemType> *next;
};
template <class ElemType>
class LinkQueue{
private:
Node<ElemType> *front,*rear;
int len;
public:
LinkQueue()
{ front=rear=NULL;len=0;}
~LinkQueue()
{ ClearQueue();}
bool QueueEmpty()
{ return len==0;}
int QueueLength()
{ return len;}
void ClearQueue();
bool EnQueue(ElemType e);
bool DeQueue(ElemType &e);
void Print();
};
template <class ElemType>
void LinkQueue<ElemType>::ClearQueue(){
ElemType del;
while(!QueueEmpty())
DeQueue(del);
}
template <class ElemType>
bool LinkQueue<ElemType>::EnQueue(ElemType e){
Node<ElemType> *insert=new Node<ElemType>[1];
insert->data=e;
insert->next=NULL;
if(QueueEmpty()){
front=rear=insert;
len++;
return 1;
}
rear->next=insert;
rear=insert;
len++;
return 1;
}
template <class ElemType>
bool LinkQueue<ElemType>::DeQueue(ElemType &e){
if(QueueEmpty()) return 0;
Node<ElemType> *del;
del=front;
e=del->data;
front=front->next;
delete del;
del=NULL;
len--;
return 1;
}
template <class ElemType>
void LinkQueue<ElemType>::Print(){
Node<ElemType> *cur;
cur=front;
for(int i=0;i<len;i++){
cout<<cur->data<<" ";
cur=cur->next;
}
}