使用单链表实现链队列

来源:互联网 发布:贵阳软件人均工资 编辑:程序博客网 时间:2024/05/22 21:40

单链表类作为链队列的私有变量

VS2005可以运行通过,但是dev通不过,不知道为什么,请各位大牛指正。

#include <iostream>using namespace std;template<class Type>//定义结点struct Node{Type data;Node<Type> *next;};//定义链表template<class Type>class LinkList{protected:int len;//链表中结点个数Node<Type>* Head; //不使用头结点public:LinkList();//默认构造函数LinkList(const LinkList<Type>& otherList);//拷贝构造函数~LinkList();void createListForward();//头插法void createBackward();//尾插法void initList();//生成头结点,尾部设置为NULLbool isEmptyList();int  length();void destoryList();void getFirstData(Type& firstItem);void getData(int pos,Type& firstItem);void insertFirst(Type newItem);void insertLast(Type newItem);void deleteFirst(Type& data);void deleteLast();void reverse();const LinkList<Type>& operator=(const LinkList<Type>&otherList);friend ostream& operator<< <>(ostream& cout,const LinkList<Type>& list);};template<class Type>LinkList<Type>::LinkList() //初始化时,只有一个头结点,有head指向{Head = NULL;len =0;}template<class Type>LinkList<Type>::LinkList(const LinkList<Type>&otherList){Head = NULL;len = otherList.len;Node<Type>* current;//自己链表的尾部元素Node<Type>* otherListCurrent=otherList.Head;//otherListCurrent指向第一个元素while(otherListCurrent!=NULL)//拷贝的目标不为空{for (int i=1;i<=otherList.len;i++){Node<Type>* NewNode = new Node<Type>;NewNode->data = otherListCurrent->data;NewNode->next = NULL;if (i==1){Head = NewNode;current = Head;}else{current->next = NewNode;current = current->next;}otherListCurrent = otherListCurrent->next;}}}template<class Type>const LinkList<Type>& LinkList<Type>::operator=(const LinkList<Type>&otherList)//赋值函数{Node<Type>* current;//current总是指向要插的位置的前一结点Node<Type>* otherListCurrent=otherList.Head;//otherListCurrent指向第一个元素if (this!=&otherList)//避免自己给自己赋值{if (Head!=NULL){destoryList();//自己有结点,先销毁}if(otherListCurrent!=NULL){bool first = true;while(otherListCurrent!=NULL){Node<Type>* newNode = new Node<Type>;newNode->data = otherListCurrent->data;newNode->next = NULL;if (first){Head = newNode;current = Head;first = false;}else{current->next = newNode;current = current->next;}otherListCurrent = otherListCurrent->next;}}}return *this;//为了连续赋值}template<class Type>LinkList<Type>::~LinkList(){destoryList();}template<class Type>void LinkList<Type>::createListForward()//头插法{Node<Type>* newNode;cout<<"输入链表长度:"<<endl;cin>>len;for (int i=1;i<=len;i++){newNode = new Node<Type>;cout<<"输入元素:"<<endl;cin>>newNode->data;newNode->next=Head;Head = newNode; //每插入一个结点,都是要把它放在第一个结点的位置}}template<class Type>void LinkList<Type>::createBackward()//尾插法{Node<Type>* newNode;Node<Type>* current;//总是指向最后一个节点cout<<"输入链表长度:"<<endl;cin>>len;for (int i=1;i<=len;i++){newNode = new Node<Type>;cout<<"输入元素:"<<endl;cin>>newNode->data;newNode->next = NULL;if (i==1){Head = newNode;current = Head;}else{current->next=newNode;current = current->next;}}}template<class Type>void LinkList<Type>::initList() //所有结点都销毁{destoryList();len=0;Head=NULL;}template<class Type>bool LinkList<Type>::isEmptyList(){if (Head==NULL){return true;}else{return false;}}template<class Type>int LinkList<Type>::length(){return len;}template<class Type>void LinkList<Type>::destoryList()//销毁包括头结点{Node<Type>* current;while(Head!=NULL){current = Head;Head = current->next;delete current;}Head=NULL;len=0;}template<class Type>void LinkList<Type>::getFirstData(Type& firstItem){if (!isEmptyList()){firstItem = Head->data;}else{cout<<"链表为空!"<<endl;}}template<class Type>void LinkList<Type>::getData(int pos,Type& newItem){if (pos<1 || pos>len){cout<<"位置不当!"<<endl;}else{Node<Type>* current = Head;for (int i=1;i<pos;i++){current = current->next;}newItem = current->data;}}template<class Type>void LinkList<Type>::insertFirst(Type newItem){Node<Type> *newNode = new Node<Type>;newNode->data = newItem;newNode->next = Head;Head= newNode;len++;}template<class Type>void LinkList<Type>::insertLast(Type newItem){Node<Type>* current = Head;while(current!=NULL && current->next!=NULL){current = current->next;}Node<Type> *newNode = new Node<Type>;newNode->data = newItem;if (current==NULL)//链表为空{newNode->next = current;current = newNode;}else{newNode->next = current->next;current->next = newNode;}len++;}template<class Type>void LinkList<Type>::deleteFirst(Type& data){if (!isEmptyList()){data = Head->data;Node<Type>* temp = Head;Head = Head->next;delete temp;}else{cout<<"栈空!"<<endl;}len--;}template<class Type>void LinkList<Type>::deleteLast(){Node<Type>* current = Head;if (isEmptyList()){cout<<"链表为空!"<<endl;}else{for (int i=1;i<len-1;i++){current = current->next;}Node<Type>* nextCurrent = current->next;current->next = nextCurrent->next;delete nextCurrent;}len--;}template<class Type>void LinkList<Type>::reverse(){Node<Type>* current = Head;Head=NULL;if (current==NULL){cout<<"链表为空!"<<endl;}else{while (current!=NULL){Node<Type>* nextCurrent = current->next;current->next = Head;Head=current;current = nextCurrent;}}}template<class Type>ostream& operator<< <>(ostream& cout,const LinkList<Type>& list){Node<Type>*current=list.Head; //有头结点,这是current指向第一个结点if (current!=NULL){while (current!=NULL){cout<<current->data<<endl;current=current->next;}}else{cout<<"链表为空!"<<endl;}return cout;}//链队列/*链栈的条件栈顶初始值:front=rear=NULL队头:front总是指向第一个元素,且栈顶就是头指针,没有头结点队尾:rear总是指向最后一个元素队不存在:front=NULL队空:front=rear && (front!=NULL)栈满:不存在栈满,可以继续申请空间入队: 申请空间出队:释放空间*/template<class Type>class Queue{private:LinkList<Type> linklist;public:Queue();Queue(const Queue<Type>& otherQueue);~Queue();void operator=(const Queue<Type>& otherQueue);void initQueue();bool isEmptyQueue()const;void destoryQueue();void deQueue(Type& data);void enQueue(Type data);int  count()const;};/*这里可以不定义两个构造函数、析构函数和赋值函数,因为这里系统会自动调用默认的函数,其运行时又会自动调用LinkList的对应函数*/template<class Type>Queue<Type>::Queue():linklist(){}template<class Type>Queue<Type>::Queue(const Queue<Type>& otherQueue):linklist(otherQueue.linklist)//新建对象的数组没有空间,这时要申请空间{}template<class Type>Queue<Type>::~Queue() //有单链表进行处理{}/* 返回值为Queue<Type>,就会陷入无限循环,如果不使用返回值就可以往下执行,不知道是为什么template<class Type>Queue<Type> Queue<Type>::operator=(const Queue<Type>& otherQueue){linklist.operator=(otherQueue.linklist);return *this;}*/template<class Type>void Queue<Type>::operator=(const Queue<Type>& otherQueue){linklist.operator=(otherQueue.linklist);}template<class Type>void Queue<Type>::initQueue(){linkList.initList();}template<class Type>bool Queue<Type>::isEmptyQueue()const{return linklist.isEmptyList();}template<class Type>void Queue<Type>::destoryQueue(){linklist.destoryList();}template<class Type>void Queue<Type>::deQueue(Type& data){linklist.deleteFirst(data);}template<class Type>void Queue<Type>::enQueue(Type NewItem){linklist.insertFirst(NewItem);}template<class Type>int Queue<Type>::count()const{return linklist.len;}void main(){Queue<int> s;int a[4]={1,2,3,4};for (int i=0;i<4;i++){s.enQueue(a[i]);}Queue<int> s1;s1 = s;int data;for (int i=0;i<4;i++){s.deQueue(data);cout<<data<<endl;}system("pause");}