//链式栈的基本运算同顺序栈
/*因为堆栈只能在头部操作,所以链式存储堆栈时不能附加表头节点*/
/*因为堆栈只能在头部操作,所以链式存储堆栈时不能附加表头节点*/
class
linkstack
{
private link top; // 栈顶指针
// 堆栈设置
public linkstack( int sz)
{
setup();
}
// 堆栈初始化
private void setup()
{
top = null ;
}
// 在堆栈中压入一个元素
public void push(object it)
{
top = new link(it,top); // 申请一个新节点,
// 数据域的值是it,同时将新节点的指针域指向原来的top节点,新节点的指针为top
}
// 在堆栈中弹出一个元素
public void pop()
{
assert .notfalse( ! isempty(), " empty stack " ); // 堆栈是否为空
object it = top.element();
top = top.next();
return it;
}
// 取栈顶元素
public object topvalue()
{
assert .notfalse( ! isempty, " not top value " );
return top.element();
}
// 测试堆栈是否为空
public boolean isempty()
{
return top = null ;
}
}
{
private link top; // 栈顶指针
// 堆栈设置
public linkstack( int sz)
{
setup();
}
// 堆栈初始化
private void setup()
{
top = null ;
}
// 在堆栈中压入一个元素
public void push(object it)
{
top = new link(it,top); // 申请一个新节点,
// 数据域的值是it,同时将新节点的指针域指向原来的top节点,新节点的指针为top
}
// 在堆栈中弹出一个元素
public void pop()
{
assert .notfalse( ! isempty(), " empty stack " ); // 堆栈是否为空
object it = top.element();
top = top.next();
return it;
}
// 取栈顶元素
public object topvalue()
{
assert .notfalse( ! isempty, " not top value " );
return top.element();
}
// 测试堆栈是否为空
public boolean isempty()
{
return top = null ;
}
}