Why is my destructor not working properly when ran with my whole code it keeps displaying 01010101010101 etc here is my code

LinkedList::~LinkedList()
 
      {
 
		   NodePointer ptr = first;
              

              while(ptr != 0){
 
                     

                      first = ptr->next;
 
                      delete ptr;

					  ptr = first;

 
                }
 
              if (first == 0) cout << "List destroyed\n";

              else cout << "List not destroyed\n";
 
      }

Are you sure it's the destructor that's the problem? How do you know?

ok it is not my destructor here is my wholeee code please help me find the problem i have been STRESSING!!

/*--- LinkedList.h --------------------------------------------------------
  This header file contains the declarations of LinkedList, a class for
  singly-linked lists.
 
  Written by:   Larry R. Nyhoff
  Written for:  Lab Manual for ADTs, Data Structures, and Problem
                Solving with C++, 2E

                    Lab #5.1 and Projects 5.1 & 5.2
                              
   Add a list of the basic operations including brief descriptions.
 
   
 --------------------------------------------------------------------------*/

#ifndef LINKEDLIST
#define LINKEDLIST

#include <iostream>
#include <new>
using namespace std;

typedef int ElementType;        //----- Add typdef statement here

class LinkedList
{
 public:
   //------ LinkedList OPERATIONS
	friend ostream& operator<<(ostream& out, LinkedList list);

   // Prototype the class constructor here
	 LinkedList();
	

   /* --- LinkedList constructor --------------------------------------
      Constructs an empty LinkedList object.

      Precondition:  None.
      Postcondition: This list's data members have been initialized 
          for an empty list.
   ---------------------------------------------------------------------*/

  // Prototype and document the size() operation here
	int size();

  // Prototype and document display() here
	void display(ostream&);

 
  // Prototype insert() here
	void insert(int, ElementType);

  /*----------------------------------------------------------------------
     Insert a value into a LinkedList at a given index.

     Precondition:  The first parameter, index, is an unsigned value with
         0 <= index <= mySize; the second parameter, dataValue, is an  
         ElementType value.  index = 0 denotes insertion at the beginning
         of the list, and index = mySize denotes insertion at the end 
         (after the current last element).
     Postcondition: dataValue has been inserted into this LinkedList
         object at the position determined by index (provided index 
         is a legal position).
  -----------------------------------------------------------------------*/

  // Prototype erase() here
  void erase(unsigned int index);
  /*----------------------------------------------------------------------
     erase() removes a value from a LinkedList at a given index.

     Precondition:  The parameter index, an unsigned value, satisfies
                              0 <= index < mySize.  
     Postcondition: The data value at the position determined by index
         (provided index is a legal position) has been removed from 
         this LinkedList object. 
  -----------------------------------------------------------------------*/

 
   // Prototype and document the destructor here
	//~LinkedList();
  
  
  // Prototype and document the copy constructor here
 LinkedList(const LinkedList & origList);


  // Prototype and document the assignment operator here
	



private:
  class Node
  {
    public:

     //------ DATA MEMBERS OF Node

     // Define data and next members here
		ElementType data;     
		Node *next;
		Node(int valu)
		{
			data = valu;
			next = NULL;
		}

     //------ Node OPERATIONS

     // Prototype the Node constructor here
				



     /* --- The Node class constructor initializes a Node's data members.

        Precondition:  None
        Receive:       dataValue, an ElementType value;
        Postcondition: The data and next members have been set to
                       dataValue and 0, respectively.
     -------------------------------------------------------------------*/

   }; //--- end of Node class

   typedef Node * NodePointer;
   

   //------ DATA MEMBERS OF LinkedList
   // declare first as a pointer to a Node and declare mySize
Node *first;
int mysize;

}; //--- end of LinkedList class

// Put prototype of operator<<() here
 

#endif
/*--- linktester.cpp --------------------------------------------------
  A program for testing class LinkedList.
 
  Written by:   Larry R. Nyhoff
  Written for:  Lab Manual for ADTs, Data Structures, and Problem
                Solving with C++, 2E

                    Lab #5.1 and Projects 5.1
   
  
   
  ---------------------------------------------------------------------*/

#include <iostream>
using namespace std;

#include "LinkedList.h"

/*---- PART 6 ---- TEST COPY CONSTRUCTOR
template <typename T>             // f() is a function template with a
void f(LinkedList<T> aList)       // LinkedList value parameter
{                                 // to test the copy constructor
  for (int i = 1; i < 5; i++)
  {
     aList.insert(i, 100*i);      // insert into the copy
     cout << aList << endl;       // output the copy
  }
}
---- END PART 6 ----*/

int main()
{
   LinkedList intList;       // TEST THE CONSTRUCTOR
   cout << "Constructing intList\n";

// ---- PART 1 ---- TEST SIZE OPERATION

   cout << "Size of intList is " << intList.size() << endl; 

//---- END PART 1 ----


// ---- PART 2A ---- TEST OUTPUT OF EMPTY LIST USING display()
   cout << "Empty List (using display): \n";
   intList.display(cout);
   cout << endl; 
//---- END PART 2A ----

// ---- PART 2B ---- TEST OUTPUT OF EMPTY LIST USING <<
   cout << "Empty List (using <<): \n"
        << intList << endl;
//---- END PART 2B ----

   
/* ---- PART 3 ---- TEST INSERT OPERATION*/
   for (int i = 0; i < 9; i++)
   {                                    
      intList.insert(i/2, 10*i);          //  -- insert 10*i at position i/2
      cout << intList << endl;            // test output
   }

  //-- Test insert at end of list:
  intList.insert(intList.size(), 999);
  cout << "\ninsert 999 at end of list:\n" 
       << intList << endl;

  //-- Test for illegal inserts
  cout << "Try inserting at positions -1 and 20:\n";
  intList.insert(-1, -99);
  intList.insert(20, 200);
//---- END PART 3 ----*/

// ---- PART 4 ---- TEST ERASE OPERATION
  cout << "\nRemove last node:\n";
  intList.erase(intList.size() - 1);
  cout << intList << endl;

  cout << "\nRemove first node:\n";
  intList.erase(0);
  cout << intList << endl;

  cout << "\nRemove node at position 4:\n";
  intList.erase(4);
  cout << intList << endl;

  //-- Test for illegal deletes
  cout << "Try deleting at positions -1 and 20\n"; 
  intList.erase(-1); 
  intList.erase(20); 

//---- END PART 4 ----*/

/* ---- PART 5 ---- TEST DESTRUCTOR
   {
      LinkedList anotherList;
      for (int i = 0; i < 5; i++)
         anotherList.insert(i, 20 * i);
      cout << "\nHere's another list:\n" << anotherList << endl;
      cout << "Now destroying this list\n";
   }
   cout << "*** If the destructor was called, anotherList was destroyed ***\n";
//---- END PART 5 ----**/


/* ---- PART 6 ---- TEST COPY CONSTRUCTOR
   cout << "\n\nTesting copy constructor" << endl;
   f(intList);

   cout << "\n\nOriginal list:";          // output the original to make sure
   cout << intList<< endl;                // it hasn't been changed.

---- END PART 6 ----*/

// ---- PART 7 ----
//   ADD STATEMENTS HERE TO TEST ASSIGNMENT OPERATOR
// ---- END PART 7 ----
system("pause");
return 0;
}
//LinkedList.cpp

#include <iostream>
using namespace std;

#include "LinkedList.h"
	 
LinkedList :: LinkedList()
	 {
		 first = NULL;
		 mysize = 0;
	 }



int LinkedList :: size()
{
	return mysize;
}

void LinkedList :: display(ostream& out)
{
	Node *ptr = first;
	while( ptr != NULL)
	{
		out << ptr->data << " ";
		ptr = ptr->next;
	}
}
void LinkedList::erase(unsigned int index)
{
	if (index >=0 && index <= mysize)
	{
		NodePointer predPtr;
		if (index ==0)
		{
			predPtr = first->next;
			first = predPtr;
		}
		else
		{
			predPtr = first;
			for (int i = 1; i <= index-1; i++)
			{
				predPtr = predPtr->next;
			}
			predPtr->next = predPtr->next->next;
		}
		mysize--;
	}
}


void LinkedList :: insert(int index, ElementType dataValue)
{
	if (index >=0 && index <=mysize)
	{
		NodePointer nPtr = new(nothrow) Node(dataValue);
		if (index == 0)
		{
			nPtr->next = first;
			first = nPtr;
		}
		else
		{
			NodePointer predPtr = first;
			for (int i=1; i <= index-1; i++)
			{
				predPtr = predPtr->next;
			}
			nPtr->next = predPtr->next;
			predPtr->next = nPtr;
		}
		mysize++;
	}
	else {
		cout << "Illegal insert" << endl;
	}

}



ostream& operator<<(ostream& out, LinkedList list)
{
	list.display(out);
	return out;
}

Either use a debugger to step through the program or comment out all the methods of the class and associated function calls in main and add them back one at a time until you find where the problem is. Learning how to debug is as important as learning how to write the code itself. Learning how not to stress when you run up against a bug is also an important skill to have. Be systematic and you should be able to narrow down where the problem is without too much trouble. It is unlikely that anyone here will do the debugging for you.

I have to admit I can't make heads nor tails of your debugging screen shot, but then I never use a debugger. I did a copy/paste of your code into Visual Sudio 8, stripped it down to the essentials, added a little nonessential "debugging" code to test the output along the way, and fixed a memory leak in the erase() function (not essential to running the program). It seems to work fine, though I would never implement a linked list using (zero based) indexing to control insert and erasure outside of a learning lab. Oh, and I'd stick to spaces for indentation and loose the tabs, but that's more personal prefence than coding accuracy.

#include <iostream>
using namespace std;

class LinkedList
{
 public:
	LinkedList();
	int size();
	void display();
	void insert(int, int);
  void erase(int index);
  ~LinkedList();
  LinkedList(const LinkedList & origList);
 private:
  class Node
  {
    public:
     int data;     
		 Node *next;
		 Node(int valu)
		 {
			data = valu;
			next = NULL;
		 }
   };   
   Node *first;
   int mysize;
}; 

int main()
{
   LinkedList intList;      
   for (int i = 0; i < 9; i++)
   {                                    
      intList.insert(i/2, 10*i);          
      intList.display();
      cout << endl;
   }
  cout << "\ninsert 999 at end of list:\n";
  intList.insert(intList.size(), 999);
  intList.display();
  cout << endl;
  cout << "Try inserting at positions -1 and 20:\n";
  intList.insert(-1, -99);
  intList.insert(20, 200);
  cout << endl;
  cout << "\nRemove last node:\n";
  intList.erase(intList.size() - 1);
  intList.display();
  cout << endl;
  cout << "\nRemove first node:\n";
  intList.erase(0);
  intList.display();
  cout << endl;
  cout << "\nRemove node at position 4:\n";
  intList.erase(4);
  intList.display();
  cout << endl;
  cout << "Try deleting at positions -1 and 20\n"; 
  intList.erase(-1); 
  intList.erase(20); 
  char ch;
  cin.get(ch);
  return 0;
}

LinkedList :: LinkedList()
{
		 first = NULL;
		 mysize = 0;
}

int LinkedList :: size()
{
	return mysize;
}

void LinkedList :: display()
{
	Node * ptr = first;
  if(ptr)
  {
	  while( ptr != NULL)
	  {
		  cout << ptr->data << " ";
		  ptr = ptr->next;
	  }
  }
  else
    cout << "empty list";
}

void LinkedList::erase(int index)
{
	if ( -1 < index && index < mysize)
	{
		Node * predPtr;
    Node * temp;
		if (index ==0)
		{
			predPtr = first;
			first = predPtr->next;
      delete predPtr;
		}
		else
		{
			predPtr = first;
			for (int i = 1; i < index; i++)
			{
				predPtr = predPtr->next;
			}
      temp = predPtr->next;
			predPtr->next = predPtr->next->next;
      delete temp;
		}
		mysize--;
	}
  else
    cout << "illegal index" << endl;
}

void LinkedList :: insert(int index, int dataValue)
{
	if (-1 < index && index < mysize)
	{
		Node * nPtr = new Node(dataValue);
		if (index == 0)
		{
			nPtr->next = first;
			first = nPtr;
		}
		else
		{
			Node * predPtr = first;
			for (int i = 1; i < index; i++)
			{
				predPtr = predPtr->next;
			}
			nPtr->next = predPtr->next;
			predPtr->next = nPtr;
		}
		mysize++;
	}
	else {
		cout << "Illegal insert" << endl;
	}

}

LinkedList::~LinkedList()       
{
  Node * ptr = first;
  while(ptr != 0)
  {
    first = ptr->next;
    delete ptr;
		ptr = first;
  }
 
  if (first == 0) 
    cout << "List destroyed\n";
  else 
    cout << "List not destroyed\n";
  char ch;
  cin.get(ch);
  cin.get(ch);
}

Merci monsieur!

Next time you need to do the same, or learn how to interpret the debugger! Doing the debugging one function, at most, at a time is also strongly recommended. You had everything set up to do that, you just needed to do it.

yes, i need to learn how to interpret it i began watching youtube videos

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.