基于单链表的链队列

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

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

代码:

// 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;
}



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值