Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm trying to make a dynamic queue using linked list to create the nodes of it, but I have some problems with the method of insertion, at one point the data that I pass to the function looses and it returns trash.

The main problem is in Nodo* PushQ(Nodo *nodo, int val)

The code

C++
#include <iostream>

using namespace std;

// Create new node

struct Nodo{

	int val;
	Nodo *next;

};

class Queue
{
	
	public:
	
		Nodo *nodo, *end, *front; // [front]->[node]->[end]->NULL
		
Queue() // Constructor
{

	nodo = end = front = NULL;

}

Nodo* PushQ(Nodo *nodo, int val) // Insert value
{
	
	if(nodo == NULL) // At this point is all right, but next, it looses the data and pass to val trash
	{
		nodo = new Nodo;
		nodo->val = val; // At this point the data is gone, now it shows trash
		
		front = nodo;       //     front         end 
		front->next = NULL; // [data|*next]->[data|*next]->NULL
		end = front;
	}
	else
	{
		nodo = front->next;
		front = nodo;
		front->next = NULL;
	}
	
	return nodo;
	
}

int PopQ(Nodo *nodo) // Show queue data
{
	
	int aux = 0;
	
	nodo = new Nodo;
	nodo = end;
	
	while(nodo) // Infinite loop
	{
		cout << nodo->val << endl;
		aux = nodo->val;
	}
	
	return aux;
	
}

};

int main()
{
	Queue *Q1 = new Queue;

	Q1->nodo = Q1->PushQ(Q1->nodo, 5);
	Q1->nodo = Q1->PushQ(Q1->nodo, 20);
	Q1->nodo = Q1->PushQ(Q1->nodo, 6);
	Q1->nodo = Q1->PushQ(Q1->nodo, 7);
	Q1->nodo = Q1->PushQ(Q1->nodo, 33);

	Q1->PopQ(Q1->nodo);
	
	return 0;
}


What I have tried:

Some debugging with Visual Studio 2015, but I don't understand why the trash appears
Posted
Updated 30-Jul-16 17:36pm
v5

Try to replace
C++
val = nodo->val; // At this point the data is gone, now it shows trash
with
C++
nodo->val = val;

it should solve the data gone problem.

This syntax is really weird.
C++
Q1->nodo = Q1->PushQ(Q1->nodo, 5);


Otherwise, push is really buggy, a completed analyze and rewrite is in order. The whole program is confuse.
You should think about what you want: a queue, a linked list or a queue of linked lists.

Quote:
Some debugging with Visual Studio 2015

You should really learn to use the debugger.
 
Share this answer
 
v3
Comments
Bit87 30-Jul-16 23:21pm    
I'm trying to make a queue of linked lists, I know that that syntax is really weird, but it works, maybe not the best way but it works :O, thanks for the advice of nodo->val = val, I didn't know that that little syntax move can affect how the code works.
i think you mean nodo->val=nodo

i assume if(nodo = NULL) is a typo? Must be, otherwise you wouldn't enter that clause in the debugger
 
Share this answer
 
Comments
Bit87 30-Jul-16 23:09pm    
Sorry, my mistake, I mean if(nodo == NULL)

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