簡體   English   中英

使用雙向鏈表的C ++ Stack

[英]C++ Stack using a doubly linked list

我正在嘗試使用雙向鏈表實現堆棧。 我知道我的堆棧類(推,彈出)的函數應該包含對我的雙向鏈接列表類的成員函數的調用,但是我很難實現它。

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()的調用嗎? 任何幫助或建議,我們將不勝感激!

您可以創建一個堆棧類,然后將鏈接列表類用作其容器。 在鏈接列表類中,項目數實際上沒有限制,因此您添加了人工限制以使其像堆棧一樣工作。 在鏈接列表中,可以在列表中的任何位置添加/刪除項目,您可以限制添加/刪除尾節點,僅使其像堆棧一樣工作。 下面的示例演示用法。

這純粹是編程練習的節點。 與雙向鏈接列表相比,堆棧相對原始。 將鏈接列表封裝在堆棧中沒有優勢。 另請注意,為了簡化問題,我將所有成員都聲明為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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM