栈的实现

来源:互联网 发布:centos 主机加固 编辑:程序博客网 时间:2024/04/29 10:18
隐藏行号 复制代码 .h
  1. #ifndef __STACK_H
  2. #define __STACK_H
  3. /*array stack*/
  4. namespace dskit
  5. {
  6.     struct StackRecord;
  7.     typedef struct StackRecord* Stack;
  8.     
  9.     #ifndef NULL
  10.     #define NULL (0)
  11.     #endif
  12.     typedef char ElementType;
  13.     typedef unsigned size_t; 
  14.     
  15.     bool IsEmpty(Stack s);
  16.     bool IsFull(Stack s);
  17.     Stack CreateStack(size_t MaxElements);
  18.     void DisposeStack(Stack s);
  19.     bool Push(ElementType x, Stack s);
  20.     ElementType Pop(Stack s);
  21.     ElementType Top(Stack s);
  22.     #define EmptyTOS (-1)
  23.     #define MinStackSize (5)
  24.     struct StackRecord
  25.     {
  26.         int Capacity;
  27.         int TopOfStack;
  28.         ElementType* Array;
  29.     };
  30. }
  31. #endif
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}
隐藏行号 复制代码 .cpp
  1. #include "stack.h"
  2. #include 
  3. #include 
  4. bool dskit::IsEmpty(Stack s)
  5. {
  6.     return s->TopOfStack == EmptyTOS;
  7. }
  8. bool dskit::IsFull(Stack s)
  9. {
  10.     return s->TopOfStack == s->Capacity;
  11. }
  12. dskit::Stack dskit::CreateStack(size_t MaxElements)
  13. {
  14.     if(MaxElements < MinStackSize)
  15.         MaxElements = MinStackSize;
  16.     
  17.     Stack S;
  18.     
  19.     S = (struct StackRecord*)malloc(sizeof(struct StackRecord));
  20.     if(NULL == S)
  21.         return NULL;
  22.     S->Array = (ElementType*) malloc(sizeof(ElementType) * MaxElements );
  23.     if(NULL == S->Array)
  24.     {
  25.         free(S);
  26.         return NULL;
  27.     }
  28.     S->Capacity = MaxElements;
  29.     S->TopOfStack = EmptyTOS;
  30.     return S;
  31. }
  32. void dskit::DisposeStack(Stack s)
  33. {
  34.     if(NULL != s)
  35.     {
  36.         free(s->Array);
  37.         free(s);
  38.     }
  39. }
  40. bool dskit::Push(ElementType x, Stack s)
  41. {
  42.     if(IsFull(s))
  43.         return false;
  44.     else
  45.     {
  46.         printf("%c was pushed./n", x);
  47.         return s->Array[++s->TopOfStack] = x;    
  48.     }
  49.         
  50. }
  51. dskit::ElementType dskit::Pop(Stack s)
  52. {
  53.     if(!IsEmpty(s))
  54.         return s->Array[s->TopOfStack--];
  55.     else
  56.     {
  57.         printf("Pop error/n");
  58.         exit(-1);
  59.     }
  60. }
  61. dskit::ElementType dskit::Top(Stack s)
  62. {
  63.     if(!IsEmpty(s))
  64.         return s->Array[s->TopOfStack];
  65.     else
  66.     {
  67.         printf("Top error/n");
  68.         exit(-1);
  69.     }
  70. }
  71. /*
  72. int main(int argc, char* argv[])
  73. {
  74.     dskit::Stack s = dskit::CreateStack(50);
  75.     dskit::Push(12, s);
  76.     dskit::Push(13, s);
  77.     dskit::Push(14, s);
  78.     
  79.     std::cout << dskit::Pop(s) << '/t' << std::endl;
  80.     std::cout << dskit::Pop(s) << '/t' << std::endl;
  81.     std::cout << dskit::Pop(s) << '/t' << std::endl;
  82.     dskit::DisposeStack(s);
  83. }
  84. */
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}