64,639
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class my_queue{
struct node{
int i;
node* next;
};
private:
node* first;
node* last;
public:
void put(int i);
int get();
my_queue(){
first=0;
last=0;
}
};
void my_queue::put(int i){
node* temp=new node();
temp->i=i;
if(first==0){
first=temp;
first->next=0;
last=first;
}
else{
last->next=temp;
last=temp;
}
}
int my_queue::get(){
if(first==0){//c++异常还不太了解 不清楚怎么处理
cerr<<"空值";
return -1;
}
else{
int temp=first->i;
first=first->next;
return temp;
}
}
#ifndef MY_QUEUE_______
#define MY_QUEUE_______
#include <iostream>
using namespace std;
class My_queue{
struct Node{
int i;
Node* next;
/* ~Node(){
cout<<"析构 node ...."<<endl;
}
*/
};
private:
Node* first;
Node* last;
public:
int has_next();
void put(int i);
int get();
~My_queue();//析构函数
My_queue(){
first=0;
last=0;
}
};
My_queue::~My_queue(){
while(!(has_next())){
get();
}
}
void My_queue::put(int i){
Node* temp=new Node();
temp->i=i;
temp->next=0;
if(first==0){
first=temp;
last=first;
}
else{
last->next=temp;
last=temp;
}
}
int My_queue::get(){
if(first==0){//c++异常还不太了解 不清楚怎么处理
cerr<<"空值";
return -1;
}
else{
int temp=first->i;
Node *node_temp=first->next;
delete(first);//释放内存
first=node_temp;
return temp;
}
}
int My_queue::has_next(){
if(first==0){
return 1;
}
else{
return 0;
}
}
#endif