Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从头开始实现队列结构(C+;+;)_C++_Pointers_Data Structures_Queue_Dynamic Memory Allocation - Fatal编程技术网

C++ 从头开始实现队列结构(C+;+;)

C++ 从头开始实现队列结构(C+;+;),c++,pointers,data-structures,queue,dynamic-memory-allocation,C++,Pointers,Data Structures,Queue,Dynamic Memory Allocation,我必须从头开始为分配实现一个队列,而不使用任何预制库。当我将一元队列(只有一个元素)排入队列并打印它时,它工作得很好,但当我将一元队列(只有一个元素)排入队列并打印它时,并没有显示它是空的,打印函数实际上打印空白,当在整个列表退出队列后尝试将新元素排入队列时,它根本不会推送新元素。看起来对第一个元素的引用丢失了。如果有人能帮忙,下面是代码: 结构: typedef struct{ int number; }TItem; typedef struct cell{ struct cell

我必须从头开始为分配实现一个队列,而不使用任何预制库。当我将一元队列(只有一个元素)排入队列并打印它时,它工作得很好,但当我将一元队列(只有一个元素)排入队列并打印它时,并没有显示它是空的,打印函数实际上打印空白,当在整个列表退出队列后尝试将新元素排入队列时,它根本不会推送新元素。看起来对第一个元素的引用丢失了。如果有人能帮忙,下面是代码:

结构:

typedef struct{
  int number;
}TItem;

typedef struct cell{
  struct cell *pNext;
  TItem item;
}TCell;

typedef struct{
  TCell *pFirst;
  TCell *pLast;
}TQueue;
以及实施措施:

void Init(TQueue* pQueue){
    pQueue->pFirst = new TCell;
    pQueue->pLast = pQueue->pFirst;
    pQueue->pFirst->pNext = NULL;
} 

int Is_Empty(TQueue* pQueue){
    return (pQueue->pFirst == pQueue->pLast);
}

int Enqueue(TQueue* pQueue, TItem x){
    pQueue->pLast->pNext = new TCell;
    pQueue->pLast = pQueue->pLast->pNext;
    pQueue->pLast->item = x;
    pQueue->pLast->pNext = NULL;
    return 1;
}

int Dequeue(TQueue* pQueue, TItem* pX){
    if(Is_Empty(pQueue))
        return 0;
    TCell* aux;
    aux = pQueue->pFirst->pNext;
    *pX = aux->item;
    pQueue->pFirst->pNext = aux->pNext;
    delete aux;
    return 1;
}

void Print(TQueue* pQueue){
    if(Is_Empty(pQueue) == 1)
         cout << "EMPTY"<<endl;

    TCell *temp;
    temp = pQueue->pFirst->pNext;
    cout << "Queue:"<<endl;
    while( temp != NULL){
        cout <<  temp->item.number << " ";
        temp = temp->pNext;
    }
}
void Init(TQueue*pQueue){
PQUE->pFirst=新的TCell;
pQueue->pLast=pQueue->pFirst;
PQUE->pFirst->pNext=NULL;
} 
int为空(TQueue*pQueue){
返回(pQueue->pFirst==pQueue->pLast);
}
整数排队(TQueue*PQUE,滴度x){
pQueue->pLast->pNext=新的TCell;
pQueue->pLast=pQueue->pLast->pNext;
PQUE->pLast->item=x;
pQueue->pLast->pNext=NULL;
返回1;
}
整数出列(TQueue*pQueue,滴度*pX){
如果(是否为空(pQueue))
返回0;
TCell*aux;
aux=PQUE->pFirst->pNext;
*pX=辅助->项目;
PQUE->pFirst->pNext=aux->pNext;
删除aux;
返回1;
}
作废打印(TQUUE*PQUE){
如果(为空(pQueue)==1)

无法解决问题

您的出列逻辑无效。出列时只需删除队列中的第一项即可

解决方案

int Dequeue(TQueue* pQueue, TItem* pX){
    if(Is_Empty(pQueue))
        return 0;
    TCell* aux;
    //aux = pQueue->pFirst->pNext;
    aux = pQueue->pFirst;
    //*pX = aux->item;
    //pQueue->pFirst->pNext = aux->pNext;
    pQueue->pFirst = aux->pNext;
    delete aux;
    return 1;
}

另外,我不知道你为什么需要通过
TItem*pX
,因为你没有真正使用它。

我得到了它,谢谢。但我已经解决了它,但问题仍然存在。当我将所有元素出列并打印时,它应该显示为“空”除了空格和我提到的另一个问题仍然存在之外,所有元素的出列使得新元素的排队变得不可能,不会显示错误,但是打印这些新元素却什么也看不到。我使用
TItem*pX
,因为我需要“返回”被删除的元素