【问题标题】:Linking Doubly linked list with Stack and Queue classes in C++将双向链表与 C++ 中的 Stack 和 Queue 类链接
【发布时间】:2017-09-21 01:35:55
【问题描述】:

所以我有一个任务,我需要创建一个双向链表,然后创建一个堆栈和队列类并从linkedlist 类继承以创建RPN calculator。到目前为止,我已经创建了我的双向链表类和另一个类。但是,我无法理解如何继承和使用带有堆栈和队列的链表类。我会提供我目前所拥有的。

我去补习了,没有太多帮助,所以我想我会寻求一些额外的帮助,不希望为我完成作业,而只是被指向正确的方向。

堆栈.h

using std::iterator;
using std::vector;
using std::string;

template<class T>
class Stack : public vector<T>
{
private:
    T getElement(bool erase);
    typename std::vector<T> ::iterator pEnd;
    T top;

public:
    Stack();
    T pop();
    T peek();
    void push(T elem);
};



template<class T>
Stack<T>::Stack()
{

}

template<class T>
void Stack<T>::push(T elem)
{
    this->push_back(elem);

}

template<class T>
T Stack<T>::peek()
{
    return this->getElement(false);
}

template<class T>
T Stack<T>::pop()
{
    return this->getElement(true);
}

template<class T>
T Stack<T>::getElement(bool erase)
{
    this->pEnd = this->end() - 1;
    T tmp;
    if (this->size() > 0)
    {
        tmp = *this->pEnd;
        if (erase) {
            this->erase(pEnd);
        }
    }

    return tmp;

}

队列.h

using namespace std;

class Queue
{

private:
    int items[MAXQUEUE];
    int head;
    int tail;

public:
    Queue();
    bool isEmpty();
    bool isFull();
    bool enqueue(int item);
    int dequeue();
    int peek();
};



Queue::Queue()
    :head(QEMPTY), tail(QEMPTY)
{

}

bool Queue::isEmpty()
{
    return this->head == this->tail;

}

bool Queue::isFull()
{

    return this->tail == MAXQUEUE;
}

bool Queue::enqueue(int item)
{
    if (this->isFull())
        return false;

    this->items[this->tail] = item;
    tail = (tail + 1) % MAXQUEUE;
    return true;

}

int Queue::dequeue()
{
    if (this->isEmpty())
        return EMPTY;

    int item = this->items[head];
    this->head = (this->head + 1) % MAXQUEUE;
    return item;
}

int Queue::peek() {

    return this->tail;
}

双链表.h

 using std::iterator;
using std::vector;
using std::string;



/*START OF NODE CLASS*/
/*---------------------------------------------*/
template<class T>
struct Node
{

    T Data;
    T Search;
    T value;
    Node<T>*Next;
    Node<T>*Prev;
};


template<class T>
class LinkedList
{
private:
    Node<T> *Head;
public:
    LinkedList();
    void addNode(T Data);
    void deleteNode(T Search);
    void insert(T Search, T value);
    void printListBackwards();
    void printListForwards();
};
template<class T>
LinkedList<T>::LinkedList()
{
    this->Head = NULL;

}

template<class T>
void LinkedList<T>::addNode(T data)
{

    if (Head == NULL)
    {
        Head = new Node<T>;
        Head->Data = data;
        Head->Next = NULL;
        Head->Prev = NULL;

    }
    else
    {
        Node<T>*p = this->Head;


        while (p->Next != NULL)

            p = p->Next;

        Node<T>*n = new Node<T>;
        n->Data = data;
        n->Next = NULL;
        p->Next = n;
        n->Prev = p;
    }


}

template<class T>
void LinkedList<T>::insert(T Search, T value)
{

    Node *p = Head;
    while (p->Data != Search)
    {
        p = p->Next;
    }

    Node *n = new Node;
    n->Data = value;
    n->Next = p->Next;
    p->Next = n;

}

template<class T>
void LinkedList<T>::deleteNode(T Search)
{
    Node *p = Head;
    while (p->Next->Data != Search)
    {
        p = p->Next;
    }

    Node *delPtr = p->Next;
    p->Next = p->Next->Next;

    delete delPtr;
}

template<class T>
void LinkedList<T>::printListBackwards()
{
    Node<T> *p = Head;

    while (p->Next != NULL)
    {

        p = p->Next;
    }
    while (p != NULL)
    {
        cout << p->Data<< endl;
        p = p->Prev;
    }
}

template<class T>
void LinkedList<T>::printListForwards()
{

    Node<T> *p = Head;
    while (p != NULL)
    {
        cout << p->Data << endl;
        p = p->Next;
    }
}

【问题讨论】:

  • 你有什么问题?不知道您到底哪里不明白,我们无法回答
  • @PasserBy 抱歉,但我应该使用从我的堆栈和队列类中的链表继承来添加到堆栈和队列,但我不知道该怎么做。

标签: c++ linked-list stack queue


【解决方案1】:

双向链表可以在头部或尾部添加,在尾部删除。

堆栈在一端压入(头?)并在同一端(头)弹出。

队列在一端(尾部)推送,在另一端(头部)弹出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多