编写一个类,实现简单的栈(提示:用链表结构实现)。数据的操作按先进后出(FILO)的顺序。

提示:

成员函数为

  void queue::put(int item);  //将数据item插入到栈中

  int queue::get();  //从栈中取数据

数据成员为

  一个指向链首的指针

链表结构为

  struct Node

  {

  int a;

  Node* next;

  }

对象使用过程

  queue que;

  que.put(10);

  que.put(12);

  que.put(14);

  cout<<que.get()<<endl;//输出14,栈中剩下10,12

  cout<<que.get()<<endl;//输出12,栈中剩下10

#include<iostream>
using namespace std;
struct Node
{
	int a;
	Node* next;
};
class queue
{
	public:
		void put(int item);     //压栈
		int get();      //出栈
    private:
    	Node *head;
};
void queue::put(int item)
{
	Node *p;
	p=new Node;
	p->a=item;
	p->next=head;
	head=p;
}
int queue::get()
{
	if(head==NULL)
	{
		cout<<"Error return ";
		return 0;
	}
	Node *p=head;
	int temp=head->a;
	head=head->next;
	delete p;
	return temp;
}
int main()
{
	queue que;
	que.put(10);
	que.put(12);
	que.put(14);
	cout<<que.get()<<endl;
	cout<<que.get()<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值