介绍
链栈,即为使用链式存储的栈。
本程序使用void指针指向不同类型的链栈头结点,使用的时候强制转换即可。
用template实现了存储不同数据类型。
实现代码
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
const int maxn = 9;
struct stack_node
{
void * root;
int data_type; // 1 int 2 double 3 char
}st[maxn + 1];
template<class T>struct node
{
T data;
node<T>*next;
};
template<class T>void des_stack(int root)
{
node<T>*pointer = (node<T>*)st[root].root;
node<T>*pointer2;
while (pointer2 = pointer->next)
{
free(pointer);
pointer = pointer2;
}
free(pointer);
st[root].root = NULL;
st[root].data_type = 0;
}
template<class T>bool stack_top(int root, T &ans)
{
node<T>*pointer = (node<T>*)st[root].root;
if (pointer->next == NULL) return 0;
ans = pointer->data;
return 1;
}
template<class T>void stack_push(int root, T data)
{
node<T>*temp_pointer = (node<T>*)malloc(sizeof(node<T>));
temp_pointer->data = data;
temp_pointer->next = (node<T>*)