Hi,
I have an assignment where I have to implement a singly linked list of integers. Can you look at my pop front function below to see if it would work. I think it works, but I get this weird error whenever I test it. Also, how would I do my push back fucntion to add a number to the back end of the list. I know all linked lists end in NULL, but how would I write that out?
Thanks
My file definition:
#ifndef INTLIST_H_INCLUDED
#define INTLIST_H_INCLUDED
#ifndef NULL
#define NULL 0
#endif
class IntNode
{
public:
int data;
IntNode *next;
IntNode(int data)
: data(data), next(NULL)
{}
};
class IntList
{
private:
IntNode *head;
public:
IntList();
~IntList();
unsigned int get_size() const;
void print() const;
const int &at(unsigned int index) const;
int &at(unsigned int index);
void push_front(int data);
void push_back(int data);
void pop_front();
void select_sort();
void insert_sorted(int value);
void erase(unsigned int position);
void remove_duplicates();
};
#endif // INTLIST_H_INCLUDED
My implementation:
#include <iostream>
#include "IntList.h"
using namespace std;
IntList::IntList()
: head(NULL)
{}
IntList::~IntList()
{}
unsigned int IntList::get_size()const
{
unsigned int count = 0;
for(IntNode *i=head;i;i->next)
{
++count;
}
return count;
}
void IntList::print()const
{
for(IntNode *i = head;i;i=i->next)
{
cout << i->data << " ";
}
}
//insert front end
void IntList::push_front(int data)
{
IntNode *new_node = new IntNode(data);
new_node->next = head; //or other way around not sure
head = new_node;
delete new_node;
}
// insert back end
void IntList::push_back(int data)
{
IntNode *new_node = new IntNode(data);
}
void IntList::pop_front()
{
if(!head)
return;
IntNode *del = head;
head = head->next;
delete del;
}