Click here to Skip to main content
15,037,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();}
Posted
Updated 3-Jan-17 23:33pm
v2
Comments
Richard MacCutchan 2-Jan-17 12:35pm
   
Any clue what the problem is?
[no name] 2-Jan-17 18:12pm
   
Time to get out the debugger isn't it?
mayashah 2-Jan-17 21:29pm
   
basically output doesn't seem to match what is desired !!
mayashah 2-Jan-17 21:30pm
   
it would launch in following manner as we all like to see !!!
20
30
40
//dequeue function called 20 removed !!!
30
40 !!!

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;
    }
}
   
Comments
nv3 4-Jan-17 5:24am
   
I guess he wants a queue, not a stack, Jochen.
Jochen Arndt 4-Jan-17 5:38am
   
You may be right. It is not clear what he expects to happen.

At least I found a memory leak which justifies posting an answer.
mayashah 4-Jan-17 12:26pm
   
actually yeah i want queue not stack !! what would be deal than
Jochen Arndt 4-Jan-17 12:57pm
   
But then entering 20, 30, 40 and dequeueing should give 30, 40.
Did you get anything else?
I have not compiled the code, but it should give the above output.
mayashah 4-Jan-17 13:00pm
   
yeah it should i am also confused about this stuff it is not giving such output as all wanted to see !!
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:
C++
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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900