链式存储的栈用链表的第一个节点作为栈顶,压栈和出栈都是通过对链表的头进行添加删除来实现的。
下面是链式存储结构栈的示意图:
用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;
}