This program actually have 4 function.
1)AddLast = to add a new node at the last of the list
2)FindNode = to find the desired node
3)DeleteNode = to delete the desired node
4)DisplayNode = display the list

Now..my problems is with the AddLast function..
Before this I done with the AddFirst..Therefore,i try for this AddLast fuction.
I have already compile it using DevC compiler..no error listed..
But suddenly when the command prompt ask me to close the program..
I think maybe it became crash..Why???
I think the AddLast function is correct..
So,,can anyone help me to fix this crash things so that I can compile&run this code..

class List  
{ 
public: 
        List(void) { head = NULL; }     // constructor 
        ~List(void);                            // destructor 
 
        bool IsEmpty() { return head == NULL; } 
        Node* AddLast(int index, double x);      
        int FindNode(double x);  
        int DeleteNode(double x); 
        void DisplayList(void); 
private: 
        Node* head; 
}; 
 
   
Node* List::AddLast(int index,double x)     //please check for me with this AddLast function 
{ 
      if(index<0) 
      return NULL; 
       
      Node* currNode=head; 
       
      Node* newNode = new Node; 
      newNode->data = x; 
       
      if(head == NULL) 
      head = newNode;  
       
      else 
      { 
        currNode->next = head; 
        while(currNode->next !=NULL) 
        { 
           currNode = currNode->next; 
        } 
      currNode->next = newNode; 
      } 
} 

int main() 
{ 
    List n; 
     
    n.AddLast(22,7.0); 
    n.AddLast(44,5.0); 
    n.AddLast(66,3.0); 
    n.AddLast(88,6.0); 
    n.AddLast(99,4.0); 
     
    n.DisplayList(); 
     
     
    system("pause"); 
    return 0; 
}

Recommended Answers

All 5 Replies

I don't believe this code compiled. There is no DisplayList()

this is the DisplayList() fuction..
sorry..for the incovenience...

void List::DisplayList()
{
     int num=0;
     Node* currNode = head;
     
     while(currNode!=NULL)
     {
         cout<<currNode->data<<endl;
         currNode = currNode->next;
         num++;
     }
     
     cout<<"The number of node inside this list is="<<num<<endl;
}

I believe this is useless:

currNode->next = head;

just take it. Also whats up with the index parameter in your addLast(). You are not
doing anything useful with it, so just remove it. And finally you are not returning anything with
your addLast() function if index >= 0.

now this code became like this..

class Node 
{
public:
	double	data;		// data
	Node* next;		// pointer to next
};

class List 
{
public:
	List(void) { head = NULL; }	// constructor
	~List(void);				// destructor

	bool IsEmpty() { return head == NULL; }
	Node* AddLast( double x);	
	void DisplayList(void);
private:
	Node* head;
};

  
Node* List::AddLast(double x)
{      
      Node* currNode;
      
      Node* newNode = new Node;
      newNode->data = x;
      
      if(head == NULL)
      {
      newNode->next = NULL;
      head = newNode; 
      
      else      //have error here..can u help me
      {
        while(currNode->next !=NULL)
        {
        currNode = currNode->next;
        }
      newNode->next = NULL;
      currNode->next = newNode;
      }
}



void List::DisplayList()
{
     int num=0;
     Node* currNode = head;
     
     while(currNode!=NULL)
     {
         cout<<currNode->data<<endl;
         currNode = currNode->next;
         num++;
     }
     
     cout<<"The number of node inside this list is="<<num<<endl;
}

List::~List(void) 
{
   Node* currNode = head;
   Node* nextNode = NULL;
   while (currNode != NULL)
   {
	nextNode	=	currNode->next;
	delete currNode;	
	currNode	=	nextNode;
    }
   
}

int main()
{
    List n;
    
    n.AddLast(7.0);
    n.AddLast(5.0);
    n.AddLast(3.0);
 
    
    n.DisplayList();
    
    
    system("pause");
    return 0;
}

such a silly..
i'm miss the close bracket..of "if"
I try to change the

currNode->next=head;
became
currNode = head;

and i can run it..the output is correct..
can anybody tell me what is the meaning of

currNode = head; at that line
thnk..

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.