自编模板类+链表实现栈、队列

#include<iostream>
using namespace std;

template<class numtype>
struct pnode
{
	numtype date;
	pnode*  p;

	pnode(numtype e)
	{p=NULL; date=e;}
};

//山寨stack
template<class numtype>
class szStack
{
private:
	pnode<numtype>* head;
public:
	void push(numtype e)
	{  
		pnode<numtype>* point=new pnode<numtype>(e);
		point->p=head;
		head=point;
	}
	numtype top() 
	{
		return head->date;
	}
	void pop()
	{
		pnode<numtype>* point=head;
		head=head->p;
		delete point;
		point=NULL;
	}
	bool empty()
	{
		if(head)
			return false;
		return true;
	}

	szStack()
	{head=NULL; }
};



struct node
{
	int x,y;
};

int main()
{
    node pp;
	int i;
	szStack<node>ss;
	for(i=1;i<99;i++)
	{
		pp.x=i+1;
		pp.y=i*2;
		ss.push(pp);
	}
    while(!ss.empty())
	{ 
		pp=ss.top();
		cout<<pp.x<<' '<<pp.y<<"    ";
		ss.pop();
	}
   

	cout<<"\n\n\n";

	szStack<int>sint;
	for(i=1;i<99;i++)
	{
		sint.push(i);
	}
    while(!sint.empty())
	{ 
		cout<<sint.top()<<"   ";
		sint.pop();
	}
	
	system("pause");
}

//

 

#include<iostream>
using namespace std;

template<class numtype>
struct pnode
{
	numtype date;
	pnode* p;
	pnode(numtype e)
	{date=e;p=NULL;}
};

template<class numtype>
class myQueue
{
private:
	pnode<numtype>* head,*tail;
	int size;
public:	
	void push(numtype e)
	{
		if(size==0)
		{
			head=tail=new pnode<numtype>(e);
		}
		else
		{
			tail->p=new pnode<numtype>(e);
			tail=tail->p;
		}
		size++;
	}

	numtype front()
	{
		return head->date;
	}
	void pop()
	{
		pnode<numtype>* tempoint=head;
		head=head->p;
		delete tempoint;
		tempoint=NULL;
		size--;

	}
    bool empty()
	{
		if(size==0)
			return true;
		return false;
	}
	int getSize()
	{
		return size;
	}

	myQueue()
	{
		head=tail=NULL;
		size=0;
	}

};



struct node
{
	int x,y;
};



int main()
{ 
	node pp;
	int i;
	myQueue<node> ss;  
	
	cout<<ss.getSize()<<endl;
	for(i=1;i<99;i++)
	{
		pp.x=i+1;
		pp.y=i*2;
		ss.push(pp);
	}

    cout<<ss.getSize()<<endl;

	while(!ss.empty())
	{ 
		pp=ss.front();
		cout<<pp.x<<' '<<pp.y<<"    ";
		ss.pop();
	}

    cout<<endl<<ss.getSize()<<endl;


	getchar();

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值