• Welcome to Valhalla Legends Archive.
 

C++/CLI - Strange behaviour in a FIFO queue?

Started by Dyndrilliac, November 16, 2006, 03:22 AM

Previous topic - Next topic

Dyndrilliac

Edit: I feel rather silly now. After lots of thinking, I realized that I had forgotten to append the carot to my class name when retrieving a newly initialized instance. I also realized that my constructor call was ill-formed. The code has been fixed and re-pasted if anyone wants to use the queue.

I'm not sure quite how to describe the bug I'm facing - perhaps I should show you my output.
QuoteValue: SimpleQueue
Value: String
Value: 34.6
Press any key to continue . . .
The first value being displayed seems to have come from absolutely nowhere. SimpleQueue (as you'll see below) is the class name for an object in my program. However, what I don't know is how it found it's way to the output screen. There isn't any reference to the string in my code, and nothing in particular jumps out as the cause (to me). I would appreciate anyone with suggestions on how to best solve the issue.

Here's the code (this is all of it, I have no idea what might be causing the anomaly).
    using namespace System;

    public ref class SimpleQueue {

        private:

            ref struct Node {

                Node^   p;
                Node^   n;
                Object^ o;

                Node() {
                    this->n = nullptr;
                    this->p = nullptr;
                    this->o = nullptr;
                }

                Node(Node^ nn) {
                    this->n = nn->n;
                    this->p = nn->p;
                    this->o = nn->o;
                }

                Node(Node^ np, Node^ nn, Object^ oo) {
                    this->n = nn;
                    this->p = np;
                    this->o = oo;
                }

            };

            Int16 size;
            Node^ head;
            Node^ tail;

        public:

            SimpleQueue() {
                this->Clear();
            }

            void Clear() {
                this->head = gcnew Node(nullptr, tail, nullptr);
                this->tail = gcnew Node(head, nullptr, nullptr);
                this->size = 0;
            }

            void Push(Object^ o) {
                if (this->size >= (Int16::MaxValue - 1)) {
                    throw gcnew Exception("Queue is full.");
                }

                if (this->Empty) {
                    Node^ t = gcnew Node(head, tail, o);
                    this->head->n = t;
                    this->tail->p = t;
                    this->size = 1;
                } else {
                    Node^ g = gcnew Node(head, head->n, o);
                    Node^ k = head->n;
                    k->p = g;
                    this->head->n = g;
                    this->size++;
                }
            }

            Object^ Pop() {
                if (this->Empty) {
                    throw gcnew Exception("Queue is empty.");
                }

                Object^ o = tail->p->o;

                Node^ temp = gcnew Node(tail->p);
                temp = temp->p;
                temp->n = tail;
                tail->p = temp;

                return (o);
            }

            property Int16 Size {
                Int16 get() {
                    return (this->size);
                }
            }

            property bool Empty {
                bool get() {
                    return (this->size == 0);
                }
            }
    };

    int main() {

        SimpleQueue^ sq = gcnew SimpleQueue;

        sq->Push("String");
        sq->Push(34.6);

        long x = sq->Size;

        for (long i = 0; i < x; i++) {
            Console::WriteLine("Value: {0}", sq->Pop());
        }

        return 0;
    }
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

MyndFyre

If you need a FIFO queue, why not just use System.Collections.Generic.Queue<T>?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Dyndrilliac

I never said I was going to use the queue. Besides, I need to know and understand the methodology behind creating a queue of my own.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.