面对链表队列的问题 [英] Facing problem with linked list queue

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

问题描述

你好!!我在我的代码中面临的问题基本上我正在使用队列技术+使用模板实现链接列表,但问题在于,当我实现浮点数和字符串类型时,它显示了我完美的答案,但它不适用于int类型我我在这里写入入队和出队功能以获得通关!!



我尝试了什么:



 #include< iostream> 
#include< string>
using namespace std;

模板< typename类型>
struct Node
{
类型信息;
节点<类型> *下一个;
};
模板< typename类型>
void Queue< Type> :: Enqueue(Type x)
{
Node< Type> *温度;
temp = new Node< Type> ;;
temp-> info = x;
temp-> Next = NULL;
if(front == NULL)
{
front = temp;
}
else
{
rear-> Next = temp;
}

rear = temp;

}





模板< typename类型> 
void Queue< Type> :: Dequeue()
{
Node< Type> *温度;
temp = new Node< Type> ;;

if(front == NULL)
{
cout<< 队列是空的。<< ENDL;
}
其他
{
temp = front;
front = front-> Next;
cout<< endl<< temp-> info<< 已经出局了。 << ENDL;
删除临时;
}
}





 int main()
{

cout<< ============== Int Queue ==============<< ENDL;
Queue< int> intqueue;
intqueue.Enqueue(20);
intqueue.Enqueue(30);
intqueue.Enqueue(40);
intqueue.Print();
intqueue.Dequeue();
intqueue.Print();}

解决方案

我将通过在您的代码中添加注释来回答:

模板< typename类型> 
void Queue< Type> :: Dequeue()
{
Node< Type> *温度;
//永远不会删除此处创建的实例:内存泄漏。
//只需删除此行即可。
temp = new Node< Type> ;;

if(front == NULL)
{
cout<< 队列是空的。<< ENDL;
}
其他
{
//你在这里删除前面。
//删除示例会话中的20。
//如果你想删除最后一项,你应该使用后方:
// temp = rear;
// rear = rear-> Prev;
//后 - >下一个= NULL;
temp = front;
front = front-> Next;
cout<< endl<< temp-> info<< 已经出局了。 << ENDL;
删除临时;
}
}


如果您使用调试器,您很快就会看到错误。你真的应该熟悉它。



我在你的代码中看到的是你没有更新前面后方。例如:后面指向空队列的是什么?可能是NULL,我假设。那么在插入第一个元素时, Enqueue 会发生什么?您正在使用读取指针(为空)并执行:

 rear-> Next = temp ; 



将产生未定义的结果。同样在 Dequeue 中:当你删除最后一个元素时会发生什么?在这种情况下,应设置为NULL。我在你的代码中没有看到。所以你再次引发了未定义的行为。


hello !! i am facing little problem here in my code basically i am implementing linked list using queue technique + using templates but the problem in that is when i implement float and string types it shows me perfect answer but it is not working for "int" type i am writing enqueue and dequeue functions here for clearance !!

What I have tried:

#include <iostream>
#include <string>
using namespace std;

template <typename Type>
struct Node
{
	Type info;
	Node<Type> *Next;
};
template <typename Type>
void Queue<Type>::Enqueue(Type x)
{
	Node<Type> *temp;
	temp = new Node<Type>;
	temp->info = x;
	temp->Next = NULL;
	if (front == NULL)
	{
		front = temp;
	}
	else
	{
		rear->Next = temp;
	}

	rear = temp;

}



template <typename Type>
void Queue<Type>::Dequeue()
{
	Node<Type> *temp;
	temp = new Node<Type>;

	if (front == NULL)
	{
		cout << " Queue is Empty. " << endl;
	}
	else
	{
		temp = front;
		front = front->Next;
		cout << endl << temp->info << " was dequeued." << endl;
		delete temp;
	}
}



int main()
{

	cout << "============== Int Queue ==============" << endl;
	Queue<int>intqueue;
	intqueue.Enqueue(20);
	intqueue.Enqueue(30);
	intqueue.Enqueue(40);
	intqueue.Print();
	intqueue.Dequeue();
	intqueue.Print();}

解决方案

I will answer by adding comments to your code:

template <typename Type>
void Queue<Type>::Dequeue()
{
    Node<Type> *temp;
    // The instance created here is never deleted: Memory leak.
    // Just remove this line.
    temp = new Node<Type>;

    if (front == NULL)
    {
        cout << " Queue is Empty. " << endl;
    }
    else
    {
        // You are removing the front here.
        // So 20 from your example session is removed.
        // If you want to remove the last item you should use rear instead:
        //  temp = rear;
        //  rear = rear->Prev;
        //  rear->Next = NULL;
        temp = front;
        front = front->Next;
        cout << endl << temp->info << " was dequeued." << endl;
        delete temp;
    }
}


If you would use the debugger you would quickly see what's wrong. You should really get acquainted to use it.

What I can see in your code is that you don't update front and rear consequently. For example: What does rear point to in an empty queue? Probably NULL, I assume. So what happens in Enqueue when you are inserting the first element? You are using the read pointer (which is null) and do:

rear->Next = temp;


which will produce undefined results. Similarly in Dequeue: What happens when you remove the last element? In that case front and rear should be set to NULL. I don't see that in your code. So again you are provoking undefined behavior.


这篇关于面对链表队列的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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