SOL9 2.0 Class: Queue

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * Queue.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
#pragma once

#include <sol\Collection.h>

#include <sol\ListEntry.h>


namespace SOL {

class Queue :public Collection {
private:
    ListEntry* head;
    ListEntry* tail;

public:
    Queue()
        :head(NULL), tail(NULL)
    {
    }

public:
    ~Queue()
    {
        //Pending
    }

public:
    void addLast(Object* obj) 
    {
        ListEntry* ent = new ListEntry(obj);
        if(head == NULL) {
            head = ent;
            tail = ent;
            return;
        }

        if(tail) {
            tail -> setNext(ent);
            tail = ent;
        }
    }

public:
    void addFirst(Object* obj) 
    {
        ListEntry* ent = new ListEntry(obj);
        if(head == NULL) {
            head = ent;
            tail = ent;
            return;
        }

        if(head) {
            ent -> setNext(head);
            head = ent;
        }
    }

public:
    ListEntry*  get() 
    {
        if(head) {
            ListEntry* ent = head;
            head = head -> getNext();
            return ent; 
        }
        else {
            return NULL;
        }
    }


public:
    ListEntry*  peek() 
    {
        if(head) {
            ListEntry* ent = head;
    //        head = head -> GetNext();
            return ent; 
        }
        else {
            return NULL;
        }
    }

public:
    int  getLength() 
    {
        int n = 0;
        if(head==NULL && tail== NULL) {
            return n;
        }
        if(head) {
            ListEntry* ent = head;
            while(ent) {
                ent = ent -> getNext();
                n++;
            }
        }
        return n;
    }
};

}


Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.