Tuesday 20 March 2012

Double linked list with templates

#ifndef __TQUEUE_H__
#define __TQUEUE_H__
/* template signatures:
1- all related classes and types will have the signature
2- the name of the class right after the template will NOT have the signature
3- the constructor names will NOT have the signature
4- the destructor name will NOT have the signature
*/

/* Tqueue requirements:
1- Template class must be copied safely.
2- Template class must work with operator<

*/

template <class type>
class Tqueue;

template <class type>
class Tqnode{
Tqnode<type>* _next;
Tqnode<type>* _prev;
type _data;
Tqnode(type data,
Tqnode<type>* prev = (Tqnode<type>*)0,
Tqnode<type>* next = (Tqnode<type>*)0):_data(data){
_next = next;
_prev = prev;
}
friend class Tqueue<type>;
};

template <class type>
class Tqueue{
    Tqnode<type>* _head;
    Tqnode<type>* _curr;
    Tqnode<type>* _tail;
public:

    Tqueue(){
        _head = _tail = _curr = (Tqnode<type>*)0;
    }

    virtual ~Tqueue(){
        goHead();
        while(_curr){
            _head = _curr;
            _curr = _curr->_next;
            delete _head;
        }
    }
bool goNext();
bool goPrev();
bool goHead();
bool goTail();


bool isEmpty(){
return !_curr;
}


void append(type data){ // appends to tail
    Tqnode<type>* temp= new Tqnode<type>(data, _tail);
    if(_curr){
        _tail = temp;
        _tail->_prev->_next = _tail;
        // _temp->_prev->_next = _temp;
    }
    else{
        _curr = _head = _tail = temp;
    }
}


type remove(){ // removes the data from the head of the list
    if(_head == _tail){
        type data = _head->_data;
        delete _head;
        _head = _tail = _curr = (Tqnode<type>*)0;
        return data;
    }
    else{
        type data = _head->_data;
        Tqnode<type>* todel = _head;
        if(_head == _curr){
                _curr = _curr->_next;
        }
        _head = _head->_next;
        _head->_prev = (Tqnode<type>*)0;
        delete todel;
        return data;
    }
}

void insertBefore(type data) { // before current (prev side)
    Tqnode<type>* preadd = new Tqnode<type>(data);
    if(_curr) {
        preadd->_prev=_curr->_prev;
        preadd->_next=_curr;
    if(_head!=_curr) {
        _curr->_prev->_next = preadd;
    }
    else {
        _head = preadd;
    }
    _curr->_prev = preadd;
    }
    else {
            _curr = _head = _tail = preadd;
    }
}


void insertAfter(type data) { // after current (next side)
    Tqnode<type>* nextadd = new Tqnode<type>(data);
    if(_curr) {
            nextadd->_prev = _curr;
            nextadd->_next = _curr->_next;
            if(_curr != _tail) {
                        _curr->_next->_prev= nextadd;
            }
            else {
                        _tail = nextadd;
            }
            _curr->_next= nextadd;
    }
    else {
            _curr = _head = _tail = nextadd;
    }
}


type visit(){ // returs the data of the current
    return _curr->_data;
}

type removeCurrent() { // removes curent
        Tqnode<type>* todel = _curr;
        type data = _curr->_data;
        if(_head != _tail) {
                    if(_head == _curr) {
                            _curr->_next->_prev = (Tqnode<type>*) 0;
                            _curr = _curr->_next;
                            _head = _curr;
                    }
                    else if(_curr == _tail) {
                            _curr->_prev->_next = _curr->_next;
                            _curr = _curr->_prev;
                            _tail = _curr;
                    }
                    else {
                            _curr->_next->_prev = _curr->_prev;
                            _curr->_prev->_next = _curr->_next;
                            _curr = _curr->_next;
                    }
        }
        else {
                _head = _tail = _curr = (Tqnode<type>*)0;
        }
    delete todel;
    return data;
}


bool sort(bool Ascending = true) { // sorts the nodes
    int ret = 0;
    if(Ascending && _curr) {
                            Tqnode<type>* seq = _head;
                            int i=0;
                            type data = 0;
                            while(seq->_next) {
                                        seq = seq->_next;
                                        i++;
                            }
                            seq = _head;
                            for(int j=0; j <= i; j++){
                                        for(int k=0; k < i; k++) {
                                                    if(seq->_data > seq->_next->_data)
                                                                    {
                                                                        data = seq->_data;
                                                                        seq->_data = seq->_next->_data;
                                                                        seq->_next->_data= data;
                                                                        ret = 1;
                                                                    }
                                                    seq = seq->_next;
                                        }
                            seq = _head;
                            }
    }
    else if(!Ascending && _curr) {
                                  Tqnode<type>* seq = _head;
                                  int i=0;
                                  type data = 0;
                                  while(seq->_next) {
                                        seq = seq->_next;
                                        i++;
                                  }
                                  seq = _head;
                                  for(int j=0; j <= i; j++){
                                            for(int k=0; k < i; k++) {
                                                    if(seq->_data < seq->_next->_data)
                                                                    {
                                                                        data = seq->_data;
                                                                        seq->_data = seq->_next->_data;
                                                                        seq->_next->_data= data;
                                                                        ret = 1;
                                                                    }
                                                    seq = seq->_next;
                                            }
                                            seq = _head;
                                    }
                                }
    return ret;
}


void push(type data) {
        Tqnode<type>* add = new Tqnode<type>(data);
        if(!_curr) {
            _curr = _head = _tail = add;
        }
        else {
            _head->_prev=add;
            add->_next=_head;
            _head = add;
        }
}


type pop() {
    if(_curr) {
            type data = _head->_data;
            Tqnode<type>* del = _head;
            if(_head == _curr) {
                    _curr = _curr->_next;
            }
            _head = _head->_next;
            delete del;
            return data;
    }
}
};

template <class type>
bool Tqueue<type>::goTail(){
bool res = false;
if(_curr){ // is not null
_curr = _tail;
res = true;
}
return res;
}

template <class type>
bool Tqueue<type>::goHead(){
bool res = false;
if(_curr){ // is not null
_curr = _head;
res = true;
}
return res;
}

template <class type>
bool Tqueue<type>::goNext(){
bool res = false;
if(_curr && _curr->_next){
_curr = _curr->_next;
res = true;
}
return res;
}

template <class type>
bool Tqueue<type>::goPrev(){
bool res = false;
if(_curr != _head){
_curr = _curr->_prev;
res = true;
}
return res;
}


#endif

No comments:

Post a Comment