Queue using a Circular Linked List
Mar 19, 2014 at 3:17am UTC
I am new to C++ and need a little direction. I am working on a queue. It is telling me that my pointer "new_data" is not initialized, but I thought I did initialize it. Can someone help me understand this concept. I have attached my initial code so you can see where I am at.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include "node2.h" //Provides node2 class
#include <cassert> // Provides assert
namespace main_savitch_8B
{
template <class Item>
queue<Item>::queue( )
//Library facilities used: node2.h
{
count = 0;
rear_ptr = NULL;
}
template <class Item>
queue<Item>::queue(queue<Item>& source)
//Library facilities used: node2.h
{
int i;
if (source.rear_ptr->link() == NULL)
return ;
for (i = 0; i < source.count; i++)
{
rear_ptr = source.rear_ptr;
rear_ptr->set_data(source.rear_ptr->data());
source.rear_ptr = source.rear_ptr->link();
}
count = source.count;
}
template <class Item>
queue<Item>::~queue()
{
int i;
for (i = 0; i < count; i++)
{
pop();
}
count = 0;
}
template <class Item>
void queue<Item>::pop( )
// Library facilities used: cassert
{
assert(!empty( ));
main_savitch_6B::node<Item> *remove_ptr;
remove_ptr = rear_ptr->link();
rear_ptr->set_link(remove_ptr->link());
delete remove_ptr;
--count;
}
template <class Item>
void queue<Item>::push(const Item& entry)
// Library facilities used: node2.h
{
main_savitch_6B::node<Item> *new_data;
if (empty())
{
new_data->set_data(entry);
new_data->set_link(rear_ptr);
rear_ptr->set_link(new_data);
}
else
{
main_savitch_6B::node<Item> *current_first = rear_ptr->link();
new_data->set_data(entry);
rear_ptr->set_link(new_data);
new_data->set_link(current_first);
}
++count;
}
template <class Item>
void queue<Item>::operator =(const queue<Item>& source)
{
if (this == &source)
return ;
clear_queue();
queue(source);
}
template <class Item>
void queue<Item>::clear_queue()
{
int i;
for (i = 0; i < count; i++)
{
pop();
}
count = 0;
}
template <class Item>
Item queue<Item>::front() const
//Library facilities used: cassert
{
main_savitch_6B::node<Item> *front;
assert(!empty());
front = rear_ptr->link();
return front->data();
}
template <class Item>
bool queue<Item>::empty() const
{
if (rear_ptr == NULL)
return true ;
}
}
Mar 19, 2014 at 4:06am UTC
> It is telling me that my pointer "new_data" is not initialized, but I thought I did initialize it.
¿where?
Topic archived. No new replies allowed.