VIZ++ Class: LinkedListT

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * LinkedListT.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */



#pragma once

#include <viz++/List.h>
#include <viz++/ListEntryT.h>
//using namespace VIZ;

/**
 * LinkedListT class reprents a linear singly-linked list.
 */
namespace VIZ {

template <class T> class LinkedListT :public List {
  
private:
  ListEntryT<T>* entry;
  bool    gc;

public:
  LinkedListT(bool gc1=true)
    :entry(NULL), gc(gc1)
  {
  }

public:
  bool  addFirst(T* object)
  {
    bool rc = true;
    entry = new ListEntryT<T>(object, entry);
    if(entry == NULL) {
      rc = false;
    }
    return rc;
  }

public:
  bool add(T* object)
  {
    ListEntryT<T>* ptr  = entry;
    ListEntryT<T>* prev = ptr;

    ListEntryT<T>* newEntry= new ListEntryT<T>(object);
    if (newEntry == NULL) {
      return false;
    }

    if (ptr == NULL) {
      entry = newEntry;
    }
    else {
      while (ptr) {
        prev = ptr;
        ptr  = ptr -> getNext();
      }
      prev -> setNext(newEntry);
    }
    return true;
  }

public:
  bool remove(T* object)
  {
    bool rc = false;
    ListEntryT<T>* ptr  = entry;
    ListEntryT<T>* prev = ptr;

    while (ptr) {
      T* obj = ptr -> getObject();
      if (obj == object && prev == ptr) {
        entry = ptr -> getNext();
        if (gc == false) {
          ptr ->setObject(NULL);
        }
        delete ptr;
        rc = true;
        break;
      }
      if (obj == object && prev != ptr) {
        prev -> setNext(ptr->getNext());
        if (gc == false) {
          ptr ->setObject(NULL);
        }  

        delete ptr;
        rc = true;
        break;
      }
      else {
        prev = ptr;
        ptr  = ptr -> getNext();
      }
    }
    return rc;
  }

public:
  ~LinkedListT()
  {
    clear();
  }

public:
  bool isContained(T* obj)
  {
    bool rc = false;

    ListEntryT<T>* ptr  = entry;

    while (ptr) {
      if (ptr -> getObject() == obj) {
        rc = true;
        break;
      }
      ptr = ptr -> getNext();
    }
    return rc;
  }


public:
  void clear()
  {
    ListEntryT<T>* ptr  = entry;
    ListEntryT<T>* prev = ptr;

    while (ptr) {
      prev = ptr;
      ptr = ptr -> getNext();
      if (gc == False) {
        prev ->setObject(NULL);
      }
      delete prev;
    }
    entry = NULL;
  }

public:
  int getLength() const
  {
    ListEntryT<T>* ptr = entry;
    int n = 0;
    while(ptr) {
      ptr = ptr -> getNext();
      n++;
    }
    return n;
  }

public:
  T* getNth(int n)
  {
    int m = 0;  // Start from 0 not 1 in VIZ++ 3.0

    T* object = NULL;
    ListEntryT<T>* ptr = entry;

    while(ptr) {
      if(m == n) {
        object = ptr ->getObject();
        break;
      }
      ptr = ptr -> getNext();
      m++;
    }
    return object;
  }


// Simple selection sort
public:
  void sort(SortDirection dir)
  {
    int length = getLength();
    int i = 0;
    ListEntryT<T>* ith = entry;
   
    while (i<length-1) {  
      ListEntryT<T>* cth = ith;
      ListEntryT<T>* jth = ith -> getNext();
      T*    obj = ith -> getObject();

      while (jth) {
        T* jthObj = jth->getObject();
        if (dir == ASCENDING) {
          if (jthObj -> compare(obj) > 0) {
            cth = jth;
            obj = jth -> getObject();
          }  
        }
        if (dir == DESCENDING) {
          if (jthObj -> compare(obj) < 0) {
            cth = jth;
            obj = jth -> getObject();
          }  
        }
        jth = jth -> getNext();
      }

      cth -> setObject(ith->getObject());
      ith -> setObject(obj);

      ith = ith -> getNext();
      i++;
    }  // wile
  }

public:
  void reverse()
  {
    ListEntryT<T>* ptr = entry;
    ListEntryT<T>* rev = NULL;
        
    while (ptr) {
      ListEntryT<T>* next = ptr -> getNext();
      ptr -> setNext(NULL);

      if (rev) {
        ptr -> setNext(rev); 
        rev = ptr;
      } else {
        rev = ptr;
      }
      ptr = next;
    }

    entry = rev;
  }


  bool  addLast(T* object) { 
    return add(object); 
  }


  ListEntryT<T>*  getEntry() const { 
    return  entry; 
  } 
  

  void  enableGC() {
      gc = true;
  }
  void  disableGC() {
      gc = false;
  }

public:
  void display()
  {
    ListEntryT<T>* ptr  = entry;

    while (ptr) {
       T* object = ptr -> getObject();
      if (object) {
        object -> display();  
      }
      ptr = ptr -> getNext();
    }
  }

};

}



Last modified: 10 Feb 2017

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