I am having trouble with the following program. I am trying to create a linked list that behaves like a FIFO queue. I need to be able to add new items to the end of the queue and remove them from the front. My confusion is with trying to use the pointers to point to the right place. I am also having trouble getting the node_number to increment correctly.
#include <iostream>
using namespace std;
class QueueItem{
private:
char *item;
int node_number;
static int node_counter;
QueueItem *next;
public:
QueueItem()
{
item=0;
node_number=0;
next=0;
};
QueueItem(char *input)
{
item=new char[strlen(input)+1];
strcpy(item,input);
next=0;
node_counter++;
node_number++;
};
void set_next(QueueItem *next_item)
{
if(next!=0)
{
QueueItem *t_ptr=next;
next=next_item;
next_item->next=t_ptr;
}
else
{
next=next_item;
}
};
QueueItem * get_next()
{
return next;
};
int get_birth_number()
{
return node_counter;
};
int get_node()
{
return node_number;
};
void get_item()
{
cout<<node_number<<""<<item<<endl;
};
};
int QueueItem::node_counter=0;
class Queue{
private:
QueueItem *head_ptr,*current;
public:
Queue()
{
head_ptr=new QueueItem;
};
void AddItem(char *str)
{
QueueItem *iter=head_ptr;
while (iter->get_next()!=0)
{
iter=iter->get_next();
}
iter->set_next(new QueueItem(str));
};
void RemoveItem()
{
QueueItem *iter=head_ptr;
delete iter;
iter=iter->get_next;
};
void print()
{
QueueItem *iter=head_ptr;
while (iter->get_next()!=0)
{
iter=iter->get_next();
iter->get_item();
}
};
/* void Erase()
{
QueueItem *next_item,*temp_item;
next_item=head_ptr;
while(next_item !=0)
{
temp_item=head_ptr->next_item();
item=next_info->get_item();
delete item;
delete next_item;
next_item=temp_item;
}
head_ptr=0;
};*/
};
void main()
{
Queue names;
names.AddItem ("Michele");
names.AddItem ("Jimmy");
names.AddItem ("Sarah");
names.AddItem ("Joshua");
names.print();
names.RemoveItem();
names.print();
}