biggnlarge 0 Newbie Poster

I am currently creating a queue ADT for one of my classes in college. We are using a linux ssh server for compiling our programs, but i am writing the code in windows. When i compile i get no errors, but when i run it goes fine until it hits the copy constructor Queue300 two(one); at which point i get a segfault and the program ends.
im sorry if its a little cryptic to read but my instructor believes that typedef is the most amazing thing since sliced bread.
my code is as follows:

header

#ifndef OBERLOHA3_H
#define OBERLOHA3_H

typedef char Element300[16];

class Queue300
{
      public:
             Queue300();
             Queue300(Queue300&);
             ~Queue300();
             void deQueue(Element300 &);
             void enQueue(const Element300);
             void view();
             
      private:
              struct Node;
              typedef Node *NodePtr;       
              struct Node
              {
                     Element300 string;
                     NodePtr next;
              };
              NodePtr front;
              NodePtr back;
};
#endif

implementation

#include <iostream>
#include <cstdlib>
#include <cstring>
#include "OberlohA3.h"
using namespace std;

Queue300::Queue300()
{
   front = NULL; 
   back = NULL;
}

Queue300::Queue300(Queue300 &queue)
{
   cout << "...";
   Queue300 temp;
   cout << "1";
   Element300 element;
   cout << "2";
   while (queue.front != NULL)
   {
       cout << "!";
       queue.deQueue(element);
       temp.enQueue(element);
   }
   cout << "3";
   while (temp.front != NULL)
   {
       cout <<"!";
       temp.deQueue(element);
       enQueue(element);
       queue.enQueue(element);
   } 
   cout << "end"; 
}
Queue300::~Queue300()
{
   NodePtr temp;
   temp = front;
   while (temp != NULL)
   {
      temp = front->next;
      delete front;
      front = temp;
   }
   temp = NULL;
   front = NULL;
   back = NULL;
}

void Queue300::deQueue(Element300 &Element)
{
     NodePtr temp = NULL;
     if (front == NULL && back == NULL)
        cout << "cannot de queue because the queue is empty." << endl;
     else
     {
        strcpy(Element, front->string);
        temp = front -> next;
        front -> next = NULL;
        front = temp;
        temp = NULL;
        if (front == NULL)
           back = front;
     }
}
 
void Queue300::enQueue(const Element300 Element)
{
     NodePtr temp = NULL;
     if (front == NULL && back == NULL)
     {
        temp = new (nothrow) Node;
        front = temp;
        back = temp;
        front -> next = NULL;
        strcpy(front->string, Element);
        temp = NULL;
     }
     else
     {
         temp = new (nothrow) Node;
         strcpy(temp->string, Element);
         back -> next = temp;
         back  = temp;
         temp = NULL;
     }
}    

void Queue300::view()
{
     Element300 Element;
     Queue300 temp;
     temp.front = NULL;
     temp.back = NULL;
     int counter = 0;
     
     if (front == NULL && back == NULL)
        cout << "cannot view queue because it is empty." << endl;
     else
     {
         while (front != NULL)
         {
             if (counter == 0)
                 cout <<  "FRONT ->";
             deQueue(Element);
             cout << Element << " ->";
             temp.enQueue(Element);
             counter ++;
         }
         cout << "BACK" << endl;
         while (temp.front != NULL)
         {   
             temp.deQueue(Element);
             enQueue(Element);
         }
     }
     cout << front << endl << back << endl;
}

main driver prg

#include <iostream>
#include <cstdlib>
#include <cstring>
#include "OberlohA3.h"
using namespace std;

int main()
{
    Element300 element;
    Queue300 one;
    one.view();
    strcpy(element, "cars");
    one.enQueue(element);
    strcpy(element, "go");
    one.enQueue(element);
    strcpy(element, "fast");
    one.enQueue(element);
    one.view();
    cout << "\n\n Copying one to two" << endl;
    Queue300 two(one);
    cout << "done\n\n";
    one.view();
    //two.view();
    cin.ignore();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.