0

que.print is not working and I cannot figure out why not
I posted a lot of code because I cannot figure out where the error is, I think it is in que.cpp, but I am nto sure what is wrong.

Here is the main

#include "Stack.h"
#include "Que.h"
#include<string>
#include <iostream>
#include <fstream>
using namespace std;
int main()

{
	string file;
	Stack stack;
	Que que;
	ifstream inputFile;
	inputFile.open("text.txt");
	while(inputFile.good())
	{
	inputFile >> file;
	stack.push(file);
	que.add(file);
	}
	stack.printStack();
	que.print();  //not working

	return 0;

}

here is the h file

#ifndef QUEUE
#define QUEUE
#include "Node.h"
class Que
{
private:
    Node* front;
    Node *rear;
	void printHelper(Node *);
   
public:
    Que();
    Que(const Que &);
    ~Que();
    void add(string);
    string remove();
    bool isEmpty() const;
    void operator=(const Que &);
    void print();

};

#endif

here is the cpp file for que

#include "Que.h"
#include "Stack.h"
#include <iostream>
using namespace std;

Que::~Que()
{
   
    Node * temp = front;

	while (front)
		{
		  temp = front->getLink();
		  delete front;
		  front = temp;
		}

}
Que::Que()
{
	front=NULL;
	rear = NULL;
}
Que::Que(const Que &obj)
{
	Node *walker;
	Node *copy;
	walker= obj.rear;
	if(obj.isEmpty()){//check to see if node is null
		front=NULL;
		rear=NULL;
	return;
	}
	copy=new Node(walker->getData(), NULL);//set to copy first node
	rear =copy;
	walker=walker->getLink();
	while(walker)
	{

		copy->setLink(new Node(walker->getData(), NULL));
		walker=walker->getLink();
		copy=copy->getLink();
	}
	front = copy;

}

void Que::operator=(const Que & obj)
{
	Node *walker;
	Node *copy;
	walker= obj.rear;
	if(obj.isEmpty()){//check to see if node is null
		front=NULL;
		rear=NULL;
	return;
	}
	copy=new Node(walker->getData(), NULL);//set to copy first node
	rear =copy;
	walker=walker->getLink();
	while(walker)
	{

		copy->setLink(new Node(walker->getData(), NULL));
		walker=walker->getLink();
		copy=copy->getLink();
	}
	front = copy;

}
void Que::add(string file)
{
	front = new Node(file, NULL);
	front=front->getLink();
}
string Que::remove()
{
	Node *temp;
	temp=rear;
	rear = rear->getLink();
	string val = temp->getData();
	delete temp;
	return val; 
}
void Que::print()
{
	printHelper(rear);
		
}
bool Que::isEmpty() const
{
	if(!front)
		return true;
	else
		return false;
}
void Que::printHelper(Node *temp)
{
	if(!temp)
		return;
	else
    {
	printHelper(temp->getLink());
	cout<<temp->getData()<<" ";
	}

}

and one more header file

#ifndef STACK_H
#define STACK_H
#include "Node.h"
class Stack
{
private:
    Node *top;
public:
    Stack();
    Stack(const Stack &obj);
    ~Stack();
    void push(string);
    string pop();
    bool isEmpty() const;
    void operator=(const Stack &);
    void printStack();

};
#endif

Edited by aaronmk2: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by griswolf
0

I think maybe Que::add is the problem: Which 'front' is the internal one? Which order should you do things in?

Debugging technique: in PrintHelper, cout << 'END'<<endl if you get a null temp.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.