Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Implement the default constructor for the LinkedList class . Remember, the default constructor 's job is to create an empty list.

In this version of Node, we are using a struct with members int data and Node *next.


My function below is incorrect. Can you please help fix this?

What I have tried:

LinkedList::LinkedList()
{
   int data;
   Node *next;
   data=0;
   *next = NULL;
	
}
Posted
Updated 2-May-17 21:27pm
v2
Comments
jeron1 2-May-17 16:15pm    
Question?
[no name] 2-May-17 22:45pm    
So you are having a problem with your homework and all you can come up with is post some kind of a question here. Do some research off your own bat. This is how you will get ahead. Google is an amazing resource: a linked list nodes c - Google Search[^]
Arthur V. Ratz 5-May-17 11:28am    
I agree with Rick Your, you normally should define a structure called LinkedList instead of class. That's easier.

Probably it should be something similar to
C++
class LinkedList
{
  // private member variables of the class 
  int data;
  Node *next;
  //...
public:
  // constructor: put the object in a known state
  LinkedList() : data(0), next(nullptr) {}
  //...
};
 
Share this answer
 
Hi Member 13041326,

This line is wrong:
*next = NULL;
It should be:
next = NULL;
next is a pointer to the next node; you want to point it to none (aka NULL) at the initialization (aka constructor).

*next would mean the value stored at next. next is a pointer, and *next is the node (that next is pointing at). The erroneous line is trying to assign 0 (NULL is 0) to a node type which is wrong, hence this would definitely give a compile error (e.g. node = 0 is not possible).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900