Before I start, I want to clarify something : YES A STACK DATA STRUCTURE CAN BE DONE WITH STANDARD PB LINKED LIST.
There a prototype of a Stack data structure implementation. See the source code for the YouTube link about where the inspiration came from.
Best regards
StarBootics
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Data Structure - Stack
; File Name : Stack - OOP.pb
; File version: 1.0.0
; Programming : Prototype
; Programmed by : StarBootics
; Date : June 9th, 2021
; Last Update : June 9th, 2021
; PureBasic code : V5.73 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Based on the code presented by the YouTube channel
;
; https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ
; Video link : https://www.youtube.com/watch?v=B31LgI4Y4DQ
; Video name : Data Structures - Full Course Using C And C++
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
DeclareModule Stack
Interface Stack
Push(Value.l)
Pop()
Top.l()
IsEmpty.b()
Free()
EndInterface
Declare.i New()
EndDeclareModule
Module Stack
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Structures declaration <<<<<
Structure Node
Value.l
*Link.Node
EndStructure
Structure Private_Members
VirtualTable.i
*Top.Node
EndStructure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Push operator <<<<<
Procedure Push(*This.Private_Members, Value.l)
*Temp.Node = AllocateStructure(Node)
*Temp\Value = Value
*Temp\Link = *This\Top
*This\Top = *Temp
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Pop operator <<<<<
Procedure Pop(*This.Private_Members)
If *This\Top = #Null
; MessageRequester("Stack Error", "The Stack is already Empty !")
ProcedureReturn 0
EndIf
*Temp.Node = *This\Top
*This\Top = *This\Top\Link
FreeStructure(*Temp)
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Top operator <<<<<
Procedure.l Top(*This.Private_Members)
If *This\Top <> #Null
ProcedureReturn *This\Top\Value
Else
MessageRequester("Stack Error", "The Stack is Empty !")
EndIf
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The IsEmpty operator <<<<<
Procedure.b IsEmpty(*This.Private_Members)
If *This\Top = #Null
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Destructor <<<<<
Procedure Free(*This.Private_Members)
While IsEmpty(*This) <> #True
Pop(*This)
Wend
FreeStructure(*This)
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Constructor <<<<<
Procedure.i New()
*This.Private_Members = AllocateStructure(Private_Members)
*This\VirtualTable = ?START_METHODS
*This\Top = #Null
ProcedureReturn *This
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The VirtualTable entries <<<<<
DataSection
START_METHODS:
Data.i @Push()
Data.i @Pop()
Data.i @Top()
Data.i @IsEmpty()
Data.i @Free()
END_METHODS:
EndDataSection
EndModule
CompilerIf #PB_Compiler_IsMainFile
MyStack.Stack::Stack = Stack::New()
MyStack\Push(25)
MyStack\Push(20)
MyStack\Push(15)
While MyStack\IsEmpty() <> #True
Debug MyStack\Top()
MyStack\Pop()
Wend
MyStack\Free()
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<