Double Linked List header node keeps returning first value as 0

Posted by Craig on Stack Overflow See other posts from Stack Overflow or by Craig
Published on 2012-09-29T21:16:39Z Indexed on 2012/09/29 21:37 UTC
Read the original article Hit count: 154

Filed under:
|

I will preface to say that this is my first question. I am currently getting my Masters degree in Information Security and I had to take C++ programming this semester. So this is homework related. I am not looking for you to answer my homework but I am running into a peculiar situation. I have created the program to work with a doubly linked list and everything works fine. However when I have the user create a list of values the first node keeps returning 0. I have tried finding some reading on this and I cannot locate any reference to it. My question is then is the header node(first node) always going to be zero? Or am I doing something wrong.

case: 'C':
 cout<<"Please enter a list:"<<endl;
  while(n!=-999){
     myList.insert(n);
     cin>> n;}
  break;

I now enter: 12321, 1234,64564,346346. The results in 0, 12321, 1234, 64564,346346. Is this what should happen or am I doing something wrong? Also as this is my first post please feel free to criticize or teach me how to color code the keywords.

Anyway this is a homework assignment so I am only looking for guidance and constructive criticism.

Thank you all in advance

So I cannot figure out the comment sections on this forum so I will edit the original post The first section is the constructor code:

template <class Type>
 doublyLinkedList<Type>::doublyLinkedList()
  {
    first= NULL;
    last = NULL;
    count = 0;
      }

Then there is my insert function :

template <class Type>
void doublyLinkedList<Type>::insert(const Type& insertItem)
 {
nodeType<Type> *current;      //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode;      //pointer to create a node
bool found;

newNode = new nodeType<Type>; //create the node
newNode->info = insertItem;  //store the new item in the node
newNode->next = NULL;
newNode->back = NULL;

if(first == NULL) //if the list is empty, newNode is 
                  //the only node
{
   first = newNode;
   last = newNode;
   count++;
}
else
{
    found = false;
    current = first;

    while (current != NULL && !found) //search the list
        if (current->info >= insertItem)
            found = true;
        else
        {
            trailCurrent = current;
            current = current->next;
        }

    if (current == first) //insert newNode before first
    {
        first->back = newNode;
        newNode->next = first;
        first = newNode;
        count++;
    }
    else
    {
          //insert newNode between trailCurrent and current
        if (current != NULL)
        {
            trailCurrent->next = newNode;
            newNode->back = trailCurrent;
            newNode->next = current;
            current->back = newNode;
        }
        else
        {
            trailCurrent->next = newNode;
            newNode->back = trailCurrent;
            last = newNode;
        }

        count++;
      }//end else
   }//end else
}//end 

Then I have an initialization function too:

template <class Type>
 void doublyLinkedList<Type>::initializeList()
 {
  destroy();
}

Did I miss anything?

© Stack Overflow or respective owner

Related posts about c++

Related posts about homework