队列的指针问题

发布于 2024-11-18 22:59:42 字数 10253 浏览 2 评论 0原文

我正在做家庭作业,一切都快完成了。我的代码中的某个部分遇到了问题。我尝试了一些事情,但似乎无法弄清楚我做错了什么或我需要做什么才能让它发挥作用。

我正在尝试实现一个接受名为 QueueData 的特定对象的队列。当我执行该程序时,它将一直运行到 P2Queue.cpp 文件中的第 44 行。代码此时的目的是将新的 QueueData 对象添加到队列的尾部,然后将 temp 的指针设置为空,表示队列的末尾。程序在文件 P2Queue.cpp 的第 44 行处停止运行。我做错了什么,我需要改变什么,这简直要了我的命。

提前致谢!如果您需要进一步说明,请告诉我。


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

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. When i execute the program it will run all the way up until line 44 in the P2Queue.cpp file. 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. The program stops running at line 44 in the file P2Queue.cpp. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画▽骨i 2024-11-25 22:59:42

最初队列是空的并且 head == tail == NULL。

将第一个项目放入队列会发生什么?

Initially the queue is empty and head == tail == NULL.

What happens you enqueue the first item?

攀登最高峰 2024-11-25 22:59:42

据我所知,您对队列的实现是完全错误的。您可以在此处找到有关如何实现它的信息:http://en.wikipedia.org/ wiki/Queue_(数据结构)

例如:这没有任何意义


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

首先,当列表为空时,它会压碎或做一些奇怪的事情。其次,它仅添加一个元素并从队列中删除所有现有元素。

它至少应该是这样的:


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

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).

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;
}

吾家有女初长成 2024-11-25 22:59:42

我相信您的问题,就像其他人所说的那样,是您的程序在队列为空时不进行处理。您需要做的是在函数中添加检查队列是否为空。如果队列为空,则需要以稍微不同的方式处理入队。我相信这会解决您的问题。下面是一些可能对您有帮助的伪代码。让我知道这是否有帮助! <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;
    }
}

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. If the queue is empty you need to handle the Enqueue a little differently. I believe this will fix your issue. Below is some pseudo code that will possibly help you. Let me know if this helps! <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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文