簡體   English   中英

使用指針表示堆棧

[英]Representing a stack using pointers

我似乎無法理解C ++中的指針,希望我的示例可以幫助您指導我,我有以下代碼:

   #include "stdafx.h"
   #include <iostream>

   using namespace std;

頭等艙:

    class Node
    {
    private:
        int value;
        Node *next;
    public:
        Node()
       {
            value = 0;
       }
       ~Node()
       {

       }
       void setValue(int x)
       {
           value = x;
        }
        int getValue()
        {
           return value;
        }
        void setNext(Node x)
        {
            next = &x;
        }
        Node getNext()
        {
            return *next;
        }
     };

第二類:

    class Stack
    {
    private:
       Node *top;
    public:
        Stack()
        {
           top = NULL;
        }
        ~Stack()
        {

        }
        Node pop()
        {
            Node *temp = top;
            top = &top->getNext();
            return *temp;
        }
        void push(Node &x)
        {
            x.setNext(getTop());
            setTop(x);
        }
        Node getTop()
        {
            return *top;
        }
        void setTop(Node x)
        {
            top = &x;
        }
    };

和主要功能:

    int main()
    {
       Node a;
       Node b;

       a.setValue(1);
       b.setValue(2);

       Stack s;

       s.push(a);
       s.push(b);

       cout << s.pop().getValue() << endl;
       cout << s.pop().getValue() << endl;
       return 0;
   }

我一直在獲取隨機數,我知道我在使用對象和指針時遇到了問題,只是無法弄清楚我的問題是什么

為成員變量的參數分配指針是完全沒有用的,因為自函數返回時參數將消失,並且指針將變得毫無意義。

class Node
{
private:
    int value;
    Node *next;
public:
    Node()
   {
        value = 0;
   }
   ~Node()
   {

   }
   void setValue(int x)
   {
       value = x;
    }
    int getValue()
    {
       return value;
    }
    void setNext(Node* x)      // setNext takes a pointer, not a Node by value.
    {
        next = x;
    }

    Node *getNext()
    {
        return next;
    }
};

class Stack
{
private:
   Node *top;
public:
    Stack()
    {
       top = NULL;
    }
    ~Stack()
    {

    }
    Node pop()
    {
        Node *temp = top;
        top = top->getNext();
        return *temp;
    }
    void push(Node &x)
    {
        x.setNext(getTop());
        setTop(&x);
    }
    Node *getTop()
    {
        return top;
    }
    void setTop(Node *x)    // setTop takes a pointer, not a Node by value.
    {
        top = x;
    }
};

恐怕此實現可能會對應用程序造成危險,但這將在此處起作用。

Node::setNextNode::getNextStack::getTopStack::setTop都返回/傳遞值,這些值有時超出范圍。 應該有引用,這將導致您的堆棧指向原始Node aNode b ,當不再使用它們時,它們將在main的末尾超出范圍。

但這都是錯誤的。 您應該將副本存儲在堆上,而不僅僅是指向具有自動存儲持續時間的對象的指針。

使您的獲取器/設置器返回指針而不是對象。 這段代碼:

        Node *temp = top;
        top = &top->getNext();
        return *temp;

temp分配給top ,然后將top分配給由getNext()創建的臨時對象的地址,然后返回temp指向的對象。 下次使用top將導致不確定的行為,因為top現在是一個懸空的指針。

節點:

class Node
{
private:
    int value;
    Node *next;
public:
    Node()
   {
        value = 0;
   }
   ~Node()
   {

   }
   void setValue(int x)
   {
       value = x;
    }
    int getValue()
    {
       return value;
    }
    void setNext(Node* x)
    {
        next = x;
    }
    Node* getNext()
    {
        return next;
    }
 };

堆:

class Stack
{
private:
   Node *top;
public:
    Stack()
    {
       top = NULL;
    }
    ~Stack()
    {

    }
    Node pop()
    {
        Node *temp = top;
        top = top->getNext();
        return *temp;
    }
    void push(Node* x)
    {
        x->setNext(getTop());
        setTop(x);
    }
    Node* getTop()
    {
        return top;
    }
    void setTop(Node* x)
    {
        top = x;
    }
};

暫無
暫無

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

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