常用數據結構之鏈式存儲的棧

     鏈式存儲的棧用鏈表的第一個節點作爲棧頂,壓棧和出棧都是通過對鏈表的頭進行添加刪除來實現的。 

      下面是鏈式存儲結構棧的示意圖:


         

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

/*
 * =====================================================================================
 *
 *       Filename:  3linkstack.h
 *
 *    Description:  template implement of stack based on link
 *
 *        Version:  1.0
 *        Created:  2012年03月12日 21時55分09秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Lavey Luo (lavey), [email protected]
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__

namespace st
{
#ifndef _STATUS_CONST_
#define _STATUS_CONST_
	enum Status
	{
		OK = 0, 
		ERROR = -1
	};
#endif

	template<class T> class linkstack; 
	template<class T>
		class stacknode
		{
			friend class linkstack<T>;
			T data;
			stacknode* next;
		};

	template<class T>
		class linkstack 
		{
			public:
				explicit linkstack():top(0), len(-1){ };
				~linkstack(){
					if (top)
					{
						Clear();
						delete top;
					}
				};
			public:
				/* 初始化順序線性表 */
				Status Init();


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

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

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

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

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

				/* 若棧不空,則刪除棧頂元素,用e返回其值,並返回OK;否則返回ERROR */
				Status Pop(T *e);
			private:
				stacknode<T>* top;
				int len;
		};

	template <class T>
		Status linkstack<T>::Init()
		{
			top = new stacknode<T>;
			if (!top)
				return ERROR;
			top->next = 0;
			len++;

			return OK;
		}

	template <class T>
		Status linkstack<T>::Empty()
		{
			if (len == 0 && top)
				return OK;
			return ERROR;
		}

	template <class T>
		Status linkstack<T>::Clear()
		{
		  if (len <= 0)	
				return ERROR;

			do {
				stacknode<T>* t =  top;               /* -----  取棧頂元素並銷燬  ----- */
				top =  top->next;
				delete t;
				len --;
			} while ( len != 0 );				/* -----  end do-while  ----- */
			
			return OK;
		}

	template <class T>
		int linkstack<T>::Length()
		{
			if (len <= 0 ||!top)
				return 0;
			return len;
		}

	template <class T>
		Status linkstack<T>::GetTop(T *e)
		{
			if (len <= 1 || !top)
				return ERROR;
			
			*e = top->data;
			return OK;
		}

	template <class T>
		Status linkstack<T>::Push(T e)
		{
			 stacknode<T>* t = new stacknode<T>;	
			 t->data = e;
			 t->next = top;
			 top = t;
			 len++;
			 return OK;
		}

	template <class T>
		Status linkstack<T>::Pop(T* e)
		{
			if (len < 1 || !top)
				return ERROR;

			*e = top->data;
			stacknode<T>* thenode = top;             /* -----  取棧頂元素並銷燬  ----- */
			top = top->next;
			len --;
			delete thenode;
			return OK;
		}
}
#endif // __LINKSTACK_H__

       測試用例如下:

/*
 * =====================================================================================
 *
 *       Filename:  test_linklist.cpp
 *
 *    Description:  test case of the 3linklinst.h
 *
 *        Version:  1.0
 *        Created:  2012年03月11日 21時58分59秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Lavey Luo (lavey), [email protected]
 *   Organization:  
 *
 * =====================================================================================
 */
#include "3linklist.h"
#include <stdio.h>

int test_linklinst (int argc,  char** argv)
{
	st::linklist<int> list1;
	list1.Init();
	if (list1.Empty() == st::OK)
		puts("the list1 is empty");
	printf(" list1 len %d\n",  list1.Length());

	puts("insert 1..5 to the list1");
	for (int i = 1; i <= 5; i++)
		list1.Insert(i,  i);
	printf(" list1 len %d\n",  list1.Length());
	
	puts("to push 200 to the end of list1");
	list1.Pushback(200);
	printf(" list1 len %d\n",  list1.Length());

	printf("locate of 200 in list1 is:%d \n", list1.Locate(200));
	printf("locate of 3 in list1 is:%d \n", list1.Locate(3));

	puts("clear the list1");
	list1.Clear();
	printf(" list1 len %d\n",  list1.Length());

	st::linklist<float> flist1;
	flist1.CreateListHead(10);
	printf(" flist1 len %d\n",  flist1.Length());

	st::linklist<float> flist2;
	flist2.CreateListTail(10);
	printf(" flist1 len %d\n",  flist2.Length());
	return 0;
}


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