基于单链表的链队列

来源:互联网 发布:七灯军团戒指淘宝 编辑:程序博客网 时间:2024/06/08 05:26

将单链表类(不带头结点)作为队列的私有变量

存在问题:拷贝构造函数不起作用

代码:

// LinkListQueue.cpp : 定义控制台应用程序的入口点。//定义的链表不含头结点//将单链表类作为链队列类的私有变量#include "stdafx.h"#include <iostream>using namespace std;template<class Type>//定义结点struct Node{Type data;Node<Type> *next;};//定义链表template<class Type>class LinkList{public:LinkList();//默认构造函数LinkList(const LinkList<Type>& otherList);//拷贝构造函数~LinkList();void createInsertHead();//头插法void createInsertRear();//尾插法void initList();//将链表归为初始状态bool isEmpty();//判断链表是否为空int  length();void destoryList();//销毁链表void getFirstData(Type& firstdata);void getData(int pos,Type& data);void insertFirst(Type newdata);//在表头插入新的元素void insertLast(Type newdata);//在表尾插入新的元素void deleteFirst(Type& data);//删除表头元素void deleteLast(Type& data);//删除表尾元素void reverse();const LinkList<Type>& operator=(const LinkList<Type>&otherList);//重载赋值运算符friend ostream& operator<< <>(ostream& cout,const LinkList<Type>& list);private:int m_length;//链表中结点个数Node<Type>* head; //不使用头结点};template<class Type>LinkList<Type>::LinkList()//默认构造函数{head = NULL;m_length = 0;}template<class Type>LinkList<Type>::LinkList(const LinkList<Type>& otherList)//拷贝构造函数{head = NULL;m_length = otherList.m_length;Node<Type> *current;Node<Type> *otherListcurrent = otherList.head;while(otherListcurrent != NULL){for (int i=1; i<=otherList.m_length; 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>LinkList<Type>::~LinkList(){destoryList();}template<class Type>void LinkList<Type>::createInsertHead()//头插法{Node<Type> *newnode;cout<<"请输入链表的长度:";cin>>m_length;cout<<"请输入链表的元素:";for (int i=0; i<m_length; i++){newnode = new Node<Type>;cin>>newnode->data;newnode->next = head;head = newnode;}}template<class Type>void LinkList<Type>::createInsertRear()//尾插法    尾插法和拷贝构造函数都有单独考虑第一个结点的情况{Node<Type> *newnode;Node<Type> *current;cout<<"请输入链表的长度:";cin>>m_length;cout<<"请输入链表的元素:";for (int i=1; i<=m_length; i++){newnode = new Node<Type>;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();head = NULL;m_length = 0;}template<class Type>bool LinkList<Type>::isEmpty(){if (head == NULL){return true;}else {return false;}}template<class Type>int  LinkList<Type>::length(){return m_length;}template<class Type>void LinkList<Type>::destoryList(){Node<Type> *current;while(head != NULL){current = head;head = current->next;delete current;}head = NULL;m_length = 0;}template<class Type>void LinkList<Type>::getFirstData(Type& firstdata){if (head!=NULL){firstdata = head->data;}else {cout<<"链表为空!"<<endl;}}template<class Type>void LinkList<Type>::getData(int pos,Type& data){if (pos<1||pos>m_length){cout<<"指定的位置不正确!"<<endl;}else{Node<Type> *current = head;int i=0;while(i<pos-1){current = current->next;i++;}data = current->data;} }template<class Type>void LinkList<Type>::insertFirst(Type newdata){Node<Type> *newnode = new Node<Type>;newnode->data = newdata;newnode->next = head;head = newnode;m_length++;}template<class Type>void LinkList<Type>::insertLast(Type newdata)//尾部插入元素{Node<Type> *current = head;while(current != NULL && current->next != NULL){current = current->next;}Node<Type> *newnode = new Node<Type>;newnode->data = newdata;if (current==NULL){/*newnode->next = current;current = newnode;*/newnode->next = head;head = newnode;}else{newnode->next = current->next;current->next = newnode;}m_length++;}template<class Type>void LinkList<Type>::deleteFirst(Type& data){if (isEmpty()){cout<<"链表为空!"<<endl;}else{Node<Type> *temp = head;data = head->data;head = head->next;delete temp;}m_length--;}template<class Type>void LinkList<Type>::deleteLast(Type& data){if (isEmpty()){cout<<"链表为空!"<<endl;}else{Node<Type> *current = head;for (int i=1; i<m_length-1; i++){current = current->next;}Node<Type> *temp = current->next;data = temp->data;current->next = temp->next;delete temp;m_length--;}}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>const LinkList<Type>& LinkList<Type>::operator=(const LinkList<Type>&otherList){Node<Type> *current;Node<Type> *otherListcurrent = otherList.head;if (this!=&otherList){if (!isEmpty()){initList();}if (otherListcurrent!=NULL){for (int i=1; i<=otherList.m_length; i++) //或用while{Node<Type> *newnode = new Node<Type>;newnode->data = otherListcurrent->data;newnode->next = NULL;if (i==1){head = newnode;current = head;//current始终指向链表的尾元素}else{current->next = newnode;current = current->next;}otherListcurrent = otherListcurrent->next;}}}return *this;}template<class Type>ostream& operator<< <>(ostream& cout,const LinkList<Type>& list){Node<Type> *current = list.head;if (current==NULL){cout<<"链表为空!"<<endl;}else{while(current!=NULL){cout<<current->data<<" ";current = current->next;}cout<<endl;}return cout;}//链队列/*链栈的条件栈顶初始值:front=rear=NULL队头:front总是指向第一个元素,且栈顶就是头指针,没有头结点队尾:rear总是指向最后一个元素队不存在:front=NULL队空:front=rear && (front!=NULL)栈满:不存在栈满,可以继续申请空间入队: 申请空间出队:释放空间*/template<class Type>class Queue{public:Queue();Queue(const Queue<Type>& otherQueue);~Queue();const Queue<Type>& operator=(const Queue<Type>& otherQueue);void initQueue();bool isEmptyQueue()const;void destoryQueue();void deQueue(Type& data);void enQueue(Type data);int  count()const;private:LinkList<Type> linklist;};/*这里可以不定义两个构造函数、析构函数和赋值函数,因为这里系统会自动调用默认的函数,其运行时又会自动调用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(){}template<class Type>const Queue<Type>& Queue<Type>::operator=(const Queue<Type>& otherQueue){linklist.operator=(otherQueue.linklist);return *this;}template<class Type>void Queue<Type>::initQueue(){linklist.initList();}template<class Type>bool Queue<Type>::isEmptyQueue()const{linklist.isEmpty();}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 data){linklist.insertLast(data);}template<class Type>int  Queue<Type>::count()const{return linklist.m_length;}int _tmain(int argc, _TCHAR* argv[]){Queue<char> queue1;Queue<char> queue2;char a[4] = {'1','2','3','4'};for (int i=0; i<4; i++){queue1.enQueue(a[i]);}queue2 = queue1;for (int i=0; i<4; i++){char b;queue2.deQueue(b);cout<<b<<" ";}cout<<endl;Queue<char> queue3(queue2);for (int i=0; i<4; i++){char c;queue3.deQueue(c);cout<<c<<" ";}cout<<endl;system("pause");return 0;}



 

原创粉丝点击