靜態數組實現的堆棧

來源:互聯網
上載者:User
/*_############################################################################
  _##
  _##  靜態數組實現的堆棧
  _##  Author: xwlee                        
  _##  Time: 2006.12.30 
  _##  Chang'an University
  _##  Development condition: win2003 Server+VC6.0
  _##
  _##  static_array.cpp 檔案
  _##########################################################################*/
#include "stack.h"
#include <assert.h>

// --------------------------靜態數組-------------------------------
// 堆棧中數的最大限制.
#define STACK_SIZE 100

// 儲存堆棧中值的數組.
static STACK_TYPE stack[ STACK_SIZE ];

// 指向堆棧頂部元素的指標.
static int top_element = -1;
// --------------------------靜態數組-------------------------------

// push函數
void push( STACK_TYPE value )
{
    assert( !is_full() ); // 若堆棧已滿,斷言成立.
    top_element += 1;
    stack[ top_element ] = value;
}

// pop函數
void pop( void )
{
    assert( !is_empty() ); // 若堆棧已空,斷言成立.
    top_element -= 1;
}

// top函數
STACK_TYPE top( void )
{
    assert( !is_empty() ); // 若堆棧已空,斷言成立.
    return stack[ top_element ];
}

// is_empty函數
int is_empty( void )
{
    return top_element == -1;
}

// is_full函數
int is_full( void )
{
    return top_element == STACK_SIZE - 1;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.