关闭

栈的表示与实现

297人阅读 评论(0) 收藏 举报

 

<iframe style="WIDTH: 89.81%; HEIGHT: 64px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750500&amp;lmt=1192819750&amp;prev_fmts=468x60_as&amp;format=468x15_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>

<iframe style="WIDTH: 44.33%; HEIGHT: 259px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCB94-nvUZWENUB9QugJQtwRQTVAgULcCUB4&amp;oe=gb2312&amp;dt=1192720966468&amp;lmt=1192720966&amp;format=250x250_as&amp;output=html&amp;correlator=1192720966453&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-67-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192720940&amp;ga_hid=1155529757&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>一、栈的定义

栈是限定仅在表尾进行插入或删除操作的线性表。

栈的表尾称为栈顶,表头称为栈底,不含元素的空表称为空栈。

栈的抽象数据类型定义:

ADT Stack{

数据对象={ai|ai(- ElemSet,i=1,2,...,n,n>=0}

数据关系:R1={<ai-1,ai>|ai-1,ai(- D,i=2,...,n}

基本操作:

InitStack(&S) 构造一个空栈S

DestroyStack(&S) 栈S存在则栈S被销毁

ClearStack(&S) 栈S存在则清为空栈

StackEmpty(S) 栈S存在则返回TRUE,否则FALSE

StackLength(S) 栈S存在则返回S的元素个数,即栈的长度

GetTop(S,&e) 栈S存在且非空则返回S的栈顶元素

Push(&S,e) 栈S存在则插入元素e为新的栈顶元素

Pop(&S,&e) 栈S存在且非空则删除S的栈顶元素并用e返回其值

StackTraverse(S,visit())栈S存在且非空则从栈底到栈顶依次对S的每个数据元素调用函数visit()一旦visit()失败,则操作失败

}ADT Stack

二、栈的表示和实现

栈的存储方式:

1、顺序栈:利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设指针top指示栈顶元素在顺序栈中的位置

2、链栈:利用链表实现

顺序栈的类C语言定义:

typedef struct{

SElemType *base;

SElemType *top; //设栈顶栈底两指针的目的是便于判断栈是否为空

int StackSize; //栈的当前可使用的最大容量.

}SqStack;

顺序栈的的模块说明:

struct STACK {

SElemType *base;

SElemType *top;

int stacksize;

};

typedef struct STACK Sqstack;

Status InitStack(SqStack &S);

Status DestroyStack(SqStack &S);

Status ClearStack(SqStack &S);

Status StackEmpty(SqStack S);

int StackLength(SqStack S);

Status GetTop(SqStack S,SElemType &e);

Status Push(SqStack &S,SElemType e);

Status Pop(SqStack &S,SElemType &e);

Status StackTraverse(SqStack S,Status (*visit)());

 

Status InitStack(SqStack &S) {

S.base=(SelemType *)malloc(STACK_INIT_SIZE *sizeof(ElemType));

if(!S.base)exit(OVERFLOW);

S.top=S.base;

S.stacksize=STACK_INI_SIZE;

return OK;

}//IniStack

Status DestroyStack(SqStack &S); {

}//DestroyStack

Status ClearStack(SqStack &S); {

S.top=S.base;

} //ClearStack

Status StackEmpty(SqStack S); {

if(S.top==S.base) return TRUE;

else return FALSE;

} //StackEmpty

int StackLength(SqStack S); {

int i; SElemType *p;

i=0;

p=S.top;

while(p!=S.base) {p++; i++; }

} //stackLength

Status GetTop(SqStack S,SElemType &e); {

if(S.top==S.base) return ERROR;

e=*(S.top-1);

return OK;

} //GetTop

Status Push(SqStack &S,SElemType e); {

if(S.top - s.base>=S.stacksize) {

S.base=(ElemType *) realloc(S.base,

(S.stacksize + STACKINCREMENT) * sizeof(ElemType));

if(!S.base)exit(OVERFLOW);

S.top=S.base+S.stacksize;

S.stacksize+=STACKINCREMENT;

}

*S.top++=e;

return OK;

} //Push

Status Pop(SqStack &S,SElemType &e); {

if(S.top==S.base)

return ERROR;

e=*--S.top;

return OK;

}//Pop

Status StackTraverse(SqStack S,Status (*visit)()); {

}//StackTraverse

以上伪代码的C语言源码
三、总结

栈的定义

栈的顺序存储实现

<iframe style="WIDTH: 52.39%; HEIGHT: 263px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCKcC4yuPlq5lUDRQDVAtUK4BUENQCA&amp;oe=gb2312&amp;dt=1192819388296&amp;lmt=1192819388&amp;format=300x250_as&amp;output=html&amp;correlator=1192819388281&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fviewthread.php%3Ftid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fpost.php%3Faction%3Dedit%26fid%3D16%26tid%3D70%26pid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1527165455&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=5&amp;u_java=true"></iframe>

<iframe style="WIDTH: 82.23%; HEIGHT: 74px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192818497343&amp;lmt=1192818497&amp;format=468x60_as&amp;output=html&amp;correlator=1192818497343&amp;channel=1741427766&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforumdisplay.php%3Ffid%3D16%26page%3D1&amp;ui=rc%3A6&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1000484876&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=6&amp;u_java=true"></iframe>

<iframe style="WIDTH: 58.41%; HEIGHT: 156px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750656&amp;lmt=1192819750&amp;prev_fmts=468x60_as%2C468x15_0ads_al_s%2C234x60_as&amp;format=200x90_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe> 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场