鏈表格儲存體實現棧

來源:互聯網
上載者:User

標籤:進棧 出棧

基本原理是:

進棧的過程中,拆開頭結點和第一個節點,插入進棧元素節點

出棧的過程中,刪除第一個節點

 class CStackLinkList{private:    SNode* pCStackLinkListHeader;    int nLinkListNodeSize;public:    CStackLinkList()    {        pCStackLinkListHeader=NULL;        nLinkListNodeSize=0;    }    ~CStackLinkList()    {        //judge header pointer is null and return        if(NULL==pCStackLinkListHeader) return;        //I can use pop function to delete all node        int tmp=0;        while(PopStack(tmp)==0)        {            cout<<"Now free node value="<<tmp<<endl;        }        cout<<"Now empty all node and release all resource"<<endl;    }    int PushStack(int nPuashStackValue)    {        //allocate node memory        SNode* pSNode=(SNode*)malloc(sizeof(SNode));        //judge whether the pointer is not NULL        if(NULL==pSNode)    return -1;        //assign value to node        pSNode->nNodeValue=nPuashStackValue;        //define tmpNode pointer to store header        SNode* pTmpNode=pCStackLinkListHeader;        //insert this node between header pointer and the first node pointer        pCStackLinkListHeader=pSNode;        pSNode->pNextNode=pTmpNode;        //node count increase one        nLinkListNodeSize=nLinkListNodeSize+1;        return 0;    }    int PopStack(int& nPopStackValue)    {        //if linklist header pointer is null,and return -1        if(NULL==pCStackLinkListHeader) return -1;        //define tmpnode store header pointer        SNode* pTmpNode=pCStackLinkListHeader;        //take apart linklist        //nPopStackValue=pCStackLinkListHeader->nNodeValue;        //pCStackLinkListHeader=pCStackLinkListHeader->pNextNode;        nPopStackValue=pTmpNode->nNodeValue;        pCStackLinkListHeader=pTmpNode->pNextNode;        nLinkListNodeSize=nLinkListNodeSize-1;        free(pTmpNode);        pTmpNode=NULL;        return 0;    }    int GetTop(int& nStackTopValue)    {        if(NULL==pCStackLinkListHeader) return -1;        nStackTopValue=pCStackLinkListHeader->nNodeValue;        return 0;    }};

鏈表格儲存體實現棧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.