线性表中单链表的前m个元素与剩下的元素互换,请问void Exchange_L(LinkQueue<int> L, int m )里面有什么问题?
#include<iostream>#include<cstdlib>
using namespace std;
template<class ElemType>
struct LinkQueueNode
{
ElemType data;
LinkQueueNode<ElemType> *next;
LinkQueueNode(LinkQueueNode<ElemType> *ptr = NULL)
{
next = ptr;
}
LinkQueueNode(ElemType &item, LinkQueueNode<ElemType> *ptr = NULL)//构造函数2,用于构造其他函数
{
data = item;
next = ptr;
}
ElemType getData()
{
return data;
}
void SetLink(LinkQueueNode<ElemType> *link)
{
next = link; //修改节点的next域
}
void SetData(ElemType value)
{
data = value; //修改节点中的data域
}
};
template<class ElemType>
class LinkQueue
{
private:
LinkQueueNode<ElemType> *front;
LinkQueueNode<ElemType> *rear;
int length;
public:
LinkQueue();
~LinkQueue()
{
LinkQueueDestroy();
}
bool LinkQueueDestroy();//销毁链表
bool LinkQueueClear();//清空链表
int QueueLength()const
{
return length; //返回长度
}
bool QueueisEmpty()const; //判空
bool deQueue(ElemType &e);//出队
void enQueue(ElemType e);//入队
LinkQueueNode<ElemType> *GetFront()
{
return front; //获取头结点指针
}
ElemType GetFrontData()
{
return front->data; //头元素
}
LinkQueueNode<ElemType> *GetRear()
{
return rear; //获取尾指针
}
bool QueueTraverse()const;
void SetFront(LinkQueueNode<ElemType> *p)
{
front = p;
}
};
template<class ElemType>
LinkQueue<ElemType>::LinkQueue()
{
front = rear = new LinkQueueNode<ElemType>;
if(!front) exit(1);
front->next = NULL;
length = 0;
}
template<class ElemType>
bool LinkQueue<ElemType>::QueueisEmpty()const
{
if(front == rear) return true;
return false;
}
template<class ElemType>
bool LinkQueue<ElemType>::deQueue(ElemType &e)
{
if(front == rear) return false;
LinkQueueNode<ElemType> *p;
p = front->next;
e = p->data;
front->next = p->next;
length--;
if(rear == p) rear = front;
delete(p);
return true;
}
template<class ElemType>
void LinkQueue<ElemType>::enQueue(ElemType e)
{
LinkQueueNode<ElemType> *s;
s = new LinkQueueNode<ElemType>;
s->data = e;
s->next = NULL;
rear->next = s;
rear = s;
length = length + 1;
}
template<class ElemType>
bool LinkQueue<ElemType>::QueueTraverse()const
{
LinkQueueNode<ElemType> *p;
p = front->next;
while(p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
return true;
}
template<class ElemType>
bool LinkQueue<ElemType>::LinkQueueDestroy()
{
LinkQueueNode<ElemType> *p;
if(!front) return false;
while(front->next)
{
p = front->next;
front->next = p->next;
delete p;
}
front = rear = NULL;
return true;
}
template<class ElemType>
bool LinkQueue<ElemType>::LinkQueueClear()
{
LinkQueueNode<ElemType> *p;
if(!front) return false;
while(front->next)
{
p = front->next;
front->next = p->next;
delete p;
}
return true;
}
void Exchange_L(LinkQueue<int> L, int m )
{
LinkQueueNode<int> *p, *q, *pr;
p = new LinkQueueNode<int>;
q = new LinkQueueNode<int>;
pr = new LinkQueueNode<int>;
p = L.GetFront();
q = L.GetRear();
q -> next = p;
int j = 0;
p = L.GetFront()->next;
pr = L.GetFront()->next;
while(j < m)
{
j++;
p = p->next;
pr = pr->next;
}
p -> next = NULL;
//pr = pr->next;
L.SetFront(pr);
}
int main()
{
int n;
while(cin >> n)
{
int i, a;
LinkQueue<int> L;
for(i = 0; i < n; i++)
{
cin >> a;
L.enQueue(a);
}
L.QueueTraverse();
int m;
cin >> m;
Exchange_L(L, m);
L.QueueTraverse();
}
return 0;
}