// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <stdlib.h>

#include "list.h"

//**********************************************************************
//
//  Class LinkList
//
//**********************************************************************

typedef struct list_elm struct_list_elm;

// constructor
LinkList::LinkList()
{
   NElm = 0;
   Bottom = NULL;
   Top = NULL;
}

// destructor
LinkList::~LinkList()
{
}

// insert at the bottom
void LinkList::InsertBottom(void *p)
{
   // allocate new element
   struct list_elm *tmp = new struct_list_elm;
   tmp->Data = p;
   NElm++;
   if(Bottom==NULL) {
      // first
      tmp->Next = NULL;
      tmp->Prev = NULL;
      Bottom = tmp;
      Top = tmp;
   } else {
      // not first
      tmp->Next = Bottom;
      tmp->Prev = NULL;
      Bottom->Prev = tmp;
      Bottom = tmp;
   }
   return;
}

// insert at the top
void LinkList::InsertTop(void *p)
{
   // allocate new element
   struct list_elm *tmp = new struct_list_elm;
   tmp->Data = p;
   NElm++;
   if(Bottom==NULL) {
      // first
      tmp->Next = NULL;
      tmp->Prev = NULL;
      Bottom = tmp;
      Top = tmp;
   } else {
      // not first
      tmp->Next = NULL;
      tmp->Prev = Top;
      Top->Next = tmp;
      Top = tmp;
   }
   return;
}

// remove from the bottom
BOOL LinkList::RemoveBottom()
{
   if(Bottom==NULL) return FALSE;
   struct list_elm *tmp = Bottom->Next;
   delete Bottom;
   Bottom = tmp;
   if(Bottom==NULL) Top=NULL;
   NElm--;
   return TRUE;
}

// remove from the top
BOOL LinkList::RemoveTop()
{
   if(Top==NULL) return FALSE;
   struct list_elm *tmp = Top->Prev;
   delete Top;
   Top = tmp;
   if(Top==NULL) Bottom=NULL;
   NElm--;
   return TRUE;
}

// get from the bottom
void *LinkList::GetBottom()
{
   if(Bottom==NULL) return NULL;
   return Bottom->Data;
}

// get from the top
void *LinkList::GetTop()
{
   if(Top==NULL) return NULL;
   return Top->Data;
}

// turn list
void LinkList::Turn()
{
   struct list_elm *curr,*tmp;
   curr = Bottom;
   for(int i=0;i<NElm;i++) {
      tmp = curr->Next;
      curr->Next = curr->Prev;
      curr->Prev = tmp;
      curr = tmp;
   }
   tmp = Top;
   Top = Bottom;
   Bottom = tmp;
   return;
}

// operators
void *LinkList::operator[](int ix)
{
   struct list_elm *p = Top;
   for(int i=0;i<ix;i++) {
      if(p==NULL) return NULL;
      p = p->Prev;
   }
   if(p==NULL) return NULL;
   return p->Data;
}

//**********************************************************************
//
//  Class Queue
//
//**********************************************************************

// constructor
Queue::Queue() : LinkList()
{
}

// destructor
Queue::~Queue()
{
}

// enqueue
void Queue::EnQueue(void *p)
{
   InsertBottom(p);
   return;
}

// dequeue
void *Queue::DeQueue()
{
   void *p = GetTop();
   RemoveTop();
   return p;
}

//**********************************************************************
//
//  Class Stack
//
//**********************************************************************

// constructor
Stack::Stack() : LinkList()
{
}

// destructor
Stack::~Stack()
{
}

// push
void Stack::Push(void *p)
{
   InsertTop(p);
   return;
}

// pop
void *Stack::Pop()
{
   void *p = GetTop();
   RemoveTop();
   return p;
}
