c++ 先进先出 请指点不足

yuyutao1 2010-09-01 07:20:40
c++新人 求高手指点 不管什么方面的不足请指出

想通过这个例子多多了解c++的编程习惯和语法
#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;
}
}

...全文
308 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
willabc 2010-09-03
  • 打赏
  • 举报
回复
学习一下!
sallan 2010-09-03
  • 打赏
  • 举报
回复
楼主如果担心内存问题,最好使用auto_ptr
c++编程,一定会使用内存,所以内存的管理尤为重要
job82824 2010-09-03
  • 打赏
  • 举报
回复
学习了,帮顶接分
duruo850 2010-09-03
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class my_queue{ // 命名规范:CMyQueue,实际应用方便
struct node{
int i;
node* next;

};
/*其他类似
typedef struct tagNode
{
int nValue;
CNode* pNextNode;
}NODE
*/

/*构造函数
CMyQueue();
~CMyQueue()
{
// 需要释放内存
delete pNextNode;
//类似这样,但是要循环删除
}
CMyQueue(int nValue);
*/
private:
node* first;
node* last;

/*其他类似
tagNode* pFirstNode;
tagNode* pLastNode;
*/
public:
void put(int i);
int get();
my_queue(){
first=0;
last=0;

/*指针是这样初始化的,指向空
pFirstNode = NULL;
*/
}

};
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;
}

/*if可以直接对指针判断,缩进问题,其他类似
if(pFirstNode)
{
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;
}
}
ForestDB 2010-09-03
  • 打赏
  • 举报
回复
挺好。
yuyutao1 2010-09-03
  • 打赏
  • 举报
回复
根据各位指出的不足稍微更正了一下 增加了析构函数 ,解决了内存泄露问题 应该解决掉了 第一次用!
#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
happynwt 2010-09-01
  • 打赏
  • 举报
回复
没什么大问题,STL本身是有管理内存的,实际一般不delete,以备以后申请。这里加上NULL指针判断和内存管理就基本OK。
xiaolen2010 2010-09-01
  • 打赏
  • 举报
回复
学习了
lzpkshy 2010-09-01
  • 打赏
  • 举报
回复
学习了。。。
AlanBruce 2010-09-01
  • 打赏
  • 举报
回复
kankan..
renzhewh 2010-09-01
  • 打赏
  • 举报
回复
多看书、源码

c语言接口与实现
STL源码剖析
yyg990441 2010-09-01
  • 打赏
  • 举报
回复
主要问题是:
1.没有析构函数,new出来的空间没有delete掉(即内存泄露)
2.int my_queue::get()中,单单first=first->next;不行,这样导致原来的first指向的内存无法被释放
guoxuqu 2010-09-01
  • 打赏
  • 举报
回复
node* temp=new node();
temp->i=i;
temp->next= NULL;//加上这句

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧