简体   繁体   English

队列的指针问题

[英]pointer problem with a queue

I am working on a homework assignment and have everything almost complete.我正在做家庭作业,一切都差不多完成了。 I am having trouble with a certain section in my code.我在代码中的某个部分遇到问题。 I have tried a few things and can not seem to figure out what i have done wrong or what i need to do to get it to work.我已经尝试了一些事情,但似乎无法弄清楚我做错了什么或者我需要做什么才能让它工作。

I am trying to implement a queue that takes in a specific object called QueueData.我正在尝试实现一个队列,该队列接收名为 QueueData 的特定 object。 When i execute the program it will run all the way up until line 44 in the P2Queue.cpp file.当我执行程序时,它将一直运行到 P2Queue.cpp 文件中的第 44 行。 The intention at this point in the code is to add the new QueueData object to the queue at the tail and then set the pointer of temp pointing at null, signifying the end of the queue.代码此时的意图是将新的QueueData object添加到队列尾部,然后设置temp指针指向null,表示队列结束。 The program stops running at line 44 in the file P2Queue.cpp.程序在文件 P2Queue.cpp 的第 44 行停止运行。 What am i doing wrong, what do i need to change, this is killing me.我做错了什么,我需要改变什么,这正在杀死我。

Thanks in advance, If you need in further clarification.提前致谢,如果您需要进一步说明。 please let me know.请告诉我。


P2Queue.cpp
#include"P2Queue.h"
#include<iostream>
#include<string.h>

using namespace std;

P2Queue::P2Queue() //: head(NULL), tail(NULL), count(0)
{
    head=NULL;
    tail=NULL;
    count=0;
}

P2Queue::~P2Queue()
{
    QueueData* temp;
    while(head!=NULL)
    {
        temp=head;
        head=temp->next;
        delete temp;
    }
    head=tail=NULL;
}

void P2Queue::Enqueue(int x, char *y)
{
    cout<<"I'm HERE in the function call"<<endl;
    QueueData* temp;
    temp=new QueueData();
    temp->num=x;
    cout<<temp->num<<endl;
    strcpy(temp->data, y);
    cout<<temp->data<<endl;
    temp->next=NULL;
    cout<<"LALALA"<<endl;
    tail->next=temp;
    cout<<"I'm here11!!"<<endl;
    tail=temp;
    cout<<"I'm here!!"<<endl;
}

void P2Queue::Enqueue(QueueData* ptr)
{
    tail->next=ptr;
    tail=ptr;
    tail->next=NULL;
}

QueueData* P2Queue::Dequeue()
{
    QueueData* temp;
    temp=new QueueData();
    temp=head;
    head=temp->next;
    return temp;
}

int P2Queue::QueueSize()
{
    QueueData* temp;
    temp=head;
    while(temp!=NULL)
    {
        temp=temp->next;
        count++;
    }
    delete temp;
    return count;
}

QueueData* P2Queue::getHead()
{
    return head;
}

QueueData* P2Queue::getTail()
{
    return tail;
}

P2Queue.h
#ifndef P2QUEUE_H
#define P2QUEUE_H

struct QueueData
{
    int num;
    char data[128];
    QueueData* next;
};

class P2Queue
{
    private:
        int count;
        QueueData* head;
        QueueData* tail;
    public:
        QueueData* getHead();
        QueueData* getTail();
        P2Queue();
        ~P2Queue();
        void Enqueue(int x, char* y);
        void Enqueue(QueueData* ptr);
        QueueData* Dequeue();
        int QueueSize();
};

#endif 

BSSim.cpp
#include"BSSim.h"
#include<stdio.h>
#include<string.h>
#include <sys/types.h>     
#include <sys/timeb.h>     
#include <time.h> 

using namespace std;

BSSim::BSSim()
{
    A = new P2Queue();
    B = new P2Queue();
    C = new P2Queue();
}

BSSim::~BSSim()
{
    delete A;
    delete B;
    delete C;
}

bool BSSim::getNextLine(char *line, int lineLen)
{
     bool    done = false;

    while(!done)
    {
        inFile.getline(line, lineLen);  // Read a line from the file 
        // Note: inFile is a private class variable

        if(inFile.good())    // If a line was successfully read
        {
           if(strlen(line) == 0)  // Skip any blank lines
                continue;
            else if(line[0] == '#')  // Skip any comment lines
                continue;
            else done = true;    // Got a valid data line so return with this line
        }
        else
        {
            strcpy(line, "");  // Clear the buffer array
            return false;      // Flag end of file
        }
    } // end while
    return true; // Flag a successful read
}

bool BSSim::runSimulation(char *cmdFile)
{
    inFile.open(cmdFile, ifstream::in);
    int    x;
    char   ch;
    char   ch2;
    char   cArray[32];
    char   str[128];
    struct _timeb   tStruct;
    double currenttime;
    double nexttime=0;
    bool done = false;

    if(!inFile.is_open())   // If the file was not opened successfully, bail out.
    {
        // inFile.is_open() returns false if the file could not be found or
        //    if for some other reason the open failed.
        cout << "Unable to open command file.\nProgram terminating.\n";
        return 0;
    }
    else
    {
        while(!done)
        {
            _ftime(&tStruct);
            currenttime = (tStruct.time*1000) + tStruct.millitm;
                if(currenttime>=nexttime)
                {
                    getNextLine(line, 128);
                    sscanf(line, "%s", cArray);
                    cout<<cArray<<endl;
                    cout<<"I'm here!"<<endl;
                    if(strcmp(cArray, "ENQUEUE")==0)
                        {
                            cout<<"I'm in the Enqueue if"<<endl;
                            sscanf(line, "%s %d %s", cArray, &x, str);
                            cout<<"HERE!"<<endl;
                            cout<<x<<" "<<str<<endl;
                            A->Enqueue(x, str);
                            cout<<"Enqueue A - ID="<<x<<", Data="<<str<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
                            nexttime=currenttime;
                            nexttime+=0.5;
                        }
                    else if(strcmp("DEQUEUE", cArray)==0)
                        {
                            sscanf(line, "%s %c", cArray, &ch);
                            if(ch=='A')
                            {
                                if(A->QueueSize()==0)
                                {
                                    cout<<"Queue A is empty"<<endl;
                                }
                                else
                                {
                                    sscanf(line, "%s %c %s %c", cArray, &ch, str, &ch2);
                                    if(ch2=='B')
                                    {
                                    B->Enqueue(A->Dequeue());
                                    cout<<"Dequeue from A into B - ID="<<B->getTail()->num<<", Data="<<B->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
                                    nexttime=currenttime;
                                    nexttime+=0.5;
                                    }
                                    else
                                    {
                                    C->Enqueue(A->Dequeue());
                                    cout<<"Dequeue from A into C - ID="<<C->getTail()->num<<", Data="<<C->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
                                    nexttime=currenttime;
                                    nexttime+=0.5;
                                    }
                                }
                            if(ch=='B')
                            {
                                if(B->QueueSize()==0)
                                {
                                    cout<<"Queue B is empty"<<endl;
                                }
                                else
                                {
                                B->Dequeue();
                                cout<<"Dequeue from B - ID="<<B->getTail()->num<<", Data="<<B->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
                                nexttime=currenttime;
                                nexttime+=0.5;
                                }
                            }
                            else
                            {
                                if(C->QueueSize()==0)
                                {
                                    cout<<"Queue C is empty"<<endl;
                                }
                                else
                                {
                                    C->Dequeue();
                                    cout<<"Dequeue from C - ID="<<C->getTail()->num<<", Data="<<C->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
                                    nexttime=currenttime;
                                    nexttime+=0.5;
                                }
                            }
                        }
                    else if(strcmp("NOACTION", cArray)==0)
                        {
                            cout<<"NO ACTION"<<endl;
                            nexttime=currenttime;
                            nexttime+=0.5;
                        }
                }
                else
                {
                    inFile.close();
                    cout<<"The simulation has terminated normally."<<endl;
                    return done;
                }

            }
        }
    }
}

BSSim.h
#ifndef BSSIM_H
#define BSSIM_H

#include"P2Queue.h"
#include<iostream>
#include<fstream>

using namespace std;

class BSSim
{
    private:
        P2Queue* A;
        P2Queue* B;
        P2Queue* C;
        ifstream inFile;
        char line[128];
    public:
        BSSim();
        ~BSSim();
        bool runSimulation(char* cmdFile);
        bool getNextLine(char* line, int lineLen);
};

#endif 

#include"BSSim.h"
#include"P2Queue.h"

using namespace std;
void main()
{
    BSSim x;
    x.runSimulation("SimData.txt");
}

Example data file:
ENQUEUE 1234 QueueTest_01
ENQUEUE 2345 QueueTest_02
ENQUEUE 3456 QueueTest_03
ENQUEUE 4567 QueueTest_04
ENQUEUE 5678 QueueTest_05
ENQUEUE 6789 QueueTest_06
NOACTION
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE B
DEQUEUE C
NOACTION

Initially the queue is empty and head == tail == NULL.最初队列是空的,head == tail == NULL。

What happens you enqueue the first item?将第一个项目排入队列会发生什么?

From what I can see you implementation of the queue is complete wrong.从我可以看到你的队列实现是完全错误的。 You can find information on how to implement it here: http://en.wikipedia.org/wiki/Queue_(data_structure) .您可以在此处找到有关如何实现它的信息: http://en.wikipedia.org/wiki/Queue_(data_structure)

For ex: this doesn't make any sense例如:这没有任何意义


void P2Queue::Enqueue(QueueData* ptr)
{
    tail->next=ptr;
    tail=ptr;
    tail->next=NULL;
}

First it will crush or do something strange when the list is empty.首先,当列表为空时,它会粉碎或做一些奇怪的事情。 Second it adds just one element and removes all existing elements from the queue.其次,它只添加一个元素并从队列中删除所有现有元素。

it should be at least like this:它至少应该是这样的:


void P2Queue::Enqueue(QueueData* ptr)
{
    ptr->next = tail;
    tail = ptr;
}

I believe your problem, like others have said, is that your program is not handling when the queue is empty.我相信您的问题,就像其他人所说的那样,是当队列为空时您的程序没有处理。 What you need to do is add a check into your function for if the queue is empty.您需要做的是在您的 function 中添加一个检查队列是否为空。 If the queue is empty you need to handle the Enqueue a little differently.如果队列为空,您需要稍微不同地处理 Enqueue。 I believe this will fix your issue.我相信这将解决您的问题。 Below is some pseudo code that will possibly help you.下面是一些可能对您有所帮助的伪代码。 Let me know if this helps!让我知道这是否有帮助! <3 <3

void P2Queue::Enqueue(int x, char *y)
{
    QueueData* temp;
    temp=new QueueData();
    temp->num=x;
    strcpy(temp->data, y);
    temp->next=NULL;

    if(queue is empty) {
        head = temp;
        tail = temp;
    }
    else {
        tail->next = temp;
        tail = temp;
    }
}

void P2Queue::Enqueue(QueueData* ptr)
{
    ptr->next = NULL
    if(queue is empty) {
        head = ptr;
        tail = ptr;
    }
    else {
        tail->next = ptr;
        tail = ptr;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM