简体   繁体   中英

C++ Stack using a doubly linked list

I'm trying to implement a stack using a doubly linked list. I know that the functions for my stack class (push, pop) should contain calls to member functions of my doubly linked list class, but I'm having trouble actually implementing that.

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;
}

Should the push function in stack basically be a call to appendNodeFront() or appendNodeback() ? Any help or advice is greatly appreciated!

You can create a stack class, then use linked list class as its container. In a linked list class there is virtually no limit to the number of items, so you add artificial limit to make it work like a stack. In a linked list, items can be added/removed anywhere in the list, you can limit add/remove the tail node only to make it work like stack. The example below demonstrate the usage.

Node that this is purely a programming exercise. Stack is relatively primitive compared to Doubly-linked list. Encapsulating a linked-list inside stack has no advantage. Also note, I declared all members as public for the sake of simplifying the problem, you may want to change some members to 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM