Sequence of stack, the basic operation of a link stack

First, the sequence of stack

The basic concept of the order of the stack, initialization, push, pop, empty, destroyed

Stack storage order: the sequence is identical with the storage structure is generally linear form, by using a group address storage unit sequentially contiguous data elements from memory stack bottom side to the top of the stack, at the bottom of the stack is generally lower address side.

  • Attached to top pointer indicates the position of the top element in the stack in order. But for ease of operation, typically the top indicates the real target address above the top element.
  • Separate base pointer indicating the position of the bottom of the stack in the order of stack elements.
  • Further, with the maximum capacity indicates stacksize stack may be used.

Stack stack empty full judgment

Empty stack: base = = top stack is empty mark
stack full: top-base = = stacksize

Processing method when the stack is full

1, error, return to the operating system
2, the allocation of more space as a storage space on the stack, the stack of original content into the new stack (drawback is too time-consuming)

Using the array as the characteristics of the storage sequence of stack:
Simple, convenient but easy to produce an overflow (fixed array size)

  • Overflow (overflow): the stack is already full, but also pushed element
  • Underflow (underflow): stack was empty, but also pop-up element

Note: Overflow is a mistake, the problem can not be processed; and underflow are generally considered to be a termination condition, namely the end of the Problem

First show at predefined constants and type:

#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define true 1
#define false 0
typedef int Status;

Stack the order of representation

#define MAXSIZE 100	//顺序栈存储空间初始分配量
typedef struct{
 SElemType *data;	//栈顶指针
 SElemType *top;	//栈尾指针
 int stacksize;		//栈可用最大容量
 }SqStack;

Stack initialization sequence

Status InitStack(SqStack &s){//构造一个空栈
S.base = new SElemType{MAxSIZE];
//或用c语言语法:S。base=(SElemType*)malloc(MAXSIZE*sizeof(SElemType));
if(!S.base)//S.base是栈中的第一个元素
	exit(OVERFLOW);//空间分配失败
S.top=S.base;//栈顶指针等于栈底指针
S.stacksize=MAXSIZE;//stacksize置为最大容量MAXSIZE	

Stack stack order

Status Push(SqStack &S,int e){//插入e,为新的栈顶元素
if(S.top-S.base==S.stacksize)//栈满
*S.top=e;//取栈顶指针所指空间存e
S.top++;//栈顶指针加一
//上面两句可以合并成一句*S.top++=e;因为算法优先级问题,现存e再指针加一
return OK;

Stack the order of the stack

Status Pop(SqStack &S,int e){//删除S的栈顶元素,用e返回其值
if(S.top==S.base)	//栈空
	return ERROR;	//若空则出错(下溢)
--S.top;		//栈顶指针减一
e==*S.top;		//将栈顶元素赋给e,带*表示指针所指的那块空间
//同样的,上面两句可以合并为e==*--S.top;
return OK;		

Stack order to take the top element

int GetTop(SqStack S){//返回S的栈顶元素,不修改栈顶指针
if(S.top!=S.base)	//栈不为空
	return *(S.top-1);	//返回栈顶元素的值,栈顶指针不变,即还在栈顶的上方

[Supplement] algorithm

Stack sequence is determined whether the stack is empty:

Status StackEmpty(SqStack S)//若栈为空,返回TURE,否则返回FLALSE
if(S.top==S.base)
	return TRUE;
else
	return FALSE;
}	

Seek length sequence of stack:

int StackLength(SqStack S)
{
	return S.top-S.base;
}	

Empty the order of the stack (space is still just remove elements):

Status ClearStack(Sqstack S){
if(S.base)	//如果base存在,即栈存在 
	S.top=S.base;//把base的值赋值给top
return OK;	

The order of destruction stack (not space):

Status DestroyStack(SqStack &S){
if(S.base){
	delete S.base;//
	S.stacksize=0; //栈内空间的个数为0
	S.base=S.top=NULL;//指针为空
}
return OK;	
}

Second, the link stack

Implemented with a stack of linked storage structure.
The same configuration as node configuration and a single list of link stack, here indicated by SNODE;

Chain stack, he said:

  • Chain stack isLimited operationSingle list, the list can only operate in the head
typedef struct SNode{
Selem data;
struct SNode *next;//嵌套定义
}SNode,*LInkStack;//栈的结点类型。*Linkstack是指针类型
LinkStack S;

Link stack pointer in a direction, generally the top element points to the predecessor element, and so on, is to facilitate the operation

  • Head pointer list is the top of the stack
  • The first node does not need
  • The basic stack full of absence, as long as there is memory space
  • Corresponding to the stack pointer points to the first empty space, S = NULL;
  • Insertion and deletion is performed only at the top of the stack

Link stack initialization

void InitStack(LinkStack &S){//构造一个空栈S,栈顶指针置空
S=NULL;
return OK;
}

[Supplement] algorithm
to determine whether a link stack is empty

Status StackEmpty(LinkStack S){
if(S==NULL)//头指针为空就是空栈
	return TRUE;
else
	return FALSE;

Chain stack stack

The new element is inserted into the top of the stack

Status Push(LinkStack &S,int e){
SNode *p=new SNode	//生成新结点p
p->data=e;		//将新结点数据域值为e
p->next=S;		//将新结点插入栈顶,p的指针域指向下面结点地址也就是原来的头指针S,
S=p;			//修改栈顶指针,也就是p变为S,S指向了新结点
return OK;

The link stack pop

Status Pop(LinkStack &S,int &e){//删除栈顶S的栈顶元素,用e返回其值
if(S==NULL)	//栈空,无元素可以删除
	return ERROR;
e=S->data;	//将栈顶元素赋给e,以便取出
SNode *p=S;	//p指向S即用p临时保存栈顶元素空间,以备释放
S=S->next;	//修改栈顶指针
delete p;	//释放原栈顶元素空间
return OK;
}	

Take the top element

int GetTop(LinkStack S){
if(S!=NULL)
	return S->data;
}	

Third, the order of the stack and a link stack Comparative

The same : the same time complexity, both O (1)
different from:

  • Sequence of stack required a fixed length determined in advance, there may be a problem of waste of memory space, the advantage is easy access positioning
  • Link stack pointer requires each element has a domain, which also increases the number of memory overhead, but the length is not limited to the stack

So how do we choose?
If the stack during use of the element changes unpredictable, sometimes very small, sometimes very large, it is best to use a linked list, conversely, if the variation in the controllable range, recommended order stack.

Published 34 original articles · won praise 85 · views 4618

Guess you like

Origin blog.csdn.net/weixin_45895026/article/details/103955869
Recommended