棧的基礎實現

2021-08-26 08:00:11 字數 2771 閱讀 4178

棧有後進先出(last in ,first out ,lifo)的特性,即放到棧中的最後乙個專案將第乙個被刪除。

定義棧的異常處理類:

#ifndef stack_exception_h_ #define stack_exception_h_ #include#includeusing namespace std; class stackexception :public logic_error }; #endif

棧基於陣列的實現:header file stack_array.h

//the adt stack is based on an array //header file stack_array.h for adt stack //arrary based implementation #ifndef stack_array_h_ #define stack_array_h_ #include"stack_exception.h" const int max=30; templateclass stack ; #endif

#include"stack_array.h" templatestack::stack() templatebool stack::isempty()const templatevoid stack::push(t newitem)throw(stackexception) } templatevoid stack::pop()throw(stackexception) templatevoid stack::pop(t &stacktop)throw(stackexception) } templatevoid stack::gettop(t&stacktop)const throw(stackexception)

基於動態分配陣列的實現:

#ifndef stack_dynamic_h_ #define stack_dynamic_h_ #include"stack_exception.h" const int max=20; templateclass stack ; #endif #include#include#include"stack_dynamic.h" templatestack::stack() templatestack::stack(const stack &astack) } templatestack::~stack() templatebool stack::isempty()const templatevoid stack::push(t newitem)throw(stackexception) } else } templatevoid stack::pop()throw(stackexception) templatevoid stack::pop(t &topstack)throw(stackexception) } templatevoid stack::gettop(t &topstack)const throw(stackexception)

基於指標的棧的實現:

#ifndef stack_pointer_h_ #define stack_pointer_h_ #include"stack_exception.h" templateclass stack ; stacknode *topptr; }; #endif #include"stack_pointer.h" #include#includetemplatestack::stack() templatestack::stack(const stack&astack) newptr->next=null; } } templatestack::~stack() templatebool stack::isempty()const templatevoid stack::push(t newitem)throw(stackexception) } templatevoid stack::pop()throw(stackexception) } templatevoid stack::pop(t &stacktop)throw(stackexception) } templatevoid stack::gettop(t & stacktop )const throw(stackexception)

基於adt列表的棧的實現:

#ifndef stack_adt_h_ #define stack_adt_h_ #include#include"stack_exception.h" using namespace std; templateclass stack ; #endif

#include"stack_adt.h" templatestack::stack() templatestack::stack(const stack&astack) templatestack::~stack() templatebool stack::isempty()const templatevoid stack::push(t newitem)throw(stackexception) catch(...) } templatevoid stack::pop()throw(stackexception) catch(...) } templatevoid stack::pop(t &topstack)throw(stackexception) catch(...) } templatevoid stack::gettop(t & topstack)throw(stackexception) catch(...) }

棧的實現方式比較:

基於陣列的實現使用靜態分配記憶體,如果棧已滿,則push操作無法將新項新增棧中。如果不能接受這個限制,則必須使用動態記憶體分配的陣列或基於指標的實現。假定使用基於指標的實現,是選擇使用鍊錶實現還是選擇使用adt列表實現,使用adt列表表示棧不如直接使用鍊錶有效,而使用adt列表可實現**的重用,而且易於編寫。

棧基礎操作1 順序棧的實現

top指標指的是乙個空格 include define maxsize 200 順序棧最大長度 using namespace std typedef int selemtype 資料型別typedef struct sqstack 順序棧頂留空 1.初始化 bool initstack sqsta...

棧的基礎知識 Python實現

本文首發於我的個人部落格suixin s blog棧 stack,堆疊 是限制在表的一端進行插入和刪除運算的線性表,通常稱插入 刪除的這一端為棧頂 top 另一端為棧底 bottom 先進後出 filo top 1時為空棧,top 0只能說明棧中只有乙個元素,並且元素進棧時top應該自增。順序儲存棧...

棧 棧的實現

棧是一種線性儲存結構,棧中資料是按照 先進後出 方式進出棧,向棧中新增 刪除資料時,只能從棧頂進行操作。include using namespace std template class t class arraystack 建構函式 template class t arraystack arr...