Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,231 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,858 people online right now. Registration is fast and FREE... Join Now!




Queues

 
Reply to this topicStart new topic

Queues, Enqueue Function

wannabecoder
15 Mar, 2007 - 05:24 PM
Post #1

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 2


My Contributions
Hi.

I am having a hard time with this function. I have built a linked list class and it works. Here is what I have of my enqueue function. When I run to test it, it will enqueue the first value but not the one after it. Am I doing somethin wrong?? Can anyone please help!!! crazy.gif

CODE
node *temp;

    temp = new node( v, rear );    
                if( empty( ) )
    {
        front = rear = temp;
    }
    else
    {
        temp->next = rear;
        rear->next = temp;
        rear=temp;

    }



This post has been edited by wannabecoder: 15 Mar, 2007 - 05:27 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Queues
15 Mar, 2007 - 05:55 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,013



Thanked: 18 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
the logic is a little a miss, if you draw this out:

temp.next -> rear.next -> temp (which is a circle not a linked list)
*this could work, except this is all you ever get, adding another element will simply replace rear and temp in the same pattern.

How are you trying to implement this as a cycle, or as a linked list with a null on the end to terminate?
Also a little more code, such as the function declaration and the node struct would help.
User is offlineProfile CardPM
+Quote Post

wannabecoder
RE: Queues
15 Mar, 2007 - 06:00 PM
Post #3

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 2


My Contributions
QUOTE(William_Wilson @ 15 Mar, 2007 - 06:55 PM) *

the logic is a little a miss, if you draw this out:

temp.next -> rear.next -> temp (which is a circle not a linked list)
*this could work, except this is all you ever get, adding another element will simply replace rear and temp in the same pattern.

How are you trying to implement this as a cycle, or as a linked list with a null on the end to terminate?
Also a little more code, such as the function declaration and the node struct would help.


Wow. I wasn't trying to use a circular list. Here is the struct for the node and the function prototype.

CODE
class node
{
     public:
            int data;
            node* next;
            node( int x, node *a )
            {
                   data = x;
                   next = a;
           }
};

void Enqueue( int );


So, am I not updating the right node each time it enqueues?


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month