• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 用數組實現順序棧

    發表于:2007-05-25來源:作者:點擊數: 標簽:數組這是實現順序
    這是《C++數據結構與程序設計》上的例子,作了些微變動 /* mystack.h */ enum Error_code { success, overflow, underflow }; constint maxstack = 100; classmystack { public: mystack(); Error_codepush(const Stack_entry item); Error_codepop(); Error
    這是《C++數據結構與程序設計》上的例子,作了些微變動

    /* mystack.h */
    enum Error_code
    {
     suclearcase/" target="_blank" >ccess,
     overflow,
     underflow
    };

    const int maxstack = 100;

    class mystack
    {
    public:
     mystack();
     Error_code push(const Stack_entry &item);
     Error_code pop();
     Error_code top(Stack_entry &item) const;
     bool empty() const;
     
    private:
     int count;
     Stack_entry entry[maxstack];
    };

    /* mystack.cpp */
    typedef char Stack_entry;
    #include "mystack.h"
    Error_code mystack::push(const Stack_entry &item)
    {
     Error_code outcome = success;
     if(count < maxstack)
     {
      entry[count++] = item;
     }
     else
     {
      outcome = overflow;
     }
     return outcome;
    }

    Error_code mystack::pop()
    {
     Error_code outcome = success;
     if(count == 0)
     {
      outcome = underflow;
     }
     else
     {
      --count;
     }
     return outcome;
    }

    Error_code mystack::top(Stack_entry &item) const
    {
     Error_code outcome = success;
     if(count == 0)
     {
      outcome = underflow;
     }
     else
     {
      item = entry[count-1];
     }
     return outcome;
    }

    bool mystack::empty() const
    {
     bool outcome;
     if (count == 0)
     {
      outcome = true;
     }
     else
     {
      outcome = false;
     }
     return outcome;
    }

    mystack::mystack()
    {
     count = 0;
    }

    /* 測試例子 */
    typedef char Stack_entry;
    #include "mystack.h"

    #include <string.h>
    #include <stdio.h>

    void main()
    {
     char my[] = "who am i";
     int length = strlen(my);
     int i;
     char c;
     mystack haha;
     for(i = 0; i < length; i++)
     {
      if (haha.push(my[i]) == overflow)
      {
       printf("error\n");
      }
     }
     
     while(!haha.empty())
     {
      haha.top(c);
      printf("%c",c);
      haha.pop();
     }
     printf("\n");
    }

    原文轉自:http://www.kjueaiud.com

    評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>