ep7_9_1.h
上传用户:wxcui2006
上传日期:2022-07-12
资源大小:1274k
文件大小:2k
- #include<iostream>
- #include<cassert>
- using namespace std;
- template<typename T>class Queue;
- template<typename T>class Node{
- T info;
- Node<T> *link;
- public:
- Node(){link=NULL;}
- Node(T data,Node *l=NULL);
- T Getinfo(){return info;}; //获得结点数据
- Node<T>* Getlink(){return link;}; //获得结点的指针
- void Setinfo(T data){info=data}; //设置结点数据
- void Setlink(Node<T> * pNext){link=pNext;} //设置结点的指针
- };
- template<typename T> Node<T>::Node(T data,Node *l){
- info=data;
- link=l;
- };
- template<typename T>class list{
- Node<T> *front,*rear;
- public:
- list(){rear=front=NULL;} //构造一个空链
- ~list();
- bool IsEmpty(){ return front==NULL;} //链空否?
- void EnRear(const T &); //进链
- T DeFront(); //出链
- T GetFront(); //查看链头数据
- void MakeEmpty(); //置空链,与析构逻辑上不同,物理上一样
- void EnFront(const T &); //从链头入链
- };
- template<typename T>void list<T>::MakeEmpty(){
- Node<T> *temp;
- while(front!=NULL){
- temp=front;front=front->Getlink();delete temp;
- }
- }
- template<typename T>list<T>::~list(){MakeEmpty();}
- template<typename T>void list<T>::EnRear(const T &data){
- if(front==NULL) front=rear=new Node<T>(data,NULL);
- else {
- rear->Setlink(new Node<T>(data,NULL));
- rear=rear->Getlink();
- }
- }
- template<typename T>void list<T>::EnFront(const T &data){ //从链头入链
- Node<T> *p=new Node<T>(data,NULL);
- if(front==NULL) front=rear=p;
- else{
- p->Setlink(front);
- front=p;
- }
- }
- template<typename T>T list<T>::DeFront(){
- assert(!IsEmpty());
- Node<T> *temp=front;
- T data=temp->Getinfo();
- front=front->Getlink();
- delete temp;
- return data;
- }
- template<typename T>T list<T>::GetFront(){
- assert(!IsEmpty());
- return front->Getinfo();
- }