Hello, I'm making a linked list and when i try to make a member function to return a new object
i keep getting an error reading: "‘node’ does not name a type". I think im writing the function
wrong, any help would great.
#include <iostream>
class list
{
private:
class node
{
public:
int data;
node *next;
}*head;
public:
list() : head(0) {}
~list();
bool isempty() const;
void print() const;
void push_back(int);
node *new_node(int);
};
//function to return a new node
node *list::new_node(int v) ///////////error here
{
node *temp = new node;
temp->data = v;
temp->next = 0;
return temp;
}
//destructor
list::~list()
{
node *temp = head;
node *tmp;
while(temp != 0)
{
tmp = temp;
temp = temp->next;
delete tmp;
}
}
//check if list is empty
bool list::isempty() const
{
return head == 0;
}
//print list
void list::print() const
{
node *temp = head;
while(temp != 0)
{
std::cout << temp->data << std::endl;
temp = temp->next;
}
std::cout << std::endl;
}
//push back into list
void list::push_back(int v)
{
node *temp = new_node(v);
if(isempty())
head = temp;
else
{
temp->next = head;
head = temp;
}
}
int main()
{
using namespace std;
list test;
test.push_back(1);
test.push_back(2);
test.push_back(3);
test.print();
return 0;
}