www.pudn.com > HEC-linux.zip > list.cpp
#include#include /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + declarations + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ class List { int LIST_INIT; int LIST_INC; public: int *ptr; /*pointer to start of list*/ int nCells; /*current capacity*/ int iNext; /*next free space*/ List(int init,int inc); ~List(); void addToList(int val); void printList(); }; /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + definitions + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ List::List(int init,int inc) { LIST_INIT=init; LIST_INC=inc; ptr = (int*)malloc(LIST_INIT*sizeof(int)); if(ptr==NULL) { printf("List(): cannot allocate memory\n"); exit(1); } iNext = 0; nCells = LIST_INIT; return; }/*end constructor*/ /*-----------------------------------------------------------------*/ List::~List() { free(ptr); return; }/*end destructor*/ /*-----------------------------------------------------------------*/ void List::addToList(int val) { int i; int *temp_ptr; /* if nCell=n will have valid indices 0,...,n-1 thus, if index=n we are out of bounds */ if(iNext >= nCells) { temp_ptr = (int*)malloc((nCells+LIST_INC)*sizeof(int)); if(temp_ptr==NULL) { printf("List.addToList(): cannot allocate more memory\n"); exit(1); } else { printf("List.addToList(): not enough room for %d\n",val); printf("List.addToList(): allocating %d more cells\n",LIST_INC); for(i=0;i