使用双重链表的c++栈

C++ Stack using a doubly linked list

本文关键字:c++ 链表      更新时间:2023-10-16

我正在尝试使用双重链表实现堆栈。我知道堆栈类(push, pop)的函数应该包含对双重链表类的成员函数的调用,但我在实际实现时遇到了麻烦。

dlist.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include "dlist.hpp"
using namespace std;
void dlist::appendNodeFront(int shares, float pps){
  Node *n = new Node(shares, pps);
  if(front == NULL){
    front = n;
    back = n;
  }
  else {
    front->prev = n;
    n->next = front;
    front = n;
  }
}
void dlist::appendNodeBack(int shares, float pps){
  Node *n = new Node(shares, pps);
  if(back == NULL){
    front = n;
    back = n;
  }
  else {
    back->next = n;
    n->prev = back;
    back = n;
  }
}
void dlist::display(){
  Node  *temp = front;
  cout << "List contents: ";
  while(temp != NULL){
    cout << temp->value << " ";
    temp = temp->next;
  }
  cout << endl;
}
void dlist::display_reverse(){
  Node *temp = back;
  cout << "List contents in reverse: ";
  while(temp != NULL){
    cout << temp->value << " ";
    temp = temp->prev;
  }
  cout << endl;
}
void dlist::destroyList(){
  Node *T = back;
  while(T != NULL){
    Node *T2 = T;
    T = T->prev;
    delete T2;
  }
  front = NULL;
  back = NULL;
}

stack.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include "stack.hpp"
using namespace std;
stack::stack(){
  int i;
  for(i = 0; i < 1500; i++){
    shares[i] = 0;
    pps[i] = 0;
  }
  first = 0;
}
void stack::push(int num, float price){
  if(first ==(1500-1)){
    cout << "Stack is full" << endl;
    return;
  }
  first++;
  shares[first] = num;
  pps[first] = price;
  return;
}
void stack::pop(int *num, float *price){
  if(first == -1){
    cout << "Stack is empty" << endl;
    return;
  }
  num = &shares[first];
  price = &pps[first];
  cout << shares[first] << endl;
  cout << pps[first] << endl;
  shares[first] = 0;
  pps[first] = 0;
  first--;
  return;
}

栈中的push函数应该是对appendNodeFront()appendNodeback()的调用吗?任何帮助或建议是非常感谢!

您可以创建一个堆栈类,然后使用链表类作为其容器。在链表类中,实际上对项目的数量没有限制,因此您添加了人为限制,使其像堆栈一样工作。在链表中,项目可以在列表的任何地方添加/删除,您可以限制添加/删除尾节点,使其像堆栈一样工作。下面的示例演示了这种用法。

Node表示这纯粹是一个编程练习。与双链表相比,堆栈是相对原始的。将链表封装在堆栈中没有任何好处。还要注意,为了简化问题,我将所有成员声明为public,您可能希望将一些成员更改为protected/private

#include <iostream>
#include <fstream>
#include <string>
using std::cout;
class Node
{
public:
    Node *prev;
    Node *next;
    int shares;
    float pps;
    Node(int vshares, float vpps)
    {
        shares = vshares;
        pps = vpps;
        prev = next = nullptr;
    }
};
class dlist
{
public:
    Node *head;
    Node *tail;
    dlist()
    {
        head = tail = nullptr;
    }
    ~dlist()
    {
        destroy();
    }
    void push_back(int shares, float pps) 
    {
        Node *node = new Node(shares, pps);
        if (head == NULL) 
        {
            head = tail = node;
        }
        else 
        {
            tail->next = node;
            node->prev = tail;
            tail = node;
        }
    }
    void destroy() 
    {
        Node *walk = head;
        while (walk) 
        {
            Node *node = walk;
            walk = walk->next;
            delete node;
        }
        head = tail = nullptr;
    }
};
class stack
{
public:
    int maxsize;
    int count;
    dlist list;
    stack(int size)
    {
        count = 0;
        maxsize = size;
    }
    void push(int num, float price)
    {
        if (count < maxsize)
        {
            list.push_back(num, price);
            count++;
        }
    }
    void pop()
    {
        Node *tail = list.tail;
        if (!tail)
        {
            //already empty
            return;
        }
        if (tail == list.head)
        {
            //only one element in the list
            delete tail;
            list.head = list.tail = nullptr;
            count--;
        }
        else
        {
            Node *temp = list.tail->prev;
            delete list.tail;
            list.tail = temp;
            list.tail->next = nullptr;
            count--;
        }
    }
    void display()
    {
        Node  *walk = list.head;
        while (walk)
        {
            cout << "(" << walk->shares << "," << walk->pps << ") ";
            walk = walk->next;
        }
        cout << "n";
    }
};
int main()
{
    stack s(3);
    s.push(101, 0.25f);
    s.push(102, 0.25f);
    s.push(103, 0.25f);
    s.push(104, 0.25f);
    s.display();
    s.pop();
    s.display();
    return 0;
}