常用數據結構之順序存儲的棧

       棧是一種LIFO的數據結構,支持從棧頂進行數據的壓入(入棧)和彈出(出棧),可應用於將遞歸算法變成非遞歸,逆波蘭後綴表達式等等。

       畫了張簡單的結構圖,如下所示:


         用C++模板類實現的代碼如下所示:

/*
 * =====================================================================================
 *
 *       Filename:  3stack.h
 *
 *    Description:  template stack baseed of arry
 *
 *        Version:  1.0
 *        Created:  2012年03月12日 21時00分45秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Lavey Luo (lavey), [email protected]
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef __STACK_H__
#define __STACK_H__
namespace st
{
#ifndef _STATUS_CONST_
#define _STATUS_CONST_
	enum Status
	{
		OK = 0, 
		ERROR = -1
	};

	const int MAX_LEN = 20;
#endif

	template<class VALUE_TYPE>
		class stack 
		{
			public:
				explicit stack():top(-1){};
				~stack(){};
			public:
				/* 初始化順序線性表 */
				Status Init();

				/* 若棧S爲空棧,則返回OK,否則返回ERROR*/
				Status Empty();

				/* 把S置爲空棧 */
				Status Clear();

				/* 返回S的元素個數,即棧的長度 */
				int Length(); 

				/* 若棧不空,則用e返回S的棧頂元素,並返回OK;否則返回ERROR */
				Status GetTop(VALUE_TYPE *e);

				/* 插入元素e爲新的棧頂元素 */
				Status Push(VALUE_TYPE e);

				/* 若棧不空,則刪除棧頂元素,用e返回其值,並返回OK;否則返回ERROR */
				Status Pop(VALUE_TYPE *e);
			private:
				int top;
				VALUE_TYPE data[MAX_LEN];
		};
	template<class VALUE_TYPE>
		Status stack<VALUE_TYPE>::Init()
		{
			top = -1;	
			return OK;
		}

	template<class VALUE_TYPE>
		Status stack<VALUE_TYPE>::Empty()
		{
			if (top == -1)
				return OK;
			return ERROR;
		}

	template<class VALUE_TYPE>
		Status stack<VALUE_TYPE>::Clear()
		{
		      top=-1;
		      return OK;
		}

	template<class VALUE_TYPE>
		int stack<VALUE_TYPE>::Length()
		{
			return  top +1;
		} 

	template<class VALUE_TYPE>
		Status stack<VALUE_TYPE>::GetTop(VALUE_TYPE* e)
		{
			if (top == -1) return ERROR;
			*e = data[top];
			return OK;
		}

	template<class VALUE_TYPE>
		Status stack<VALUE_TYPE>::Pop(VALUE_TYPE* e)
		{
			if (top == -1) return ERROR;
			*e = data[top];
			top--;
			return OK;
		}

	template<class VALUE_TYPE>
		Status stack<VALUE_TYPE>::Push(VALUE_TYPE e)
		{
			if (top >= MAX_LEN -1)	
				return ERROR;
			top++;
			data[top] = e;
			return OK;
		}
}
#endif // __STACK_H__

測試用例的代碼如下:

/*
 * =====================================================================================
 *
 *       Filename:  test_stack.cpp
 *
 *    Description:  test case of 4stack.h
 *
 *        Version:  1.0
 *        Created:  2012年03月12日 21時33分36秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Lavey Luo (lavey), [email protected]
 *   Organization:  
 *
 * =====================================================================================
 */
#include "3stack.h"
#include <stdio.h>

int test_stack(int argc,  char** argv)
{
	st::stack<int> stack;
	if (stack.Init() == st::OK)
	{
		puts("壓棧10個元素,1..10");
		for(int j=1;j<=10;j++)
			stack.Push(j);
		int e = 0;
		do
		{
			stack.Pop(&e);
			printf("彈出當前的棧頂元素 e=%d\n", e);
		} while(stack.Empty() != st::OK);
		printf("棧空否:%d(0:空 -1:否)\n", stack.Empty());
		stack.GetTop(&e);
		printf("棧頂元素 e=%d 棧的長度爲%d\n",e,stack.Length());
		stack.Clear();
		printf("棧空否:%d(0:空 -1:否)\n", stack.Empty());
	}
	return 0;
}



發佈了34 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章