在程序设计中,大家一定接触过“堆栈”,其实堆和栈是两个完全不同的概念,栈是一种特殊的数据结构。在中断处理中常用来保护现场。
在栈的结构中,只能在栈的一端进行数据操作,这一端成为栈顶,另一端成为栈底。即:数据的存取只能在栈顶进行。从数据运算的角度来分析,栈结构式按照“后进先出”(Last In Firt OUT,LIFO)的原则处理结点的。
1.数据准备部分
<h3><pre name="code" class="java"><span style="font-weight: normal;"><span style="font-size:14px;">//数据</span></span>
<span style="font-weight: normal;"><span style="font-size:14px;">class DATA
{
String name;
int age;
}
//结构
class StackType
{
static final int MAXLEN=50;
DATA3[] data=new DATA3[MAXLEN+1]; //数据元素
int top; //栈顶 </span></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-weight: normal;"><span style="font-size:10px;"> }</span></span>
2.初始化栈
<span style="font-weight: normal;"> StackType STInit()
{
StackType p;
if((p=new StackType())!=null) //申请栈内存
{
p.top=0; //设置栈顶为0
return p; //返回指向栈的指针
}
return null;
}</span>
3.判断栈空/栈满
<span style="font-weight: normal;"> StackType STInit()
{
StackType p;
if((p=new StackType())!=null) //申请栈内存
{
p.top=0; //设置栈顶为0
return p; //返回指向栈的指针
}
return null;
}</span>
<span style="font-weight: normal;"> boolean STIsEmpty(StackType s) //判断栈是否为空
{
boolean t;
t=(s.top==0);
return t;
}</span>
<span style="font-weight: normal;"> boolean STIsFull(StackType s) //判断栈是否已满
{
boolean t;
t=(s.top==MAXLEN);
return t;
}</span>
4.清空栈/释放空间
<span style="font-weight: normal;"> boolean STIsEmpty(StackType s) //判断栈是否为空
{
boolean t;
t=(s.top==0);
return t;
}</span>
<span style="font-weight: normal;"> boolean STIsFull(StackType s) //判断栈是否已满
{
boolean t;
t=(s.top==MAXLEN);
return t;
}</span>
<span style="font-weight: normal;"> void STClear(StackType s) //清空栈
{
s.top=0;
}</span>
<span style="font-weight: normal;"> void STFree(StackType s) //释放栈所占用空间
{
if(s!=null)
{
s=null;
}
}</span>
5.入栈/出栈
<span style="font-weight: normal;"> void STClear(StackType s) //清空栈
{
s.top=0;
}</span>
<span style="font-weight: normal;"> void STFree(StackType s) //释放栈所占用空间
{
if(s!=null)
{
s=null;
}
}</span>
<span style="font-weight: normal;"> int PushST(StackType s,DATA3 data) //入栈操作
{
if((s.top+1)>MAXLEN)
{
System.out.print("栈溢出!\n");
return 0;
}
s.data[++s.top]=data; //将元素入栈
return 1;
}
</span>
<span style="font-weight: normal;">
DATA PopST(StackType s) //出栈操作
{
if(s.top==0)
{
System.out.print("栈为空!\n");
//
System.exit(0);
}
return (s.data[s.top--]);
}
</span>
6.读取结点数据
<span style="font-weight: normal;"> int PushST(StackType s,DATA3 data) //入栈操作
{
if((s.top+1)>MAXLEN)
{
System.out.print("栈溢出!\n");
return 0;
}
s.data[++s.top]=data; //将元素入栈
return 1;
}
</span>
<span style="font-weight: normal;">
DATA PopST(StackType s) //出栈操作
{
if(s.top==0)
{
System.out.print("栈为空!\n");
//
System.exit(0);
}
return (s.data[s.top--]);
}
</span>
<span style="font-weight: normal;"> DATA PeekST(StackType s) //读栈顶数据
{
if(s.top==0)
{
System.out.printf("栈为空!\n");
//
System.exit(0);
}
return (s.data[s.top]);
}
}</span>
以上就是数据结构中的栈。
<span style="font-weight: normal;"> DATA PeekST(StackType s) //读栈顶数据
{
if(s.top==0)
{
System.out.printf("栈为空!\n");
//
System.exit(0);
}
return (s.data[s.top]);
}
}</span>