仅使用数组创建Deque

时间:2015-02-06 18:40:42

标签: c++ arrays deque

我的任务是在c ++中只使用数组创建一个双端队列,并且在弹出正面或背面时会有点挂起(删除双端队列的头部或尾部的元素。我更喜欢Java并且我对c ++不太满意因此我不知道这是不是我不了解语言或者我的逻辑不正确的问题。顺便提一下我的代码。方法pop前面是问题。< / p>

#include <iostream>
#include <stdio.h>

//#include "DeQue.h"
using namespace std;

class Deque{
private:
    std::string *queue;             //The array which holds the queue
    std::string deque[];
    int numElements;                //The number of elements in the queue
    int sizeOfQueue;                //The capacity of the queue
    int front;                      //Points to the front of the queue
    int back;                       //Points to the back of the queue

public:
    //Constructor
    Deque() {
        string deque[8] = {"a","b","c"};
        this->numElements = 3;
        this->sizeOfQueue = 8;
        this->front = 0;
        this->back = 2;

    }

    //Destructor
    ~Deque() {

    }

    //Inserts the element at the front of the queue.
    void push_front(std::string item){
        if(front==0){
            deque[sizeOfQueue-1] = item;
            front = sizeOfQueue-1;
        }
        else if(front >= 1 && front < sizeOfQueue - 1)
            deque[front-1] = item;
            front--;
    }

    //Inserts the element at the back of the queue
    void push_back(std::string item) {
        if(back==sizeOfQueue-1){
            deque[0] = item;
            back = 0;
        }
        else if(back < sizeOfQueue-1 && back >= 0){
            deque[back+1] = item;
            back++;
        }
    }

    //Deletes the element at the front of the queue
    std::string pop_front() {
        delete deque[front];
        if(front == sizeOfQueue-1)
            front = 0;
        else
            front++;
    }

就发布问题而言,我是stackoverflow的新手,所以如果我能更好地格式化这个问题,请告诉我。提前感谢您的帮助。

0 个答案:

没有答案