//simple class to represent linked list nodes
template <class obj>
class Node {
public:
Node(Obj data): next(NULL) {
this -> data = data;
}
Node (): next(NULL){} //default constructor
Obj data;
Node<obj>*next;
};
//implement of a simple linked list
template <class Obj>
class List {
public:
//default constructor
List () {
this ->size = 0;
this -> head = NULL;
}
//add a node to the end of the list
void push_back (Obj data) {
if(this -> head == NULL) { //handle empty list
push_front(data);
return;
}
Node<Obj> *p = new Node<Obj> (data);
Node<Obj> *last = this -> head;
//go to the end of the list
while (last != NULL && last -> next != NULL) {
last = last -> next; //go to the next node
}
last -> next = p; //add the new node to the end
this -> size++; //increment the list count
}
//add a node at the beginning of the list
void push_front (Obj data) {
Node <Obj> *p = new Node <Obj> (data);
if (this -> head != NULL) { //if the list is not empty
p -> next = this -> head; //put the new node before the head
}
this -> head = p; //the new node is the head
this -> size++; //increment the list count
}
//get the first node
Obj front () {
if (this -> head != NULL)
return this -> head -> data;
}
//print all the elements starting with the head
void print() {
Node<Obj> *p = this -> head;
while (p != NULL) {
cout<<p-> data <<"\t";
p = p -> next;
}
}
private:
int size; //the number of nodes in the list
Node<Obj> *head; //link to the first node of the list
};
//driver stub
int main ()
{
List<int> l;
for (int i = 0; i < 5; i++)
l.push_front(1);
l.print();
1. Please use the code tags, to see some nice formatting
2. You have both Obj and obj
3. pop_front is not defined
4. Please explain what exactly is your problem