栈与队列Part1--栈的定义、抽象数据类型(ADT)、顺序存储结构定义、两栈共享空间定义及进栈出栈操作具体实现方法(C语言描述)(转载)

一、栈(Stack)的定义:
栈(Stack): 是限定仅在表尾进行插入和删除操作的线性表。是一种特殊的线性表。
栈又称为 后进先出(Last In First Out) 的线性表,简称LIFO结构。
(Examples:Photoshop and Word’s “Undo” function,browsers’s “back” function。)

允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom),不含任何数据元素的栈称为空栈。

栈的插入操作叫做进栈(Push),也称压栈、入栈。
栈的删除操作叫做出栈(Pop),也称弹栈。

二、栈的抽象数据类型(Abstract Data Type):
对于栈而言,线性表的操作特性它基本都具备,但操作上会有些区别。特别是插入与删除操作。

Data: 性质同线性表。数据元素具有相同的类型,相邻元素具有前驱和后驱的关系。

[ADT of stacks:]

Operations:
InitStack(*S): Set up an empty stack. //初始化操作,建立一个空栈S。
DestroyStack(*S): Destroy the stack when it exists. //若栈存在,则销毁它。
ClearStack(*S): Empty the stack. //将栈清空。
StackEmpty(S): (Bool Type)Judge whether the stack is empty. //若栈为空,则返回true,否则返回false。
GetTop(S, *e): If the stack exists and not empty, give the top element value to “e” and return “e”. //若栈存在且非空,用e返回S的栈顶元素。
Push(*S, e): If the stack exists, push new element “e” into stack to be the top. //若栈存在,插入新元素e到栈S中并成为栈顶元素。
Pop(*S, *e): Delete the top element in stack, give its value to “e”, and return “e”. //删除栈S中的栈顶元素,并用e返回其值。
StackLength(S): Return the amount of elements in the stack. //返回栈中元素个数。

end ADT

三、栈的顺序存储结构(Sequential Stack)定义(顺序栈):
首先,为了示意清楚以及方便,先声明定义:

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int SElemType; //SElemType的类型根据实际需求而定,此处假设为int。
typedef int Status;/“Status” is defined as a type of a function, with its returned value representing the result of the function.
(Status是函数的类型,它的返回值势函数结果状态代码,0或1.)
/

由于我们只能从表尾进行插入与删除的操作,我们选择下标为0的一端作为栈底,因为它的变化最小,更适合作为栈底。

此时,我们需要定义一个新的变量 “top” 来指示栈顶元素在栈中的位置(top 应该时刻保持小于栈的长度StackSize)。同时,我们规定当栈为空栈时,top = -1;因此当栈中只有一个元素时,top = 0.

接下来是顺序栈SqStack的结构代码:

/*Since we can only insert and take out element from one side,
it’s better to choose the start of the array as the bottom of the stack because it changes less. */

/*Here we definite a variable “top” to instruct the top element’s position in the stack. “top” should be smaller than “StackSize”.
We usually consider “top” as “-1” when the stack is empty, thus, “top” is “0” when there’s only one element. */

[The structure code of a SqStack:] //顺序栈的结构代码
typedef struct
{
SElemType data[MAXSIZE]; //SElemType为typedef的int类型数据。
int top; //As the top pointer.
}SqStack;

四、SqStack部分操作具体实现方法(C语言描述):
“Push”(进栈): T(n)=O(1)
[Achieve the operation of “push” in a SqStack:] //顺序栈的进栈操作
/*Insert a new element “e” as the new top element. */
Status Push ( SqStack *S, SElemType e )
{
if( S->top==MAXSIZE-1 ) //The stack is full.
{
return ERROR;
}
S->data[++S->top] = e;

return OK;

}
T(n) = O(1)

“Pop”(出栈): T(n)=O(1)
[Achieve the operation of “pop” in a SqStack:] //顺序栈的出栈操作
/*If the stack isn’t empty, delete the top element and return its value using “e”. */
Status Pop ( SqStack *S, SElemType *e )
{
if( S->top==-1 )
{
return ERROR;
}
*e = S->data[S->top–];

return OK;

}
T(n) = O(1)

五、两栈共享空间定义(SqDoubleStack):
虽然顺序栈不存在SqList那样插入删除元素需要大量元素的问题,但它有一个很大的缺陷:必须实现确定数组存储空间大小。 由此引出:两栈共享空间–用一个数组来存储两个栈。

方法:数组有两个端点,两个栈有两个栈底,让一个栈的栈底为数组的始端,即index=0处。而另一个栈的栈底为数组的末端,即index=n-1处。这样的话,两栈若增加元素,就是两端点向中间延伸。

关键思路:两栈顶指针top1 top2是在数组的两端,向中间靠拢。
(“top1 + 1 == top2” 代表栈满。)

[The structure code of two stacks sharing space.] //两栈共享空间的结构代码
typedef struct
{
SElemType data[MAXSIZE];
int top1; //Stack1’s top pointer.
int top2; //Stack2’s top pointer.
}SqDoubleStack;
// “top1+1==top2” represents that the stack is full.

六、SqDoubleStack部分操作具体实现方法(C语言描述):
对于SqDoubleStack的进出栈方法,我们需要的参数除了插入元素值 e 之外,还需要一个判断是对栈1还是栈2进行操作的栈号参数 stackNumber。

“Push”(进栈): T(n)=O(1)
/As for the operation of “Push” and “Pop” of SqDoubleStack,
we need another parameter “stackNumber” to judge the stack we are to operate is 1 or 2.
/
[Achieve the operation of “push” in a SqDoubleStack.]
Status Push ( SqDoubleStack *S, SElemType e, int stackNumber )
{
if( S->top1+1 S->top2 ) //Representing the stack is full.
{
return ERROR;
}
if( stackNumber
1 )
{
S->data[++S->top1] = e;
}
if( stackNumber==2 )
{
S->data[–S->top2] = e;
}

return OK;

}
T(n) = O(1)

“Pop”(出栈): T(n)=O(1)
[Achieve the operation of “pop” in a SqDoubleStack.]
Status Pop ( SqDoubleStack *S, SElemType *e, int stackNumber )
{
if( stackNumber1 )
{
if( S->top1
-1 )
{
return ERROR;
}
*e = S->data[S->top–];
}
if( stackNumber2 )
{
if( S->top2
MAXSIZE )
{
return ERROR:
}
*e = S->data[S->top2++];
}

return OK;

}
T(n) = O(1)

对于SqDoubleStack这样的数据结构,通常都是当两栈的空间需求有相反关系时,即“此消彼长”时才使用。这样使用,该数据结构才比较有意义,否则两栈都在增长,很快就会因栈满而溢出了。

同时应该注意,这种方法是针对两个具有相同数据类型的栈的技巧。如果是不同数据类型的栈,则不能使用,会使问题更加复杂。

原文链接:https://blog.csdn.net/ZZirk/article/details/107004045

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值