第十章 基本数据结构
以前写的栈类:
.h文件
/********************************************************************
created: 2011/03/25
created: 25:3:2011 15:38
filename: Stack.h
file base: Stack
file ext: h
author: Danir
purpose: 堆栈
*********************************************************************/
#ifndef STACK_H_
#define STACK_H_
#include <iostream>
using namespace std;
/************************************************************************
数组描述
************************************************************************/
template <class T>
class Stack
{
public:
Stack();
Stack(int nMaxStackSize);
Stack(const Stack<T>& sStack);
~Stack();
bool IsEmpty() const;
bool IsFull() const;
T Top() const;
Stack<T>& Add(const T& x);
Stack<T>& Delete(T& x);
int Length() const;
void Output(ostream& out) const;
void Input(istream& in);
void Split(Stack<T>& StackA,Stack<T>& StackB) const;
Stack<T>& Combine(Stack<T>& sStack);
protected:
private:
int m_nTop;//栈顶
int m_nMaxTop;//最大栈顶值
T* m_pStack;//堆元素数组
};
/************************************************************************
链表描述
************************************************************************/
template <class T> class LinkedStack;
template <class T>
class Node
{
friend class LinkedStack<T>;
private:
T m_Data;
Node<T>* m_pLink;
};
template <class T>
class LinkedStack
{
public:
LinkedStack();
LinkedStack(const LinkedStack<T>& lStack);
~LinkedStack();
bool IsEmpty() const;
bool IsFull() const;
T Top() const;
LinkedStack<T>& Add(const T& x);
LinkedStack<T>& Delete(T& x);
int Length() const;
void Output(ostream& out) const;
void Input(istream& in);
void Split(LinkedStack<T>& StackA,LinkedStack<T>& StackB) const;
LinkedStack<T>& Combine(LinkedStack<T>& lStack);
void Erase();
protected:
private:
Node<T>* m_pTop;
int m_nLength;
};
#endif
.cpp文件
/********************************************************************
created: 2011/03/25
created: 25:3:2011 15:59
filename: Stack.cpp
file base: Stack
file ext: cpp
author: Danir
purpose: 堆栈
*********************************************************************/
#include "Stack.h"
#include "xcept.h"
/************************************************************************
数组描述
************************************************************************/
/************************************************************************
Function:构造函数
Parameter:
Return:
************************************************************************/
template <class T>
Stack<T>::Stack()
{
m_nMaxTop = 0;
m_pStack = new T[1];
m_nTop = -1;
}
template <class T>
Stack<T>::Stack(int nMaxStackSize)
{
m_nMaxTop = nMaxStackSize - 1;
m_pStack = new T[nMaxStackSize];
m_nTop = -1;
}
template <class T>
Stack<T>::Stack(const Stack<T>& sStack)
{
m_nMaxTop = sStack.m_nMaxTop;
m_nTop = sStack.m_nTop;
m_pStack = new T[m_nMaxTop + 1];
for (int i=0; i <= m_nTop; i++) {
m_pStack[i] = sStack.m_pStack[i];
}
}
/************************************************************************
Function:析构函数
Parameter:
Return:
************************************************************************/
template <class T>
Stack<T>::~Stack()
{
delete [] m_pStack;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
T Stack<T>::Top() const
{
if (IsEmpty()) {
throw OutOfBounds();
}else{
return m_pStack[m_nTop];
}
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
Stack<T>& Stack<T>::Add(const T& x)
{
if (IsFull()) {
T *temp;
m_nMaxTop = m_nMaxTop*2 +1;
temp = new T[m_nMaxTop + 1];
for (int i = 0; i <= m_nTop; i++) {
temp[i] = m_pStack[i];
}
delete m_pStack;
m_pStack = temp;
}
m_pStack[++m_nTop] = x;
return *this;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
Stack<T>& Stack<T>::Delete(T& x)
{
if (IsEmpty()) {
throw OutOfBounds();
}
x = m_pStack[m_nTop--];
if (m_nTop<(m_nMaxTop/4)) {
T *temp;
m_nMaxTop /= 2;
temp = new T[m_nMaxTop + 1];
for (int i = 0; i <= m_nTop; i++) {
temp[i] = m_pStack[i];
}
delete m_pStack;
m_pStack = temp;
}
return *this;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
inline bool Stack<T>::IsEmpty() const
{
return m_nTop == -1;
}
template <class T>
inline bool Stack<T>::IsFull() const
{
return m_nTop == m_nMaxTop;
}
template <class T>
inline int Stack<T>::Length() const
{
return m_nTop + 1;
}
/************************************************************************
Function:输入函数
Parameter:
Return:
************************************************************************/
template <class T>
void Stack<T>::Input(istream& in)
{
int i,n;
T temp;
cout << "Enter number of Stack element" << endl;
in >> n;
for (i = 0; i < n; i++) {
cout << "Enter the element " << (i+1) << endl;
in >> temp;
Add(temp);
}
}
/************************************************************************
Function:输出
Parameter:
Return:
************************************************************************/
template <class T>
void Stack<T>::Output(ostream& out) const
{
for (int i = m_nTop; i >= 0; i--) {
out << m_pStack[i] << " ";
}
out << endl;
}
/************************************************************************
Function:拆分,从中间拆分
Parameter:
Return:
************************************************************************/
template <class T>
void Stack<T>::Split(Stack<T>& StackA,Stack<T>& StackB) const
{
int i;
StackA.m_nTop = -1;
StackB.m_nTop = -1;
for (i = 0;i <=m_nTop/2; i++) {
StackA.Add(m_pStack[i]);
}
for (i = m_nTop/2+1; i <= m_nTop; i++) {
StackB.Add(m_pStack[i]);
}
}
/************************************************************************
Function:合并
Parameter:
Return:
************************************************************************/
template <class T>
Stack<T>& Stack<T>::Combine(Stack<T>& sStack)
{
int i;
for (i = 0; i <= sStack.m_nTop; i++) {
Add(sStack.m_pStack[i]);
}
sStack.m_nTop = -1;
return *this;
}
/************************************************************************
Function:输入输出流
Parameter:
Return:
************************************************************************/
template <class T>
ostream& operator<<(ostream& out,const Stack<T>& sStack)
{
sStack.Output(out);
return out;
}
template <class T>
istream& operator>>(istream& in,Stack<T>& sStack)
{
sStack.Input(in);
return in;
}
/************************************************************************
链表描述
************************************************************************/
/************************************************************************
Function:构造函数
Parameter:
Return:
************************************************************************/
template <class T>
LinkedStack<T>::LinkedStack()
{
m_pTop = 0;
m_nLength = 0;
}
template <class T>
LinkedStack<T>::LinkedStack(const LinkedStack<T>& lStack)
{
Node<T> *ptemp,*pNext,*pNew;
m_nLength = lStack.m_nLength;
pNext = lStack.m_pTop;
if (pNext) {
ptemp = new Node<T>;
ptemp->m_Data = pNext->m_Data;
m_pTop = ptemp;
}
pNext = pNext->m_pLink;
while (pNext) {
pNew = new Node<T>;
ptemp->m_pLink = pNew;
pNew->m_Data = pNext->m_Data;
ptemp = pNew;
pNext = pNext->m_pLink;
}
ptemp->m_pLink = 0;
}
/************************************************************************
Function:析构
Parameter:
Return:
************************************************************************/
template <class T>
LinkedStack<T>::~LinkedStack()
{
Node<T>* pNext;
while (m_pTop) {
pNext = m_pTop->m_pLink;
delete m_pTop;
m_pTop = pNext;
}
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
bool IsFull()
{
try{
Node*<T> pNew = new Node<T>;
delete pNew;
pNew = 0;
return false;
}
catch (...) {
return true;
}
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
inline bool LinkedStack<T>::IsEmpty() const
{
return m_pTop == 0;
}
template <class T>
inline int LinkedStack<T>::Length() const
{
return m_nLength;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
T LinkedStack<T>::Top() const
{
if (IsEmpty()) {
throw OutOfBounds();
}
return m_pTop->m_Data;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
LinkedStack<T>& LinkedStack<T>::Add(const T& x)
{
Node<T> *pNew = new Node<T>;
pNew->m_Data = x;
pNew->m_pLink = m_pTop;
m_pTop = pNew;
m_nLength++;
return *this;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
LinkedStack<T>& LinkedStack<T>::Delete(T& x)
{
if (IsEmpty()) {
throw OutOfBounds();
}
x = m_pTop->m_Data;
Node<T> *ptemp = m_pTop;
m_pTop = m_pTop->m_pLink;
delete ptemp;
m_nLength--;
return *this;
}
/************************************************************************
Function:从中间拆分
Parameter:
Return:
************************************************************************/
template <class T>
void LinkedStack<T>::Split(LinkedStack<T>& StackA,LinkedStack<T>& StackB) const
{
if (!m_pTop) {
return;
}
if (!m_pTop->m_pLink) {//一个元素
StackA.Erase();
Node<T> *p = new Node<T>;
StackA.m_pTop = p;
p->m_Data = m_pTop->m_Data;
p->m_pLink = 0;
return;
}
Node<T> *pNew = new Node<T>;
Node<T> *pNext,*ptemp;
StackB.Erase();
StackB.m_pTop = pNew;
ptemp = pNew;
pNew->m_Data = m_pTop->m_Data;
pNext = m_pTop;
StackB.m_nLength = 1;
for (int i = 1; i < m_nLength/2; i++ ) {
pNext = pNext->m_pLink;
pNew = new Node<T>;
ptemp->m_pLink = pNew;
pNew->m_Data = pNext->m_Data;
ptemp = pNew;
StackB.m_nLength++;
}
pNew->m_pLink = 0;
pNew = new Node<T>;
StackA.Erase();
StackA.m_pTop = pNew;
pNext = pNext->m_pLink;
pNew->m_Data = pNext->m_Data;
ptemp = pNew;
pNext = pNext->m_pLink;
StackA.m_nLength = 1;
while (pNext) {
pNew = new Node<T>;
ptemp->m_pLink = pNew;
pNew->m_Data = pNext->m_Data;
ptemp = pNew;
pNext = pNext->m_pLink;
StackA.m_nLength++;
}
pNew->m_pLink = 0;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
void LinkedStack<T>::Erase()
{
Node<T>* pNext = m_pTop;
while (m_pTop) {
pNext = m_pTop->m_pLink;
delete m_pTop;
m_pTop = pNext;
}
m_pTop = 0;
m_nLength = 0;
}
/************************************************************************
Function:合并,lStack所有元素放到当前堆栈顶部
Parameter:
Return:
************************************************************************/
template <class T>
LinkedStack<T>& LinkedStack<T>::Combine(LinkedStack<T>& lStack)
{
Node<T> *pNext = lStack.m_pTop;
Node<T> *ptemp;
while (pNext) {
ptemp = pNext;
pNext = pNext->m_pLink;
}
ptemp->m_pLink = m_pTop;
m_pTop = lStack.m_pTop;
m_nLength +=lStack.m_nLength;
lStack.m_pTop = 0;
lStack.m_nLength = 0;
return *this;
}
/************************************************************************
Function:输出
Parameter:
Return:
************************************************************************/
template <class T>
void LinkedStack<T>::Output(ostream& out) const
{
Node<T>* pNext = m_pTop;
while (pNext) {
out << pNext->m_Data << " ";
pNext = pNext->m_pLink;
}
}
template <class T>
ostream& operator<<(ostream& out,const LinkedStack<T>& lStack)
{
lStack.Output(out);
return out;
}
/************************************************************************
Function:输入
Parameter:
Return:
************************************************************************/
template <class T>
void LinkedStack<T>::Input(istream& in)
{
int i,n;
T temp;
cout << "Enter number of Stack element" << endl;
in >> n;
for (i = 0; i < n; i++) {
cout << "Enter element " << (i+1) << endl;
in >> temp;
Add(temp);
}
}
template <class T>
istream& operator>>(istream& in,LinkedStack<T>& lStack)
{
lStack.Input(in);
return in;
}
队列类:
.h文件
/********************************************************************
created: 2011/03/26
created: 26:3:2011 15:03
filename: Queue.h
file base: Queue
file ext: h
author: Danir
purpose: 队列
*********************************************************************/
#ifndef QUEUE_H_
#define QUEUE_H_
#include <iostream>
using namespace std;
/************************************************************************
公式化(数组)描述:location(i)=(location(1)+i-1)/maxsise
************************************************************************/
template <class T>
class Queue
{
public:
Queue(int nMaxQueueSize = 10);
~Queue();
bool IsEmpty() const;
bool IsFull() const;
T First() const;
T Last() const;
int Length() const;
Queue<T>& Add(const T& x);
Queue<T>& Delete(T& x);
void Output(ostream& out) const;
void Input(istream& in);
void Split(Queue<T>& qQueueA,Queue<T>& qQueueB) const;
Queue<T>& Alternate(const Queue<T>& qQueueA,const Queue<T>& qQueueB);
protected:
private:
int m_nFront;
int m_nRear;
int m_nMaxSize;
T *m_pQueue;
};
/************************************************************************
链表描述
************************************************************************/
template <class T> class LinkedQueue;
template <class T>
class Node
{
friend class LinkedQueue<T>;
private:
T m_Data;
Node<T>* m_pLink;
};
template <class T>
class LinkedQueue
{
public:
LinkedQueue();
~LinkedQueue();
bool IsEmpty() const;
bool IsFull() const;
T First() const;
T Last() const;
int Length() const;
LinkedQueue<T>& Add(const T& x);
LinkedQueue<T>& Delete(T& x);
void Output(ostream& out) const;
void Input(istream& in);
void Split(LinkedQueue<T>& qQueueA,LinkedQueue<T>& qQueueB);
LinkedQueue<T>& Alternate(LinkedQueue<T>& qQueueA,LinkedQueue<T>& qQueueB);
void Erase();
protected:
private:
Node<T>* m_pFront;
Node<T>* m_pRear;
};
#endif
.CPP文件
/********************************************************************
created: 2011/03/26
created: 26:3:2011 15:11
filename: Queue.cpp
file base: Queue
file ext: cpp
author: Danir
purpose: 队列
*********************************************************************/
#include "Queue.h"
#include "xcept.h"
/************************************************************************
公式化(数组)描述:location(i)=(location(1)+i-1)/maxsise
************************************************************************/
/************************************************************************
Function:构造函数
Parameter:
Return:
************************************************************************/
template <class T>
Queue<T>::Queue(int nMaxQueueSize /* = 10 */)
{
m_nMaxSize = nMaxQueueSize + 1;
m_pQueue = new T[m_nMaxSize];
m_nFront = m_nRear = 0;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
Queue<T>::~Queue()
{
delete [] m_pQueue;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
inline bool Queue<T>::IsEmpty() const
{
return m_nFront == m_nRear;
}
template <class T>
inline bool Queue<T>::IsFull() const
{
return (((m_nRear + 1)%m_nMaxSize == m_nFront)?1:0);
}
template <class T>
inline int Queue<T>::Length() const
{
return (m_nRear - m_nFront + m_nMaxSize)%m_nMaxSize;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
T Queue<T>::First() const
{
if (IsEmpty()) {
throw OutOfBounds();
}
return m_pQueue[(m_nFront+1)%m_nMaxSize];
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
T Queue<T>::Last() const
{
if (IsEmpty()) {
throw OutOfBounds();
}
return m_pQueue[m_nRear];
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
Queue<T>& Queue<T>::Add(const T& x)
{
if (IsFull()) {
throw NoMem();
}
m_nRear = (m_nRear + 1)%m_nMaxSize;
m_pQueue[m_nRear] = x;
return *this;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
Queue<T>& Queue<T>::Delete(T& x)
{
if (IsEmpty()) {
throw OutOfBounds();
}
m_nFront =(m_nFront +1)%m_nMaxSize;
x = m_pQueue[m_nFront];
return *this;
}
/************************************************************************
Function:输出
Parameter:
Return:
************************************************************************/
template <class T>
void Queue<T>::Output(ostream& out) const
{
int nIndex;
int nlength = Length();
for (nIndex = 1;nIndex <= nlength;nIndex++) {
out << m_pQueue[(nIndex+m_nFront)%m_nMaxSize] << " ";
}
}
template <class T>
ostream& operator<<(ostream& out, const Queue<T>& qQueue)
{
qQueue.Output(out);
return out;
}
/************************************************************************
Function:输入
Parameter:
Return:
************************************************************************/
template <class T>
void Queue<T>::Input(istream& in)
{
int i,n;
T temp;
cout << "Enter number of Stack element" << endl;
in >> n;
for (i = 0; i < n; i++) {
cout << "Enter element " << (i+1) << endl;
in >> temp;
Add(temp);
}
}
template <class T>
istream& operator>>(istream& in,Queue<T>& qQueue)
{
qQueue.Input(in);
return in;
}
/************************************************************************
Function:奇偶拆分
Parameter:
Return:
************************************************************************/
template <class T>
void Queue<T>::Split(Queue<T>& qQueueA,Queue<T>& qQueueB) const
{
int nIndex;
int nlength = Length();
qQueueA.m_nFront = qQueueA.m_nRear = 0;
qQueueB.m_nFront = qQueueB.m_nRear = 0;
for (nIndex = nlength;nIndex > 0;nIndex--) {
if (nIndex%2 == 1) {
qQueueA.Add(m_pQueue[(nIndex+m_nFront)%m_nMaxSize]);
}else{
qQueueB.Add(m_pQueue[(nIndex+m_nFront)%m_nMaxSize]);
}
}
}
/************************************************************************
Function:轮流合并
Parameter:
Return:
************************************************************************/
template <class T>
Queue<T>& Queue<T>::Alternate(const Queue<T>& qQueueA,const Queue<T>& qQueueB)
{
int lenA = qQueueA.Length();
int lenB = qQueueB.Length();
int nlength = lenA + lenB;
int Index = 1;
if (m_nMaxSize <= nlength) {
m_nMaxSize = nlength +1;
delete [] m_pQueue;
m_pQueue = new T[m_nMaxSize];
}
m_nFront = m_nRear = 0;
while (Index <= lenA && Index <= lenB) {
m_pQueue[++m_nRear] = qQueueA.m_pQueue[(qQueueA.m_nFront+Index)%qQueueA.m_nMaxSize];
m_pQueue[++m_nRear] = qQueueB.m_pQueue[(qQueueB.m_nFront+Index)%qQueueB.m_nMaxSize];
Index++;
}
while (Index <= lenA) {
m_pQueue[++m_nRear] = qQueueA.m_pQueue[(qQueueA.m_nFront+Index)%qQueueA.m_nMaxSize];
Index++;
}
while (Index <= lenB) {
m_pQueue[++m_nRear] = qQueueB.m_pQueue[(qQueueB.m_nFront+Index)%qQueueB.m_nMaxSize];
Index++;
}
return *this;
}
/************************************************************************
链表描述
************************************************************************/
/************************************************************************
Function:构造函数
Parameter:
Return:
************************************************************************/
template <class T>
LinkedQueue<T>::LinkedQueue()
{
m_pFront = m_pRear = 0;
}
/************************************************************************
Function:析构函数
Parameter:
Return:
************************************************************************/
template <class T>
LinkedQueue<T>::~LinkedQueue()
{
Node<T>* pNext;
while (m_pFront) {
pNext = m_pFront->m_pLink;
delete m_pFront;
m_pFront = pNext;
}
}
template<class T>
void LinkedQueue<T>::Erase()
{
Node<T>* pNext;
while (m_pFront) {
pNext = m_pFront->m_pLink;
delete m_pFront;
m_pFront = pNext;
}
m_pFront = 0;
m_pRear = 0;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
bool LinkedQueue<T>::IsFull() const
{
Node<T>* pnew;
try{
pnew = new Node<T>;
delete pnew;
return false;
}
catch (NoMem) {
return true;
}
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
int LinkedQueue<T>::Length() const
{
Node<T>* pNext = m_pFront;
int i = 0;
while (pNext) {
i++;
pNext = pNext->m_pLink;
}
return i;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
T LinkedQueue<T>::First() const
{
if (IsEmpty()) {
throw OutOfBounds();
}
return m_pFront->m_Data;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
T LinkedQueue<T>::Last() const
{
if (IsEmpty()) {
throw OutOfBounds();
}
return m_pRear->m_Data;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
LinkedQueue<T>& LinkedQueue<T>::Add(const T& x)
{
Node<T>* pNew = new Node<T>;
pNew->m_Data = x;
pNew->m_pLink = 0;
if (m_pFront) {
m_pRear->m_pLink = pNew;
}else{
m_pFront = pNew;
}
m_pRear = pNew;
return *this;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x)
{
if (IsEmpty()) {
throw OutOfBounds();
}
x = m_pFront->m_Data;
Node<T> *pNext = m_pFront;
m_pFront = m_pFront->m_pLink;
delete pNext;
return *this;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
inline bool LinkedQueue<T>::IsEmpty() const
{
return ((m_pFront)?false:true);
}
/************************************************************************
Function:输出
Parameter:
Return:
************************************************************************/
template <class T>
void LinkedQueue<T>::Output(ostream& out) const
{
Node<T>* pNext = m_pFront;
while (pNext) {
out << pNext->m_Data << " ";
pNext = pNext->m_pLink;
}
}
template <class T>
ostream& operator<<(ostream& out,const LinkedQueue<T>& lqQueue)
{
lqQueue.Output(out);
return out;
}
/************************************************************************
Function:输入
Parameter:
Return:
************************************************************************/
template <class T>
void LinkedQueue<T>::Input(istream& in)
{
int i,n;
T temp;
cout << "Enter number of Queue element" << endl;
in >> n;
for (i = 0; i < n; i++) {
cout << "Enter element " << (i+1) << endl;
in >> temp;
Add(temp);
}
}
template <class T>
istream& operator>>(istream& in,LinkedQueue<T>& lqQueue)
{
lqQueue.Input(in);
return in;
}
/************************************************************************
Function:奇偶拆分
Parameter:
Return:
************************************************************************/
template <class T>
void LinkedQueue<T>::Split(LinkedQueue<T>& qQueueA,LinkedQueue<T>& qQueueB)
{
Node<T> *cnCurrentA,*cnCurrentB,*cnCurrentC;
cnCurrentC = m_pFront;
if (!cnCurrentC) {//C为空表
qQueueA.m_pFront = 0;
qQueueA.m_pRear = 0;
qQueueB.m_pFront = 0;
qQueueB.m_pRear = 0;
return;
}
if (cnCurrentC) {//A表奇数位
cnCurrentA = cnCurrentC;
qQueueA.m_pFront = cnCurrentC;
}
if (!cnCurrentC->m_pLink) {//C表只有一个元素
cnCurrentA->m_pLink = 0;
qQueueA.m_pRear = cnCurrentC;
qQueueB.m_pFront = qQueueB.m_pRear =0;
m_pFront = m_pRear =0;
return;
}else{//C表至少两个元素
cnCurrentB = cnCurrentC->m_pLink;
qQueueB.m_pFront = cnCurrentC->m_pLink;
}
int nIndex,nLength = Length()/2;
for (nIndex =0;nIndex < nLength;nIndex++) {
cnCurrentA->m_pLink = cnCurrentB->m_pLink;
if (!cnCurrentA->m_pLink) {
break;
}
cnCurrentA = cnCurrentB->m_pLink;
cnCurrentB->m_pLink = cnCurrentA->m_pLink;
if (!cnCurrentB->m_pLink) {
break;
}
cnCurrentB = cnCurrentA->m_pLink;
}
qQueueA.m_pRear = cnCurrentA;
qQueueB.m_pRear = cnCurrentB;
m_pFront = m_pRear = 0;
return;
}
/************************************************************************
Function:轮流合并
Parameter:
Return:
************************************************************************/
template <class T>
LinkedQueue<T>& LinkedQueue<T>::Alternate(LinkedQueue<T>& qQueueA,LinkedQueue<T>& qQueueB)
{
Node<T> *cnCurrentA,*cnCurrentB,*cnCurrentC;
cnCurrentA = qQueueA.m_pFront;
cnCurrentB = qQueueB.m_pFront;
Erase();//清除本表
if (!cnCurrentA&&!cnCurrentB) {//两张空表
m_pFront = m_pRear = 0;
return *this;
}
if (!cnCurrentA) {//A表空
m_pFront = qQueueB.m_pFront;
m_pRear = qQueueB.m_pRear;
qQueueB.m_pFront = 0;
qQueueB.m_pRear = 0;
return *this;
}
if (!cnCurrentB) {//B表空
m_pFront = qQueueA.m_pFront;
m_pRear = qQueueA.m_pRear;
qQueueA.m_pFront = 0;
qQueueA.m_pRear = 0;
return *this;
}
m_pFront = cnCurrentA;
while (cnCurrentA&&cnCurrentB) {
cnCurrentC = cnCurrentA;
cnCurrentA = cnCurrentA->m_pLink;
cnCurrentC->m_pLink = cnCurrentB;
cnCurrentB = cnCurrentB->m_pLink;
cnCurrentC = cnCurrentC->m_pLink;
cnCurrentC->m_pLink = cnCurrentA;
}
if (cnCurrentA) {//链表
m_pRear = qQueueA.m_pRear;
}else if(cnCurrentB) {
cnCurrentC->m_pLink = cnCurrentB;//cnCurrentA 为空
m_pRear = qQueueB.m_pRear;
}else{
m_pRear = qQueueB.m_pRear;
}
qQueueB.m_pFront = qQueueB.m_pRear = 0;
qQueueA.m_pFront = qQueueA.m_pRear = 0;
return *this;
}