开发者

Reading data into a queue

Using the following (incomplete) code, I am attempting to insert 10 strings into a string queue (declared and defined further down), then read the contents out one at a time, however the output from the queue doe snot correspond to the expected output

int main()
{           
    ifstream File;
    for (int count = 1; count <= 10; count ++)
    {
        string filename;    
        stringstream ss;
        ss << "PersonLists/PL" << count << ".txt";
        filename = ss.str();
        WaitingListList.Add(filename);

        WaitingListList.Remove(filename);
                cout << filename << endl;
        WaitingListList.Add(filename);
    }
}

The following is what I would expect from my code, and I have verified that the filename objects are being constructed correctly using the stringstream seen in int main() by inserting an output stream directly after the line filename = ss.str().

PersonLists/PL1.txt
PersonLists/PL2.txt
PersonLists/PL3.txt
PersonLists/PL4.txt
PersonLists/PL5.txt
PersonLists/PL6.txt
PersonLists/PL7.txt
PersonLists/PL8.txt
PersonLists/PL9.txt
PersonLists/PL10.txt

However, when I print the contents of the queue, this is the output I receive, and I am unable to discern any pattern in the file names that could indicate the cause of this. Can anyone looking at the code here figure out what's going on? I've successfully used this same process in other parts of my program, using the same queue template.

PersonLists/PL1.txt
PersonLists/PL1.txt
PersonLists/PL2.txt
PersonLists/PL1.txt
PersonLists/PL3.txt
PersonLists/PL2.txt
PersonLists/PL4.txt
PersonLists/PL1.txt
PersonLists/PL5.txt
PersonLists/PL3.txt

queue.h

#ifndef QUEUE_H
#define QUEUE_H
using namespace std;
template <class Type>
class queue
{
private:
    Type *Contents;
    int Front, Back;
    int QueueSize;
public:
    queue(int queuesize = 10);
    ~queue();
    bool Empty(开发者_运维百科) const;
    bool Full() const;
    bool Remove(Type&);
    bool Add(Type&);

};
#endif

queuetemplate.h

#ifndef QUEUETEMPLATE_H
#define QUEUETEMPLATE_H
#include "queue.h"
using namespace std;

// Constructor
template <class Type>
queue<Type> :: queue(int queuesize): 
    Front(0), Back(0),
    QueueSize(queuesize),
    Contents(new Type[queuesize + 1])
{}

// Destructor
template <class Type>
queue<Type> :: ~queue()
{
    delete[] Contents;
}

// Tests
template <class Type>
bool queue<Type> :: Empty() const
{
    return (Front == Back) ? true : false;
}

template <class Type>
bool queue<Type> :: Full() const
{
    return ((1 + Back) % (QueueSize + 1) == Front) ? true : false;
}

// Push and pop
template <class Type>
bool queue<Type> :: Remove(Type& FrontElement)
{
    if (Empty())
    {
        return false;
    }
    else
    {
        FrontElement = Contents[Front];
        Front = (Front + 1) % (QueueSize + 1);
        return true;
    }
}

template <class Type>
bool queue<Type> :: Add(Type& NewElement)
{
    if(Full())
    {
        return false;
    }
    else
    {
        Contents[Back] = NewElement;
        Back = (Back + 1) % (QueueSize + 1);
        return true;
    }
}
#endif


First pass: You add PL1, remove PL1, add PL1. Queue now contains PL1.

Second pass: You add PL2, remove PL1 (which is at front), add PL1 (filename is updated by remove). Queue now contains PL2, PL1

Third pass: You add PL3, remove PL2 (which is at front), add PL2 (filename is updated by remove). Queue now contains PL1, PL3, PL2

And so on....

EDIT: The first 7 iterations

  • Add "PL1", Remove/Print "PL1", Add "PL1" - Queue: "PL1"
  • Add "PL2", Remove/Print "PL1", Add "PL1" - Queue: "PL2", "PL1"
  • Add "PL3", Remove/Print "PL2", Add "PL2" - Queue: "PL1", "PL3", "PL2"
  • Add "PL4", Remove/Print "PL1", Add "PL1" - Queue: "PL3", "PL2", "PL4", "PL1"
  • Add "PL5", Remove/Print "PL3", Add "PL3" - Queue: "PL2", "PL4", "PL1", "PL5", "PL3"
  • Add "PL6", Remove/Print "PL2", Add "PL2" - Queue: "PL4", "PL1", "PL5", "PL3", "PL6", "PL2"
  • Add "PL7", Remove/Print "PL4", Add "PL4" - Queue: "PL1", "PL5", "PL3", "PL6", "PL2", "PL7", "PL4"


Since you have not tagged this as homework, I suggest you use the std::queue.

It's been tested (thousands of times), it works, you don't have to write it, just use it.

See: http://www.cplusplus.com/reference/stl/queue/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜