Topic: How to create working chain list?


Author: menardsy@JSP.UMontreal.CA (Menard Sylvain)
Date: 1995/06/23
Raw View
I'm new to the C++ language. I'm currently using turbo C++ V3.0. I'm
trying to figure out how to use a chain list in this language, but
partially successful. I've written a little program to test how chain list
may be use in TC++. The problem is that the program sometime lock. I've
also include a little test to know if there's enought memory to create
another object, and it always stop at this point. I can't believe that I
can't create 10000 objects(as you will see, the structure of the object is
really s mall). Could someone tell me why it is not working? Here's the
source:


-------------------------------------------------------------------------
#include <dos.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <bios.h>
#include <process.h>
class tab
{
public : unsigned int number;
tab *suivant;

// recursive procedure to print the variable 'number'
 void next()
 {
  cout << number <<" ";
  if (suivant != NULL) suivant->next();
 };
};

void main()
{
tab *ref1, *ref2;
// keep a pointer on the initial chain
ref1= new tab;
ref2= new tab;
ref2=ref1;

//  create a chain list with integer from 0 to 10000
 for (int i=0;i<10000;i++)
 {
 ref1->number=i;
 if (!(ref1->suivant=new tab))

  {cout << "not enought memory: stopped at: "<< i;
  delete ref1;
  delete ref2;
  exit(1);
  }

 ref1=ref1->suivant;
 }
 ref1->suivant=NULL;
 ref1=ref2;

cout << "\n";
cout <<"finished!\n";
ref1=ref2;
ref1->next();
delete ref1;
delete ref2;

getch();
}

--------------------------------------------------------------------------


Sylvain.