資料結構 鏈式棧

2021-07-25 01:47:37 字數 2346 閱讀 8404

編譯錯誤:passing 『const linkstack』 as 『this』 argument discards qualifiers [-fpermissive]

解決方法:c++中const 引用的是物件時只能訪問該物件的const 函式,因為其他函式有可能會修改該物件的成員,編譯器為了避免該類事情發生,會認為呼叫非const函式是錯誤的。

函式末尾加const,表示不會修改該物件的成員。

// stack_node.h

#ifndef stack_node_h

#define stack_node_h

template> class linkstack;

template> class stacknode

private:

type m_data;

stacknode<

type

>

*m_pnext;

};#endif

// stack_node_h

// link_stack.h

#ifndef link_stack_h

#define link_stack_h

#include

"stack_node.h"

#include

template> class linkstack

~linkstack()

public:

void makeempty();

void push(const type item);

type pop();

type gettop() const;

void print();

bool isempty() const

private:

stacknode<

type

>

*m_ptop;

};template>

void linkstack<

type

>

::makeempty()

stacknode<

type

>

*pdel =

null;

while(m_ptop !=

null)

}template>

void linkstack<

type

>

::push(const type item)

template>

type linkstack<

type

>

::gettop() const

return m_ptop->m_data;

}template>

type linkstack<

type

>

::pop()

stacknode<

type

>

*pdel = m_ptop;

m_ptop = m_ptop->m_pnext;

type temp = pdel->m_data;

delete pdel;

return temp;

}template>

void linkstack<

type

>

::print()

std::cout

<<

"--->top"

<< std::endl

<< std::endl

<< std::endl;

}#endif

// link_stack_h

// main.cpp

#include

#include "link_stack.h"

using

namespace

std;

int main(int argc, char *argv)

; for(int i = 0; i < 10; i++)

stack.print();

cout

<< stack.pop() << endl;

stack.print();

cout

<< stack.gettop() << endl;

stack.print();

cout

<< stack.pop() << endl;

stack.print();

stack.makeempty();

stack.print();

stack.pop();

return

0;}

資料結構之鏈式棧

好久不見,前面我們學過了資料結構的順序棧。今天我們來學習下鏈式棧的實現,鏈式棧的話,還是要利用前面我們實現的鏈式鍊錶,不知道鏈式鍊錶的,出門左轉,前面就有介紹。第七個例子,鏈式棧的實現 注 把我們先前實現的鏈式鍊錶的標頭檔案和實現檔案包含進來 標頭檔案 ifndef linkstack h defi...

資料結構棧(鏈式實現)

真正的棧操作是在棧頂,這裡給出的棧是帶了頭結點的棧,也就是說head next代表圖示棧頂,head next data是1 實現 include include typedef int datatype typedef struct snode lsnode 初始化帶頭結點的鏈式堆疊,初始化函式中...

資料結構 鏈式棧 Linked stack

08年9月入學,12年7月畢業,結束了我在軟體學院愉快豐富的大學生活。此系列是對四年專業課程學習的回顧,索引參見 棧是只能在某一端插入和刪除的特殊線性表。它按照後進先出的原則儲存資料,先進入的資料被壓入棧底 push 最後的資料在棧頂 top 需要讀資料的時候從棧頂開始彈出資料 top 最後乙個資料...