提示:
成员函数为
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;
}