简单的队列类错误 [英] Simple queue class error

查看:86
本文介绍了简单的队列类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的队列类

头文件:Queue.h



  #ifndef QUEUE_H 
#define QUEUE_H
struct 节点
{
int 数据;
节点*链接;
};
typedef 节点* nodeptr;
class 队列
{
public
Queue ();
~Queue();
void add( int number);
int remove(); // 删除队列的顶部节点并返回其值
bool empty() const ;
受保护
nodeptr front;
nodeptr返回;
};
#endif // QUEUE_H





实现文件:Queue.cpp



  #include     Queue.h 
#include < < span class =code-leadattribute> cstdlib >
#include < iostream >
#include < cstddef >
使用 命名空间标准;
Queue :: Queue():front(NULL),back(NULL)
{}
Queue :: ~Queue()
{
int next;
nodeptr discard;
discard = front;
while (discard!= NULL)
{
next = remove();
discard = front;
}
}
void Queue :: add( int number )
{
if (empty())
{
front = new 节点;
front-> data = number;
front-> link = NULL;
back = front;
}
else
{
nodeptr temp;
temp = new 节点;
temp-> data = number;
temp-> link = NULL;
back-> link = temp;
back = temp;
}
}
int Queue :: remove()
{
if (empty())
{
cout<< 从空队列中删除错误\ n;
退出( 1 );
}
else
{
int result =前置式>数据;
nodeptr discard;
discard = front;
front = front-> link;
if (front == NULL)
{
back = NULL;
}
delete discard;
返回结果;
}
}
bool Queue :: empty() const
{
return (front == NULL);
}





我有两个版本的主程序基本相同,但产生不同的结果,尽管事实上,课程没有受到影响。

主要版本1-输出:4321



  #include     Queue.h 
#include < span class =code-keyword>< iostream >
使用 命名空间标准;

int main()
{
Queue a;
a.add( 1 );
a.add( 2 );
a.add( 3 );
a.add( 4 );
cout<< a.remove()<< a.remove()<< a.remove()<< a.remove();
}





主版本2输出:

1

2

3

4



  #include    < span class =code-string> Queue.h 
#include < iostream >
使用 命名空间标准;
int main()
{
Queue a;
a.add( 1 );
a.add( 2 );
a.add( 3 );
a.add( 4 );
cout<< a.remove()<< endl;
cout<< a.remove()<< endl;
cout<< a.remove()<< endl;
cout<< a.remove()<< endl;
}







我的尝试:



我在两个版本之间进行了数百次更改,但结果没有变化。我在这里做错了什么?

解决方案

C语言编译器是一个重写器。这意味着编译器决定在单行源代码中评估部件的顺序。

 cout<< a.remove()<< a.remove()<< a.remove()<< a.remove(); 

编译器发现以相反的顺序进行评估更有效;

你可以用这个来检查:

 cout<< a.remove()+ 10<< a.remove()+ 20<< a。除去()+ 30℃;< a.remove()+ 40; 


I have a very simple queue class
Header file: Queue.h


#ifndef QUEUE_H
#define QUEUE_H
struct Node
{
	int data;
	Node* link;
};
typedef Node* nodeptr;
class Queue
{
public:
	Queue();
	~Queue();
	void add(int number);
	int remove();//removes the top node of the queue and return its value
	bool empty() const;
protected:
	nodeptr front;
	nodeptr back;
};
#endif //QUEUE_H 



Implementation file: Queue.cpp


#include "Queue.h"
#include <cstdlib>
#include <iostream>
#include <cstddef>
using namespace std;
Queue::Queue():front(NULL),back(NULL)
{}
Queue::~Queue()
{
	int next;
	nodeptr discard;
	discard=front;
	while (discard!=NULL)
	{
		next=remove();
		discard=front;
	}
}
void Queue::add(int number)
{
	if (empty())
	{
		front=new Node;
		front->data=number;
		front->link=NULL;
		back=front;
	}
	else
	{
		nodeptr temp;
		temp=new Node;
		temp->data=number;
		temp->link=NULL;
		back->link=temp;
		back=temp;
	}
}
int Queue::remove()
{
	if (empty())
	{
		cout<<"error removing from an empty queue\n";
		exit(1);
	}
	else
	{
		int result=front->data;
		nodeptr discard;
		discard=front;
		front=front->link;
		if (front==NULL)
		{
			back=NULL;
		}
		delete discard;
		return result;
	}
}
bool Queue::empty() const
{
	return (front==NULL);
}



I have two versions of main program which are basically the same but produce different results despite the fact that the class is untouched.
Main version 1- output: 4321


#include "Queue.h"
#include<iostream>
using namespace std;

int main()
{
	Queue a;
	a.add(1);
	a.add(2);
	a.add(3);
	a.add(4);
	cout<<a.remove()<<a.remove()<<a.remove()<<a.remove();
}



Main version 2-output:
1
2
3
4


#include "Queue.h"
#include<iostream>
using namespace std;
int main()
{
	Queue a;
	a.add(1);
	a.add(2);
	a.add(3);
	a.add(4);
	cout<<a.remove()<<endl;
	cout<<a.remove()<<endl;
	cout<<a.remove()<<endl;
	cout<<a.remove()<<endl;
}




What I have tried:

I have change between either versions hundreds of times but the results are unchanged. What did I do wrong here?

解决方案

A C language compiler is a rewriter. This means that the compiler decide the order of evaluation of the parts in a single line of source code.

cout<<a.remove()<<a.remove()<<a.remove()<<a.remove();

The compiler find it is more efficient to evaluate in reverse order;
You can check with this:

cout<<a.remove()+10<<a.remove()+20<<a.remove()+30<<a.remove()+40;


这篇关于简单的队列类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆