[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[游戏娱乐] 批处理线性结构类库

本帖最后由 老刘1号 于 2020-4-29 11:24 编辑

介绍
让你在批处理编程中优雅的使用线性结构。
代码&文档
约定:
        <必选参数> [可选参数]
        默认传入变量名,有?包裹代表可传入数值、可传入变量名。
        返回值在!ErrorLevel!,默认0为成功,其它为失败。

LinearList_LSS_SLL.txt
库名:线性表
实现:
        链式储存结构-单向链表(含头结点)。
        (Linked Storage Structure - Singly Linked List)
结点字段:
        Data 储存结点的数据。
        Next 指向结点对应的下一个结点。
用法:Call LinearList_LSS_SLL 函数名 参数
函数:
        :Init <ListName>
                建立空的线性表。
                返回后,变量ListName即为头指针,头指针指向头结点。
        :Clear <?ListName?>
                清空线性表。
        elete <ListName>
                删除线性表。
                变量ListName也会被删除。
        :IsEmpty <?ListName?>
                判断线性表是否为空表。
        :GetLength <?ListName?>
                返回线性表的元素个数(不计头结点)。
        :GetNextNodePtr <?NodePtr?>
                返回下一个结点的指针,0表示不存在下个结点。
        :InsertNextNode        <?NodePtr?> <VarToInsert>
                在当前指针指向结点后插入新结点。
                将返回新结点的指针。
        :DeleteNextNode <?NodePtr?> [VarToSaveNextNodeElemValue]
                删除指针指向结点的后一结点,可以返回后一结点的值。
        :GetNodeElem <?NodePtr?> <VarToSaveElemValue>
                得到指针指向结点的值。
        :EditNodeElem <?NodePtr?> <VarToReplaceElemValue>
                修改指针指向结点的值。

LinearList_LSS_SLL.bat
  1. ::Code by OldLiu
  2. ::老刘编写
  3. ::Start
  4. Set "_TMP_Arguments_=%*"
  5. If "!_TMP_Arguments_:~,1!" Equ ":" (
  6. Set "_TMP_Arguments_=!_TMP_Arguments_:~1!"
  7. )
  8. Call :LinearList_LSS_SLL_!_TMP_Arguments_!
  9. Set _TMP_Arguments_=
  10. Goto :Eof
  11. ::LinearList_LSS_SLL Begin
  12. :LinearList_LSS_SLL_GetRandom
  13. Set /A ErrorLevel=%random%%%10000+%random%*10000
  14. If !ErrorLevel! Equ 0 Goto GetRandom
  15. If Defined Memory[!ErrorLevel!].Data Goto GetRandom
  16. Goto :Eof
  17. :LinearList_LSS_SLL_Init ListName
  18. Call :LinearList_LSS_SLL_GetRandom
  19. Set /A %~1=ErrorLevel
  20. Set Memory[!ErrorLevel!].Data=ListHead
  21. Set /A Memory[!ErrorLevel!].Next=0
  22. Set /A ErrorLevel=0
  23. Goto :Eof
  24. :LinearList_LSS_SLL_Clear ListName
  25. Call :LinearList_LSS_SLL_DeleteNextNode "%~1"
  26. If !ErrorLevel! Equ 0 Goto :LinearList_LSS_SLL_Clear
  27. Set /A ErrorLevel=0
  28. Goto :Eof
  29. :LinearList_LSS_SLL_Delete ListName
  30. Call :LinearList_LSS_SLL_Clear "%~1"
  31. Set Memory[!%~1!].Data=
  32. Set Memory[!%~1!].Next=
  33. Set /A ErrorLevel=0
  34. Goto :Eof
  35. :LinearList_LSS_SLL_GetLength ListName
  36. Set /A _LinearList_LSS_SLL_GetLength_TMP_ListLength_=0
  37. Set /A _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=%~1
  38. :LinearList_LSS_SLL_GetLength_Loop
  39. Call :LinearList_LSS_SLL_GetNextNodePtr _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_
  40. Set /A _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=ErrorLevel
  41. If !_LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_! Neq 0 (
  42. Set /A _LinearList_LSS_SLL_GetLength_TMP_ListLength_+=1
  43. Goto LinearList_LSS_SLL_GetLength_Loop
  44. )
  45. Set /A ErrorLevel=_LinearList_LSS_SLL_GetLength_TMP_ListLength_
  46. Set _LinearList_LSS_SLL_GetLength_TMP_ListLength_=
  47. Set _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=
  48. Goto :Eof
  49. :LinearList_LSS_SLL_IsEmpty ListName
  50. :LinearList_LSS_SLL_GetNextNodePtr NodePtr
  51. Set /A _LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_=%~1
  52. Set /A ErrorLevel=Memory[!_LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_!].Next
  53. Set _LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_=
  54. Goto :Eof
  55. :LinearList_LSS_SLL_InsertNextNode NodePtr VarToInsert
  56. Set /A _LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_=%~1
  57. Set /A _LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_=Memory[!_LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_!].Next
  58. Call :LinearList_LSS_SLL_GetRandom
  59. Set /A Memory[!_LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_!].Next=ErrorLevel
  60. Set /A Memory[!ErrorLevel!].Next=_LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_
  61. Set "Memory[!ErrorLevel!].Data=!%~2!"
  62. Set _LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_=
  63. Set _LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_=
  64. Goto :Eof
  65. :LinearList_LSS_SLL_DeleteNextNode NodePtr [VarToSaveNextNodeElemValue]
  66. Set /A _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=%~1
  67. Set /A _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_!].Next
  68. If !_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_! Equ 0 (
  69. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=
  70. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=
  71. Set /A ErrorLevel=1
  72. Goto :Eof
  73. )
  74. Set /A Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_!].Next=Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Next
  75. If "%~2" Neq "" Call Set "%~2=%%Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Data%%"
  76. Set Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Next=
  77. Set Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Data=
  78. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=
  79. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=
  80. Set /A ErrorLevel=0
  81. Goto :Eof
  82. :LinearList_LSS_SLL_GetNodeElem NodePtr VarToSaveElemValue
  83. Set /A LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_=%~1
  84. Call Set "%~2=%%Memory[!LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_!].Data%%"
  85. Set LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_=
  86. Set /A ErrorLevel=0
  87. Goto :Eof
  88. :LinearList_LSS_SLL_EditNodeElem NodePtr VarToReplaceElemValue
  89. Set /A LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_=%~1
  90. Set "Memory[!LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_!].Data=!%~2!"
  91. Set LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_=
  92. Set /A ErrorLevel=0
  93. Goto :Eof
  94. ::LinearList_LSS_SLL End
复制代码
LinearList_SSS.txt
库名:线性表
实现:
        顺序储存结构
        (Sequence Storage Structure)
用法:Call LinearList_SSS 函数名 参数
函数:
        :Init <ListName> <?MaxLength?>
                初始化操作,建立一个空的线性表。
                表最多可储存MaxLength个元素。
        :Clear <ListName>
                清空线性表。
        :Delete <ListName>
                删除线性表。
        :GetElem <ListName> <?Index?> <VarToReturn>
                将线性表中第Index(从1开始)个元素的值返回。
        :EditElem <ListName> <?Index?> <VarToReplaceWith>
                修改线性表中第Index个位置的元素值。

LinearList_SSS.bat
  1. ::Code by OldLiu
  2. ::老刘编写
  3. ::Start
  4. Set "_TMP_Arguments_=%*"
  5. If "!_TMP_Arguments_:~,1!" Equ ":" (
  6. Set "_TMP_Arguments_=!_TMP_Arguments_:~1!"
  7. )
  8. Call :LinearList_SSS_!_TMP_Arguments_!
  9. Set _TMP_Arguments_=
  10. Goto :Eof
  11. ::LinearList_SSS Begin
  12. :LinearList_SSS_Init ListName MaxLength
  13. Set /A "%~1.MaxLength=%~2"
  14. Set /A ErrorLevel=0
  15. Goto :Eof
  16. :LinearList_SSS_Clear ListName
  17. For /L %%a In (1 1 !%~1.MaxLength!) Do (
  18. Set "%~1.Element[%%a]="
  19. )
  20. Set /A ErrorLevel=0
  21. Goto :Eof
  22. :LinearList_SSS_Delete ListName
  23. Call :LinearList_SSS_Clear "%~1"
  24. Set "%~1.MaxLength="
  25. Set /A ErrorLevel=0
  26. Goto :Eof
  27. :LinearList_SSS_GetElem ListName Index VarToReturn
  28. Set /A _LinearList_SSS_GetElem_TMP_Index=%~2
  29. If !_LinearList_SSS_GetElem_TMP_Index! Lss 1 (Set /A ErrorLevel=1 & Goto :Eof)
  30. If !_LinearList_SSS_GetElem_TMP_Index! Gtr !%~1.MaxLength! (Set /A ErrorLevel=1 & Goto :Eof)
  31. Call Set "%~3=%%%~1.Element[!_LinearList_SSS_GetElem_TMP_Index!]%%"
  32. Set _LinearList_SSS_GetElem_TMP_Index=
  33. Set /A ErrorLevel=0
  34. Goto :Eof
  35. :LinearList_SSS_EditElem ListName Index VarToReplaceWith
  36. Set /A _LinearList_SSS_EditElem_TMP_Index=%~2
  37. If !_LinearList_SSS_EditElem_TMP_Index! Lss 1 (Set /A ErrorLevel=1 & Goto :Eof)
  38. If !_LinearList_SSS_EditElem_TMP_Index! Gtr !%~1.MaxLength! (Set /A ErrorLevel=1 & Goto :Eof)
  39. Set "%~1.Element[!_LinearList_SSS_EditElem_TMP_Index!]=!%~3!"
  40. Set _LinearList_SSS_EditElem_TMP_Index=
  41. Set /A ErrorLevel=0
  42. Goto :Eof
  43. ::LinearList_SSS End
复制代码
Queue_LSS.txt
库名:队列
实现:
        链式储存结构-单向链表(含头结点)。
        (Linked Storage Structure - Singly Linked List)
用法:Call Queue_LSS 函数名 参数
函数:
        :Init <QueueName>
                建立空的队列。
        :Clear <?QueueName?>
                清空队列。
        :Delete <QueueName>
                删除队列。
                变量QueueName也会被删除。
        :IsEmpty <?QueueName?>
                判断队列是否为空。
        :Enqueue <?QueueName?> <VarToInsert>
                将VarToInsert插入队列。
        :Dequeue <?QueueName?> [VarToReturn]
                将队头数据元素从队列中删除并返回。
        eep <?QueueName?> <VarToReturn>
                返回队头元素但不删除。

Queue_LSS.BAT
  1. ::Code by OldLiu
  2. ::老刘编写
  3. ::Start
  4. Set "_TMP_Arguments_=%*"
  5. If "!_TMP_Arguments_:~,1!" Equ ":" (
  6. Set "_TMP_Arguments_=!_TMP_Arguments_:~1!"
  7. )
  8. Call :Queue_LSS_!_TMP_Arguments_!
  9. Set _TMP_Arguments_=
  10. Goto :Eof
  11. ::Queue_LSS(require LinearList_LSS_SLL) Begin
  12. :Queue_LSS_Init QueueName
  13. Call :LinearList_LSS_SLL_Init "%~1"
  14. Call :LinearList_LSS_SLL_EditNodeElem "%~1" "%~1"
  15. Goto :Eof
  16. :Queue_LSS_Clear QueueName
  17. Call :LinearList_LSS_SLL_Clear "%~1"
  18. Set /A _Queue_LSS_Clear_TMP_HeadNodePtr=%~1
  19. Call :LinearList_LSS_SLL_EditNodeElem "%~1" "_Queue_LSS_Clear_TMP_HeadNodePtr"
  20. Set _Queue_LSS_Clear_TMP_HeadNodePtr=
  21. Goto :Eof
  22. :Queue_LSS_Delete QueueName
  23. Call :LinearList_LSS_SLL_Delete "%~1"
  24. Goto :Eof
  25. :Queue_LSS_IsEmpty QueueName
  26. Call :LinearList_LSS_SLL_IsEmpty "%~1"
  27. Goto :Eof
  28. :Queue_LSS_Enqueue QueueName VarToInsert
  29. Call :LinearList_LSS_SLL_GetNodeElem "%~1" _Queue_LSS_Enqueue_TMP_QueueRearPtr
  30. Call :LinearList_LSS_SLL_InsertNextNode _Queue_LSS_Enqueue_TMP_QueueRearPtr "%~2"
  31. Call :LinearList_LSS_SLL_EditNodeElem "%~1" ErrorLevel
  32. Goto :Eof
  33. :Queue_LSS_Dequeue QueueName [VarToReturn]
  34. Call :LinearList_LSS_SLL_DeleteNextNode "%~1" "%~2"
  35. If !ErrorLevel! Neq 0 Goto :Eof
  36. Call :Queue_LSS_IsEmpty "%~1"
  37. If !ErrorLevel! Equ 0 Call :Queue_LSS_Clear "%~1"
  38. Set /A ErrorLevel=0
  39. Goto :Eof
  40. :Queue_LSS_Peep QueueName VarToReturn
  41. Call :LinearList_LSS_SLL_GetNextNodePtr "%~1"
  42. Call :LinearList_LSS_SLL_GetNodeElem ErrorLevel "%~2"
  43. Goto :Eof
  44. ::Queue_LSS End
  45. ::LinearList_LSS_SLL Begin
  46. :LinearList_LSS_SLL_GetRandom
  47. Set /A ErrorLevel=%random%%%10000+%random%*10000
  48. If !ErrorLevel! Equ 0 Goto GetRandom
  49. If Defined Memory[!ErrorLevel!].Data Goto GetRandom
  50. Goto :Eof
  51. :LinearList_LSS_SLL_Init ListName
  52. Call :LinearList_LSS_SLL_GetRandom
  53. Set /A %~1=ErrorLevel
  54. Set Memory[!ErrorLevel!].Data=ListHead
  55. Set /A Memory[!ErrorLevel!].Next=0
  56. Set /A ErrorLevel=0
  57. Goto :Eof
  58. :LinearList_LSS_SLL_Clear ListName
  59. Call :LinearList_LSS_SLL_DeleteNextNode "%~1"
  60. If !ErrorLevel! Equ 0 Goto :LinearList_LSS_SLL_Clear
  61. Set /A ErrorLevel=0
  62. Goto :Eof
  63. :LinearList_LSS_SLL_Delete ListName
  64. Call :LinearList_LSS_SLL_Clear "%~1"
  65. Set Memory[!%~1!].Data=
  66. Set Memory[!%~1!].Next=
  67. Set /A ErrorLevel=0
  68. Goto :Eof
  69. :LinearList_LSS_SLL_GetLength ListName
  70. Set /A _LinearList_LSS_SLL_GetLength_TMP_ListLength_=0
  71. Set /A _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=%~1
  72. :LinearList_LSS_SLL_GetLength_Loop
  73. Call :LinearList_LSS_SLL_GetNextNodePtr _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_
  74. Set /A _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=ErrorLevel
  75. If !_LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_! Neq 0 (
  76. Set /A _LinearList_LSS_SLL_GetLength_TMP_ListLength_+=1
  77. Goto LinearList_LSS_SLL_GetLength_Loop
  78. )
  79. Set /A ErrorLevel=_LinearList_LSS_SLL_GetLength_TMP_ListLength_
  80. Set _LinearList_LSS_SLL_GetLength_TMP_ListLength_=
  81. Set _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=
  82. Goto :Eof
  83. :LinearList_LSS_SLL_IsEmpty ListName
  84. :LinearList_LSS_SLL_GetNextNodePtr NodePtr
  85. Set /A _LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_=%~1
  86. Set /A ErrorLevel=Memory[!_LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_!].Next
  87. Set _LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_=
  88. Goto :Eof
  89. :LinearList_LSS_SLL_InsertNextNode NodePtr VarToInsert
  90. Set /A _LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_=%~1
  91. Set /A _LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_=Memory[!_LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_!].Next
  92. Call :LinearList_LSS_SLL_GetRandom
  93. Set /A Memory[!_LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_!].Next=ErrorLevel
  94. Set /A Memory[!ErrorLevel!].Next=_LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_
  95. Set "Memory[!ErrorLevel!].Data=!%~2!"
  96. Set _LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_=
  97. Set _LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_=
  98. Goto :Eof
  99. :LinearList_LSS_SLL_DeleteNextNode NodePtr [VarToSaveNextNodeElemValue]
  100. Set /A _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=%~1
  101. Set /A _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_!].Next
  102. If !_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_! Equ 0 (
  103. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=
  104. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=
  105. Set /A ErrorLevel=1
  106. Goto :Eof
  107. )
  108. Set /A Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_!].Next=Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Next
  109. If "%~2" Neq "" Call Set "%~2=%%Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Data%%"
  110. Set Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Next=
  111. Set Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Data=
  112. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=
  113. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=
  114. Set /A ErrorLevel=0
  115. Goto :Eof
  116. :LinearList_LSS_SLL_GetNodeElem NodePtr VarToSaveElemValue
  117. Set /A LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_=%~1
  118. Call Set "%~2=%%Memory[!LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_!].Data%%"
  119. Set LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_=
  120. Set /A ErrorLevel=0
  121. Goto :Eof
  122. :LinearList_LSS_SLL_EditNodeElem NodePtr VarToReplaceElemValue
  123. Set /A LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_=%~1
  124. Set "Memory[!LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_!].Data=!%~2!"
  125. Set LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_=
  126. Set /A ErrorLevel=0
  127. Goto :Eof
  128. ::LinearList_LSS_SLL End
复制代码
Stack_LSS.txt
库名:堆栈
实现:
        链式储存结构-单向链表(含头结点)。
        (Linked Storage Structure - Singly Linked List)
        头结点的Next指向栈顶结点。
结点字段:
        Data 储存结点的数据。
        Next 指向结点对应的下一个结点。
用法:Call Stack_LSS 函数名 参数
函数:
        :Init <StackName>
                初始化操作,建立一个空的堆栈。
        :Clear <?StackName?>
                清空堆栈。
        :Delete <StackName>
                删除堆栈。
        :IsEmpty <?StackName?>
                判断堆栈是否为空。
        :GetLength <?StackName?>
                ErrorLevel返回堆栈中的元素个数。
        :Push <?StackName?> <VarToPush>
                变量入栈。
        :Pop <?StackName?> <VarToReturn>
                变量出栈。
        :GetTopElem <?StackName?> <VarToReturn>
                得到栈顶元素。
        :EditTopElem <?StackName?> <VarToReplaceWith>
                修改栈顶元素。

Stack_LSS.bat
  1. ::Code by OldLiu
  2. ::老刘编写
  3. ::Start
  4. Set "_TMP_Arguments_=%*"
  5. If "!_TMP_Arguments_:~,1!" Equ ":" (
  6. Set "_TMP_Arguments_=!_TMP_Arguments_:~1!"
  7. )
  8. Call :Stack_LSS_!_TMP_Arguments_!
  9. Set _TMP_Arguments_=
  10. Goto :Eof
  11. ::Stack_LSS(require LinearList_LSS_SLL) Begin
  12. :Stack_LSS_Init StackName
  13. Call :LinearList_LSS_SLL_Init "%~1"
  14. Goto :Eof
  15. :Stack_LSS_Clear StackName
  16. Call :LinearList_LSS_SLL_Clear "%~1"
  17. Goto :Eof
  18. :Stack_LSS_Delete StackName
  19. Call :LinearList_LSS_SLL_Delete "%~1"
  20. Goto :Eof
  21. :Stack_LSS_IsEmpty StackName
  22. Call :LinearList_LSS_SLL_IsEmpty "%~1"
  23. Goto :Eof
  24. :Stack_LSS_GetLength StackName
  25. Call :LinearList_LSS_SLL_GetLength "%~1"
  26. Goto :Eof
  27. :Stack_LSS_Push StackName VarToPush
  28. Call :LinearList_LSS_SLL_InsertNextNode "%~1" "%~2"
  29. Set /A ErrorLevel=0
  30. Goto :Eof
  31. :Stack_LSS_Pop StackName VarToReturn
  32. Call :LinearList_LSS_SLL_DeleteNextNode "%~1" "%~2"
  33. Goto :Eof
  34. :Stack_LSS_GetTopElem StackName VarToReturn
  35. Call :LinearList_LSS_SLL_GetNextNodePtr "%~1"
  36. If !ErrorLevel! Equ 0 (
  37. Set /A ErrorLevel=1
  38. Goto :Eof
  39. )
  40. Call :LinearList_LSS_SLL_GetNodeElem ErrorLevel "%~2"
  41. Goto :Eof
  42. :Stack_LSS_EditTopElem StackName VarToReplaceWith
  43. Call :LinearList_LSS_SLL_GetNextNodePtr "%~1"
  44. If !ErrorLevel! Equ 0 (
  45. Set /A ErrorLevel=1
  46. Goto :Eof
  47. )
  48. Call :LinearList_LSS_SLL_EditNodeElem ErrorLevel "%~2"
  49. Goto :Eof
  50. ::Stack_LSS End
  51. ::LinearList_LSS_SLL Begin
  52. :LinearList_LSS_SLL_GetRandom
  53. Set /A ErrorLevel=%random%%%10000+%random%*10000
  54. If !ErrorLevel! Equ 0 Goto GetRandom
  55. If Defined Memory[!ErrorLevel!].Data Goto GetRandom
  56. Goto :Eof
  57. :LinearList_LSS_SLL_Init ListName
  58. Call :LinearList_LSS_SLL_GetRandom
  59. Set /A %~1=ErrorLevel
  60. Set Memory[!ErrorLevel!].Data=ListHead
  61. Set /A Memory[!ErrorLevel!].Next=0
  62. Set /A ErrorLevel=0
  63. Goto :Eof
  64. :LinearList_LSS_SLL_Clear ListName
  65. Call :LinearList_LSS_SLL_DeleteNextNode "%~1"
  66. If !ErrorLevel! Equ 0 Goto :LinearList_LSS_SLL_Clear
  67. Set /A ErrorLevel=0
  68. Goto :Eof
  69. :LinearList_LSS_SLL_Delete ListName
  70. Call :LinearList_LSS_SLL_Clear "%~1"
  71. Set Memory[!%~1!].Data=
  72. Set Memory[!%~1!].Next=
  73. Set /A ErrorLevel=0
  74. Goto :Eof
  75. :LinearList_LSS_SLL_GetLength ListName
  76. Set /A _LinearList_LSS_SLL_GetLength_TMP_ListLength_=0
  77. Set /A _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=%~1
  78. :LinearList_LSS_SLL_GetLength_Loop
  79. Call :LinearList_LSS_SLL_GetNextNodePtr _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_
  80. Set /A _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=ErrorLevel
  81. If !_LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_! Neq 0 (
  82. Set /A _LinearList_LSS_SLL_GetLength_TMP_ListLength_+=1
  83. Goto LinearList_LSS_SLL_GetLength_Loop
  84. )
  85. Set /A ErrorLevel=_LinearList_LSS_SLL_GetLength_TMP_ListLength_
  86. Set _LinearList_LSS_SLL_GetLength_TMP_ListLength_=
  87. Set _LinearList_LSS_SLL_GetLength_TMP_Next_Node_Ptr_=
  88. Goto :Eof
  89. :LinearList_LSS_SLL_IsEmpty ListName
  90. :LinearList_LSS_SLL_GetNextNodePtr NodePtr
  91. Set /A _LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_=%~1
  92. Set /A ErrorLevel=Memory[!_LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_!].Next
  93. Set _LinearList_LSS_SLL_GetNextNodePtr_TMP_NodePtr_=
  94. Goto :Eof
  95. :LinearList_LSS_SLL_InsertNextNode NodePtr VarToInsert
  96. Set /A _LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_=%~1
  97. Set /A _LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_=Memory[!_LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_!].Next
  98. Call :LinearList_LSS_SLL_GetRandom
  99. Set /A Memory[!_LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_!].Next=ErrorLevel
  100. Set /A Memory[!ErrorLevel!].Next=_LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_
  101. Set "Memory[!ErrorLevel!].Data=!%~2!"
  102. Set _LinearList_LSS_SLL_InsertNextNode_TMP_NextNodePtr_=
  103. Set _LinearList_LSS_SLL_InsertNextNode_TMP_NodePtr_=
  104. Goto :Eof
  105. :LinearList_LSS_SLL_DeleteNextNode NodePtr [VarToSaveNextNodeElemValue]
  106. Set /A _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=%~1
  107. Set /A _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_!].Next
  108. If !_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_! Equ 0 (
  109. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=
  110. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=
  111. Set /A ErrorLevel=1
  112. Goto :Eof
  113. )
  114. Set /A Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_!].Next=Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Next
  115. If "%~2" Neq "" Call Set "%~2=%%Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Data%%"
  116. Set Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Next=
  117. Set Memory[!_LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_!].Data=
  118. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_Next_Node_Ptr_=
  119. Set _LinearList_LSS_SLL_DeleteNextNode_TMP_NodePtr_=
  120. Set /A ErrorLevel=0
  121. Goto :Eof
  122. :LinearList_LSS_SLL_GetNodeElem NodePtr VarToSaveElemValue
  123. Set /A LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_=%~1
  124. Call Set "%~2=%%Memory[!LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_!].Data%%"
  125. Set LinearList_LSS_SLL_GetNodeElem_TMP_NodePtr_=
  126. Set /A ErrorLevel=0
  127. Goto :Eof
  128. :LinearList_LSS_SLL_EditNodeElem NodePtr VarToReplaceElemValue
  129. Set /A LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_=%~1
  130. Set "Memory[!LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_!].Data=!%~2!"
  131. Set LinearList_LSS_SLL_EditNodeElem_TMP_NodePtr_=
  132. Set /A ErrorLevel=0
  133. Goto :Eof
  134. ::LinearList_LSS_SLL End
复制代码
Stack_SSS.txt
库名:堆栈
实现:
        顺序储存结构
        (Sequence Storage Structure)
用法:Call Stack_SSS 函数名 参数
函数:
        :Init <StackName> <?MaxLength?>
                初始化操作,建立一个空的堆栈。
                堆栈最多可储存MaxLength个元素。
        :Clear <StackName>
                清空堆栈。
        :Delete <StackName>
                删除堆栈。
        :IsEmpty <StackName>
                判断堆栈是否为空。
        :IsFull <StackName>
                判断堆栈是否已满。
        :GetLength <StackName>
                ErrorLevel返回堆栈中的元素个数。
        :Push <StackName> <VarToPush>
                变量入栈。
        :Pop <StackName> <VarToReturn>
                变量出栈。
        :GetTopElem <StackName> <VarToReturn>
                得到栈顶元素。
        :EditTopElem <StackName> <VarToReplaceWith>
                修改栈顶元素。

Stack_SSS.bat
  1. ::Code by OldLiu
  2. ::老刘编写
  3. ::Start
  4. Set "_TMP_Arguments_=%*"
  5. If "!_TMP_Arguments_:~,1!" Equ ":" (
  6. Set "_TMP_Arguments_=!_TMP_Arguments_:~1!"
  7. )
  8. Call :Stack_SSS_!_TMP_Arguments_!
  9. Set _TMP_Arguments_=
  10. Goto :Eof
  11. ::Stack_SSS(require LinearList_SSS) Begin
  12. :Stack_SSS_Init StackName MaxLength
  13. Call :LinearList_SSS_Init "%~1" "%~2"
  14. Set /A %~1.StackTop=0
  15. Goto :Eof
  16. :Stack_SSS_Clear StackName
  17. Call :LinearList_SSS_Clear "%~1"
  18. Set /A %~1.StackTop=0
  19. Goto :Eof
  20. :Stack_SSS_Delete StackName
  21. Call :LinearList_SSS_Delete "%~1"
  22. Set %~1.StackTop=
  23. Goto :Eof
  24. :Stack_SSS_GetLength StackName
  25. :Stack_SSS_IsEmpty StackName
  26. Set /A ErrorLevel=%~1.StackTop
  27. Goto :Eof
  28. :Stack_SSS_IsFull StackName
  29. Set /A ErrorLevel=%~1.MaxLength-%~1.StackTop
  30. Goto :Eof
  31. :Stack_SSS_Push StackName VarToPush
  32. Call :Stack_SSS_IsFull "%~1"
  33. If "!ErrorLevel!" Equ "0" (
  34. Set /A ErrorLevel=1
  35. Goto :Eof
  36. )
  37. Set /A %~1.StackTop+=1
  38. Call :Stack_SSS_EditTopElem "%~1" "%~2"
  39. Set /A ErrorLevel=0
  40. Goto :Eof
  41. :Stack_SSS_Pop StackName VarToReturn
  42. Call :Stack_SSS_IsEmpty "%~1"
  43. If "!ErrorLevel!" Equ "0" (
  44. Set /A ErrorLevel=1
  45. Goto :Eof
  46. )
  47. Call :Stack_SSS_GetTopElem "%~1" "%~2"
  48. Set /A %~1.StackTop-=1
  49. Set /A ErrorLevel=0
  50. Goto :Eof
  51. :Stack_SSS_GetTopElem StackName VarToReturn
  52. Call :LinearList_SSS_GetElem "%~1" "%~1.StackTop" "%~2"
  53. Goto :Eof
  54. :Stack_SSS_EditTopElem StackName VarToReplaceWith
  55. Call :LinearList_SSS_EditElem "%~1" "%~1.StackTop" "%~2"
  56. Goto :Eof
  57. ::Stack_SSS End
  58. ::LinearList_SSS Begin
  59. :LinearList_SSS_Init ListName MaxLength
  60. Set /A "%~1.MaxLength=%~2"
  61. Set /A ErrorLevel=0
  62. Goto :Eof
  63. :LinearList_SSS_Clear ListName
  64. For /L %%a In (1 1 !%~1.MaxLength!) Do (
  65. Set "%~1.Element[%%a]="
  66. )
  67. Set /A ErrorLevel=0
  68. Goto :Eof
  69. :LinearList_SSS_Delete ListName
  70. Call :LinearList_SSS_Clear "%~1"
  71. Set "%~1.MaxLength="
  72. Set /A ErrorLevel=0
  73. Goto :Eof
  74. :LinearList_SSS_GetElem ListName Index VarToReturn
  75. Set /A _LinearList_SSS_GetElem_TMP_Index=%~2
  76. If !_LinearList_SSS_GetElem_TMP_Index! Lss 1 (Set /A ErrorLevel=1 & Goto :Eof)
  77. If !_LinearList_SSS_GetElem_TMP_Index! Gtr !%~1.MaxLength! (Set /A ErrorLevel=1 & Goto :Eof)
  78. Call Set "%~3=%%%~1.Element[!_LinearList_SSS_GetElem_TMP_Index!]%%"
  79. Set _LinearList_SSS_GetElem_TMP_Index=
  80. Set /A ErrorLevel=0
  81. Goto :Eof
  82. :LinearList_SSS_EditElem ListName Index VarToReplaceWith
  83. Set /A _LinearList_SSS_EditElem_TMP_Index=%~2
  84. If !_LinearList_SSS_EditElem_TMP_Index! Lss 1 (Set /A ErrorLevel=1 & Goto :Eof)
  85. If !_LinearList_SSS_EditElem_TMP_Index! Gtr !%~1.MaxLength! (Set /A ErrorLevel=1 & Goto :Eof)
  86. Set "%~1.Element[!_LinearList_SSS_EditElem_TMP_Index!]=!%~3!"
  87. Set _LinearList_SSS_EditElem_TMP_Index=
  88. Set /A ErrorLevel=0
  89. Goto :Eof
  90. ::LinearList_SSS End
复制代码
算法实例
堆栈应用:批处理版逆波兰四则计算器
单向循环链表应用:约瑟夫问题
快慢指针:腾讯面试题-寻找单链表的中间节点
队列应用:一元多项式相加算法
2

评分人数

明天来我司【震惊部】上班吧
QQ 33892006

TOP

本帖最后由 老刘1号 于 2020-4-10 21:46 编辑

腾讯面试题:快速找到未知长度单链表的中间节点。
中间节点(n∈N*)
        表长为2n        中间节点为n
        表长为2n-1        中间结点为n
End

快慢指针解法.BAT
  1. @Echo off
  2. rem 参数:头指针数值
  3. Set /A _TMP_Ptr_Slow_=_TMP_Ptr_Fast_=%~1,_TMP_Index_=0
  4. Goto J
  5. :Loop
  6. Set /A _TMP_Index_+=1
  7. Set /A _TMP_Ptr_Slow_=Memory[!_TMP_Ptr_Slow_!].Next
  8. Set /A _TMP_Ptr_Fast_=Memory[!_TMP_Ptr_Fast_!].Next
  9. Set /A _TMP_Ptr_Fast_=Memory[!_TMP_Ptr_Fast_!].Next
  10. :J
  11. For %%a in (Memory[!_TMP_Ptr_Fast_!].Next) Do (
  12. If !%%a! Neq 0 (
  13. Goto Loop
  14. ) Else (
  15. Goto Print
  16. )
  17. )
  18. For %%a in (Memory[!_TMP_Ptr_Fast_!].Next) Do (
  19. For %%b in (Memory[!%%a!].Next) Do (
  20. If !%%b! Neq 0 (
  21. Goto Loop
  22. ) Else (
  23. Set /A _TMP_Index_+=1
  24. )
  25. )
  26. )
  27. :Print
  28. Echo !_TMP_Index_!
复制代码

TOP

返回列表