⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 模板.cpp

📁 c++模板小实验
💻 CPP
字号:
#include<string.h>
#include<stdio.h>
#include<conio.h>
#define null 0
#include <iostream.h>
//enum bool {false,true};

template<class t> //定义 队列中结点类型
struct quenode 
{  t        nodedata;
   quenode  *next;
};

template<class t>
class queue 
{protected :
 int queuesize;
 quenode<t> *head;
 quenode<t> *tail;
 bool allocateerror;
 public:
	 queue();
	 ~queue(){clearque();}
	 void push(t &);
   	 bool  pop(t &);
	 bool isempty()
	 {return (quesize==0)?true:false;}
	 void clearque();
};

	 template<class t>//定义构造函数
		 queue<t>::queue()
	 {queuesize=0;
	 allocateerror=false;
	 head=null;
	 tail=null;
	 }
	 

	 template<class t>//向队尾插入元素
		 void queue<t>::push(t &x)
	 {quenode<t>*p;
	 p=new quenode<t>;
	 if (!p)
	 {allocateerror=true;
	 return;}
	 p->nodedata=x;
	 if(tail)
	 {p->next=null;
	 tail->next=p;
	 tail=p;}
	 else 
	 {p->next=null;
	 tail=p;
	 head=p;}
	 queuesize++;
	 }

	 template<class t>//从队头取一结点
		 bool queue<t>::pop(t &x)
	 {quenode<t> *p;
	  if(head)
      {x=head->nodedata;
	  p=head;
	  head=head->next;
	  if(head==null)
		  tail=null;
	  delete p;
	  queuesize--;
	  return true;
	  }
	  return false;
	 }

template <class t>//将队列清空
void queue<t>::clearque()
{t p;
 allocateerror=false;
 while (pop(p)) ;
	 head=tail=null;
};


void main()
{queue<int> que;
int i;
for( i=1;i<6;i++)
{que.push(i);
cout<<"压入队列元素i:"<<i<<endl;}
int a;
bool b;
for( i=1;i<6;i++)
{b=que.pop(a);
if(b)
cout<<"弹出队首元素a:"<<a<<endl;}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -