Display of a singlyLinked list using Queue with rear and front as pointers

So i am having trouble understanding how to display my queue, and if my implementation of queue is correct

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

#ifndef DB_H
#define DB_H

#include "Node.h"

class DB {
private:
    Node* front;
    Node* rear;
    int   size;
    void  printHeader(ostream&);
public:
    DB();
    ~DB();
    void  enqueue(const string&);
    void  dequeue();
    void  display(ostream&);
  
};
#endif




#include <iostream>
#include <iomanip>
#include "DB.h"

using namespace std;

DB::DB()
{
    cout << "DB() is called" << endl;
    front = nullptr;
    rear = nullptr;
    size = 0;
}

void DB::enqueue(const string& input) 
{
    rear = new Node(input, rear);
    size++;
}

void DB::dequeue() 
{
    if (isFull())
    {
        throw QueueEmptyException();
    }
    if (front == NULL)
    {
        return;
    }
    Node* temp = front;
    front = front->next;
    delete temp;
    size--;
}


void DB::display(ostream& out)
{
    printHeader(cout);
    Node* temp;
    rear = front;

    while (rear!= NULL)
    {
        temp->emp->display(cout);
        temp = temp->next;
    }
}

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

#include "DB.h"
using namespace std;
int main()
{

    fstream fin;
    fin.open("Employee.txt", ios::in);
    if (fin.fail()) {
        cout << "Could not open file " << endl;
        exit(1);
    }
    else {
        cout << " File opened" << endl;
    }

    string input;
    DB employeeDB;

    while (getline(fin, input) && input != "")
    {
        if (input != "") {
            employeeDB.enqueue(input);
        }
    }


    cout << "\nAfter enqueue() for each employee" << endl;
    employeeDB.display(cout);

1
2
3
4
5
6
7
8
9
10
11
12
void DB::display(ostream& out)
{
    printHeader(cout);
    Node* temp; // HERE, the pointer temp is pointed at random memory.
    rear = front;

    while (rear!= NULL) // When will rear EVER equal NULL? Where do you change rear ?
    {
        temp->emp->display(cout); // HERE, you try to use temp. It's still pointing at random memory.
        temp = temp->next;
    }
}
Last edited on
Ive been working on it but somehow i still cant crack it
1
2
3
4
5
6
7
8
9
10
void DB::display(ostream& out)
{
    printHeader(cout);
    Node* temp = front;
    while (temp!= NULL)
    {
        temp->emp->display(cout);
        temp = temp->next;
    }
}
I see that your dequeue function doesn't actually return anything or provide a value from the queue to the caller.

I think a queue that makes it impossible to actually get any values back out of isn't much use.

I see also that your dequeue function throws an exception if the queue is full. That doesn't make much sense. Why does the dequeue function care if the queue is full? The dequeue function is for removing a node, not adding one.

Your destructor needs to delete every node in the queue or you'll have a memory leak.
Does your Node::display function actually work?
Last edited on
it goes to my display but it says that both front, temp are null, and if i set it to rear, it also says that my rear is Null so it exits out
Well then maybe front, temp and rear are null.

How many items did you put in the queue? How do you know that many went in?
I also have my own function of ~DB i just did not post it because it was irrelevant to my question
I have a tracker, size tells me the amount of items that have went in
Yes, and how many did it say went in? Did they go in? What happens if you try to display a Node that you know for sure definitely exists?

After you enqueue a node for the first, is rear still null? How do you know?

Add code to print out each node in the enqueue function so you know for sure that they're going in and the pointers are pointing to the right place.

Prove to yourself that your display function works. Prove it by writing code that should work.

Does this work?
1
2
Node a_node("Some string");
a_node->emp->display(cout);


Last edited on
1
2
3
4
5
void DB::enqueue(const string& input) 
{
    rear = new Node(input, rear);
    size++;
}


Does the constructor for Node correctly link a new node?

Where does front get set if DB is initially empty??? This sets rear, but front is still nullptr. Shouldn't this be something like:

1
2
3
4
5
6
7
8
void DB::enqueue(const string& input) 
{
    rear = new Node(input, rear);
    if (front == nullptr)
        front = rear;

    size++;
}


Have you traced through the code using the debugger to see what's going on? Proficiency using the debugger is a skill that all programmers need to acquire - the sooner the better.

 
 while (temp!= NULL)


In C++ this should be (and elsewhere):

 
 while (temp != nullptr)


- as used in the constructor.




Last edited on
> if my implementation of queue is correct
>> I also have my own function of ~DB i just did not post it because it was irrelevant to my question
the destructor is quite relevant to the implementation

you also didn't post Node definition and some member function are missing too.
¿do you just read code and never execute it?
prepare some test, run the program, receive a crash, read the backtrace, inspect variables in those specific functions, checkout the logic
that's how debugging is done, so unless you provide a complete testcase your snips are quite useless.


1
2
3
4
5
void DB::enqueue(const string& input) 
{
    rear = new Node(input, rear);
    size++;
}
rear is the "last" element, ¿what's rear->next?
I don't think this is the issue you are having, but might come back to bite you down the road.

Your display function takes an ostream& argument out. Ostensibly, you intend to write your output to that stream. However, you ignore out and write everything to cout (I assume you have a using namespace std; statement at the beginning of your file, and you mean std::cout).

1
2
3
4
5
6
7
8
9
10
void DB::display(ostream& out)
{
    printHeader(cout);
    Node* temp = front;
    while (temp!= NULL)
    {
        temp->emp->display(cout);
        temp = temp->next;
    }
}


The reason you would pass an ostream& to the function is to allow you to write the output to std::cout, std::cerr, a file, a particular piece of memory, another process, a socket, or any other stream you might come up with. Right now, the argument is being ignored. You should change cout to out in the body of your function.

Please post node.h.

Anywhere that rear can change, there's a good chance that front can change also.
Anywhere that front can change, there's a good chance that rear can change also.

For example, when you insert into an empty queue, both from and rear change.
When you remove the last item from a queue, both front and rear change.

Why does dequeue() throw an exception when the queue is full? I don't see why you can't dequeue from a full queue. On the other hand, I can see why there would be a problem trying to enqueue into a full queue.
Topic archived. No new replies allowed.