链表 环形链表 队列 环形队列 栈

#include
#include "stdlib.h"
#define  _AFXDLL
#include "windows.h"
#include "process.h"
#define SelTest 2
using namespace std;
template
class mylist
{
private:
	struct node
	{
		T data;
		node* m_next;
		node()
		{
			m_next=NULL;
		}
	};
public:
	
	mylist()
	{
		m_head =NULL;
		m_isize =0;
	}
private:

	node* m_head;
	int m_isize;
public:
	void show()
	{
		int i=0;
		node * ptemp= m_head;
		if(ptemp==NULL)
			return;
		cout<<"链表显示开始";
		while(ptemp->m_next !=NULL)
		{
			if(i%20==0)
				cout<data<<" ";
			ptemp=ptemp->m_next;
		}
		std::cout<data<data = val;
		m_isize++;
		if(!m_head)
		{
			m_head =pnode;
			m_head->m_next=NULL;
		}
		else
		{ 
			node* pnodetemp = m_head ;
			while(pnodetemp->m_next !=NULL)
			{
				pnodetemp= pnodetemp->m_next;
			}
			pnodetemp->m_next = pnode;
		}
	}
	bool insertNode(int index ,T val)
	{
		node * ptemp= m_head;
		if(index<=m_isize-1)
		{
			int ix=0;
			node* pnode = new node;
			pnode->data = val;
			node* poldnode=ptemp;
			while (index != ix++  && ptemp && ptemp->m_next)
			{
				poldnode=ptemp;
				ptemp = ptemp->m_next;
			}
			if(index != 0)
			{  
				poldnode->m_next =pnode;
				pnode->m_next= ptemp;
			}
			else
			{
				pnode->m_next =m_head;
				m_head = pnode;
			}
			m_isize++;
			return true;
		}
		else
			return false;
	}
	void ReversalList()
	{
		if(m_head==NULL  || m_head->m_next==NULL )
			return;
		node* firistnode =m_head;
		node* p=  m_head;
		node* q=p ? p->m_next:NULL;	
		node* m=q ? q->m_next:NULL;
		
		while (q->m_next!=NULL)
		{
			q->m_next=p;
			p=q;
			q=m;
			m=q->m_next;
		}
		if(q)
		{
	      q->m_next=p;
		  m_head =q;
		}
		firistnode->m_next=NULL;
	}
	bool check()
	{
		if (m_head==NULL)
		 return false;
		node * pfast = m_head->m_next;
		node * pslow = m_head;

		while ( pfast!=pslow  && pfast->m_next!=NULL )
		{
			pfast=pfast->m_next;
			pslow=pslow->m_next;
		}
		if(pfast->m_next!=NULL)
			return true;
		else
			return false;
	}
	void  pop()
	{
		if(m_head!=NULL)
		{
			node *ptem = m_head->m_next;
			delete m_head;
			m_head=ptem;
		}
	};

	void foront(T& Val)
	{
		if(m_head!=NULL)
		{
			Val=m_head->data;
			pop();
		}
	}
};

template
class myCirclelist
{
private:
	struct node
	{
		T data;
		node* m_next;
		node()
		{
			m_next=NULL;
		}
	};
public:
	myCirclelist()
	{
		m_head =NULL;
		m_isize =0;
	}
private:
	node* m_head;
	int m_isize;
public:
	void show()
	{
		int i=0;
		node * ptemp= m_head;
		if(ptemp==NULL)
			return;
		cout<<"环形链表显示开始";
		while(ptemp->m_next !=m_head)
		{
			if(i%20==0)
				cout<data<<" ";
			ptemp=ptemp->m_next;
		}
		std::cout<data<data = val;
		m_isize++;
		if(!m_head)
		{
			m_head =pnode;
			m_head->m_next=m_head;
		}
		else
		{ 
			node* pnodetemp = m_head ;
			while(pnodetemp->m_next !=m_head)
			{
				pnodetemp= pnodetemp->m_next;
			}
			pnodetemp->m_next = pnode;
			pnode->m_next=m_head;
		}
	}
	bool insertNode(int index ,T val)
	{
		node * ptemp= m_head;
		if(index<=m_isize-1)
		{
			int ix=0;
			node* pnode = new node;
			pnode->data = val;
			node* poldnode=ptemp;
			while (index != ix++  && ptemp && ptemp->m_next)
			{
				poldnode=ptemp;
				ptemp = ptemp->m_next;
			}
			if(index != 0)
			{  
				poldnode->m_next =pnode;
				pnode->m_next= ptemp;
			}
			else
			{
				if(m_head==NULL)
					return false;
				node* ptem =m_head;
				while(ptem->m_next != m_head)
				{
					ptem=ptem->m_next;
				}
				pnode->m_next =m_head;
				m_head = pnode;
				ptem->m_next= m_head;
				
			}
			m_isize++;
			return true;
		}
		else
			return false;
	}
	
	bool check()
	{
		if (m_head==NULL)
			return false;
		node * pfast = m_head->m_next;
		node * pslow = m_head;

		while ( pfast!=pslow  && pfast->m_next!=NULL )
		{
			pfast=pfast->m_next;
			pslow=pslow->m_next;
		}
		if(pfast->m_next!=NULL)
			return true;
		else
			return false;
	}
	void  pop()
	{
		if(m_head!=NULL)
		{
			node *ptem = m_head->m_next;
			delete m_head;
			if(ptem != m_head)
			{
				m_head=ptem;
			}
			else
				m_head=NULL;
			
		}
	};

	void foront(T& Val)
	{
		if(m_head!=NULL)
		{
			Val=m_head->data;
			pop();
		}
	}
};
template
class myCircleQueueByList
{
private:
	struct node
	{
		T data;
		node* m_next;
		node()
		{
			m_next=NULL;
		}
	};
public:
	myCircleQueueByList()
	{
		m_pTail=m_head =NULL;
		m_isize =0;
	}
private:
	node* m_head;
	node* m_pTail;
	int m_isize;
public:
	void show()
	{
		int i=0;
		node * ptemp= m_head;
		if(ptemp==NULL)
			return;
		cout<<"环形链式队列显示开始";
		while(ptemp->m_next !=m_head)
		{
			if(i%20==0)
				cout<data<<" ";
			ptemp=ptemp->m_next;
		}
		std::cout<data<data = val;
		m_isize++;
		if(!m_head)
		{
			m_head =pnode;
			m_head->m_next=m_head;
			m_pTail=m_head;
		}
		else
		{ 
			node* pnodetemp = m_head ;
			while(pnodetemp->m_next !=m_head)
			{
				pnodetemp= pnodetemp->m_next;
			}
			pnodetemp->m_next = pnode;
			pnode->m_next=m_head;
			m_pTail=pnode;
		}
	}
	bool insertNode(int index ,T val)
	{
		node * ptemp= m_head;
		if(index<=m_isize-1)
		{
			int ix=0;
			node* pnode = new node;
			pnode->data = val;
			node* poldnode=ptemp;
			while (index != ix++  && ptemp && ptemp->m_next)
			{
				poldnode=ptemp;
				ptemp = ptemp->m_next;
			}
			if(index != 0)
			{  
				poldnode->m_next =pnode;
				pnode->m_next= ptemp;
			}
			else
			{
				if(m_head==NULL)
					return false;
				node* ptem =m_head;
				while(ptem->m_next != m_head)
				{
					ptem=ptem->m_next;
				}
				pnode->m_next =m_head;
				m_head = pnode;
				ptem->m_next= m_head;
			}
			m_isize++;
			return true;
		}
		else
			return false;
	}

	bool check()
	{
		if (m_head==NULL)
			return false;
		node * pfast = m_head->m_next;
		node * pslow = m_head;

		while ( pfast!=pslow  && pfast->m_next!=NULL )
		{
			pfast=pfast->m_next;
			pslow=pslow->m_next;
		}
		if(pfast->m_next!=NULL)
			return true;
		else
			return false;
	}
	void  pop()
	{
		if(m_head!=NULL)
		{
			node *ptem = m_head->m_next;
			delete m_head;
			if(ptem != m_head)
			{
				m_head=ptem;
			}
			else
			{
				m_head=NULL;
				m_pTail=NULL;
			}
			if(m_pTail !=NULL)
				m_pTail->m_next=m_head;

		}
	};

	void foront(T& Val)
	{
		if(m_head!=NULL)
		{
			Val=m_head->data;
			pop();
		}
	}
};
#define N 5
template
class MyStack
{
public:
	MyStack()
	{
		m_iCout=0;
	}
	void pushStack(T val)
	{
		if(m_iCout>=N)
			return ;
		m_arr[m_iCout++]=val;
	}
	bool popFromStack(T& val)
	{
		if(m_iCout>N-1)
			return  false;
		val=m_arr[m_iCout--];
	}
protected:
private:
	int m_iCout;
	T m_arr[N];
};
template
class myQueueByList
{
private:
	mylist m_CircleQueueByList;
public:
	void enqueue(T Val)
	{
		m_CircleQueueByList.push_back(Val);
	}
	void dequeue(T& Val)
	{
		m_CircleQueueByList.foront(Val);
	}
	void show()
	{
		m_CircleQueueByList.show();
	}
};
template
class myCircleQueue
{
public:
	myCircleQueue()
	{
		 m_itail=0;
		 m_ihead=0;
	}
public:
	bool enqueue(T val)
	{
		if((m_itail+1)%N == m_ihead)//queue over
			return false;
	    m_arr[m_itail++]=val;
		m_itail=m_itail%N;
		return true;
	}
	bool outqueue(T& val)
	{
		if(m_itail==m_ihead)
			return false;
		val= m_arr[m_ihead++];
		m_ihead=m_ihead%N;
		return true;
	}
	void ShowQueue()
	{
		cout <<"显示队列开始"<
class myCircleQueueZ
{
public:
	myCircleQueueZ()
	{
		m_itail=0;
		m_ihead=0;
	}
public:
	bool enqueue(T val)
	{
		m_enterCritical.Lock();
		if((m_itail+1)%NZ == m_ihead)//queue over
		{
			m_enterCritical.Unlock();
		    return false;
		}
		m_arr[m_itail++]=val;
		m_enterCritical.Unlock();
		return true;
	}
	bool outqueue(T& val)
	{
		m_outCritical.Lock();
		if(m_itail==m_ihead)
		{	
			m_outCritical.Unlock();
			return false;
		}
		val= m_arr[m_ihead++];
		m_outCritical.Unlock();
		return true;
	}
	void ShowQueue()
	{
		cout <<"显示队列开始";
		for (int i=0 ;i cirZ;
void ProductThread1(void* p)
{
	int istart=0;
    int i=istart;
	while(i mlist;
	int index =1;

	mlist.push_back(1);
	mlist.push_back(2);
	mlist.push_back(3);
	mlist.push_back(4);
	mlist.push_back(5);
	mlist.show();
	cout<<"****"< SL;
	SL.push_back(0);
	SL.push_back(3);
	SL.push_back(5);
	SL.show();
	cout<<"****"< cir;
	cir.enqueue(1);
	cir.enqueue(2);
	cir.enqueue(3);
	cir.enqueue(4);
	cir.enqueue(5);

	cir.ShowQueue();
	int a;
	cir.outqueue(a);
	cout<<"a:"< m_s;
	for (int k=0;k<100;k++)
	{
		m_s.enqueue(k);
	}
	m_s.show();
	int af;
	for (int j=0;j<200;j++)
	{
		m_s.dequeue(af);
		cout<< "af:"<  Circlelist;
	for (int i=0;i< 4;i++)
	{
		Circlelist.push_back(i);
	}
	Circlelist.show();
	Circlelist.insertNode(0,100);
	Circlelist.insertNode(4,101);
	Circlelist.show();
#endif
#pragma endregion 环形链表
#pragma region 链式环形队列
#if SelTest==6
	myCircleQueueByList qlist;
	for (int i=0; i<4 ;i++)
	{
		qlist.EnterQueue(i);
	}
	qlist.show();
	int as;
	qlist.dequeue(as);
	cout<<"as:" << as<< " ";
	qlist.dequeue(as);
	cout<<"as:" << as<< " ";
	qlist.dequeue(as);
	cout<<"as:" << as<< " ";
	qlist.dequeue(as);
	cout<<"as:" << as<< " ";
	qlist.dequeue(as);
	cout<<"as:" << as<< " "<

 

你可能感兴趣的:(数据结构)