単一チェーン・テーブル・ベースのチェーン・キュー
単一チェーンテーブルクラス(先頭ノードなし)をキューのプライベート変数として使用
問題:コピーコンストラクタが機能しない
コード:
問題:コピーコンストラクタが機能しない
コード:
// 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;
}