2016-10-19 17 views
0

二重リンクリストを使用してスタックを実装しようとしています。私は私のスタッククラス(push、pop)の関数は、二重リンクリストクラスのメンバ関数への呼び出しを含むべきであることを知っていますが、実際にそれを実装する際に問題があります。二重リンクリストを使用したC++スタック

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

スタックのプッシュ機能は、基本的にappendNodeFront()またはappendNodeback()への呼び出しであるべき?どんな助けや助言も大歓迎です!

+0

プッシュにいくつかのメンバーを変更したい場合がありappendNodebackので()コールする適切な関数です。 pop操作では、スタックに最後に挿入された要素を削除する必要があるため、removeLastNode()関数を実装する必要があります。 – StaticBeagle

+0

これらの機能には責任が混在しているようです。たとえば、ノードを作成してどこかに追加します。責任を分け合わせると、事柄はより簡単になります。ちょっと、そのコンセプトにも[独自のWikipediaのページ](https://en.wikipedia.org/wiki/Single_responsibility_principle)があります!ウワ –

+0

2つのシナリオ:ヘッドからポップ/ポップ、またはテールからポップ/ポップをテールから押します。要点は、最後に挿入された要素が最初に削除された要素(LIFO)であることです。 – 0x499602D2

答えて

0

スタッククラスを作成し、リンクされたリストクラスをそのコンテナとして使用できます。リンクされたリストクラスでは、アイテムの数に事実上制限がないので、人為的な制限を追加してスタックのように動作させます。リンクされたリストでは、アイテムはリスト内のどこにでも追加/削除できます。テールノードの追加/削除をスタックのようにするためにのみ制限できます。以下の例は、その使い方を示しています。

ノードこれは純粋にプログラミングの練習です。スタックは、二重リンクリストと比較して比較的プリミティブです。スタック内のリンクリストをカプセル化することは利点がありません。また、私は問題を簡単にするため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; 
} 
+0

"リンクリストクラスではアイテムの数に事実上制限がないので、スタックのように動作させるために人為的な制限を追加します" - リストには制限がありませんが、スタックには制限がありません。 –

関連する問題