您所说的栈,是由操作系统负责管理的一段栈空间,在递归、子程序调用等处应用广泛。这是操作系统的范畴。
——并不是在C语言范畴讨论的概念。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef int DataType
typedef struct node{
DataType data
struct node * next
}Stack
Stack* CreateStack() //创建栈
void StackEmpty(Stack* ) //清空栈
void DestoryStack(Stack*)//撤销(删除)栈
int IsEmpty(Stack*) //判空
int PushStack(Stack*, DataType) //入栈
int PopStack(Stack*) //出栈
DataType GetTopElement(Stack*)//取栈顶元素
Stack* CreateStack()
{
Stack *stack = (Stack*)malloc(sizeof(Stack))
if(NULL != stack)
{
stack->next = NULL
return stack
}
printf("out of place.\n")
return NULL
}
//清空栈
void StackEmpty(Stack* stack)
{
while(!IsEmpty(stack))
{
PopStack(stack)
}
printf("now stack is empty. \n")
}
//撤销栈
void DestoryStack(Stack* stack)
{
free(stack)
exit(0)
}
int IsEmpty(Stack* stack)
{
return (stack->next == 0)
}
//入栈,成功返回1,失败返回0, 把元素 data 存入 栈 stack 中
int PushStack(Stack* stack, DataType data)
{
Stack* newst = (Stack*)malloc(sizeof(Stack))
if(NULL != newst)
{
newst->data = data
newst->next = stack->next //s->next = NULL
stack->next = newst
return 1
}
printf("out of place PushStack.\n")
return 0
}
/*
出栈,成功返回1,失败返回0,出栈不取出元素值,只是删除栈顶元素。
如出栈要实现,取出元素值,并释放空间,可结合取栈顶元素函数做修改,这里不再给出。
*/
int PopStack(Stack* stack)
{
Stack* tmpst
if(!IsEmpty(stack))
{
tmpst = stack->next
stack->next = tmpst->next
free(tmpst)
return 1
}
return 0
}
//取栈顶元素,仅取出栈顶元素的值,取出之后,该元素,任然存在栈中。成功返回元素值,失败输出提示信息,并返回 -1
DataType GetTopElement(Stack* stack)
{
if(!IsEmpty(stack))
{
return stack->next->data
}
printf("stack is empty GetTopElement.\n")
return -1
}
int main()
{
Stack* stack = CreateStack()
char str[200]
printf("请输入字符串:")
scanf("%s", str)
int num = 0
for (int i = 0i <strlen(str)i++) {
if (!IsEmpty(stack) &&GetTopElement(stack) == '(' &&str[i] == ')') {
PopStack(stack)
num++
} else {
PushStack(stack, str[i])
}
}
if (!IsEmpty(stack)) {
num = -1
}
printf("匹配结果:%d", num)
DestoryStack(stack)
return 1
}
//顺序栈#include
#include
#include
#define
STACK_INIT_SIZE
100
#define
STACKINCREMENT
10
typedef
struct
{
int
*base
int
*top
int
stacksize
}SqStack
typedef
int
ElemType
int
InitStack(SqStack
&S)
//为栈S分配存储空间,并置S为空栈
{
int
size
=
STACK_INIT_SIZE
S.base=(int
*)malloc(size*sizeof(ElemType))
if(!S.base)
return
0
S.top=S.base
//置栈S为空栈
S.stacksize=STACK_INIT_SIZE
return
1
}
int
GetTop(SqStack
S,int
&e)
//若栈不空,则用e返回S的栈顶元素
{
if(S.top==S.base)
return
0
e=*(S.top-1)
return
1
}
int
Push(SqStack
&S,
int
e)
/*进栈函数,将e插入栈S中,并使之成为栈顶元素*/
{
if(S.top-S.base>=S.stacksize)
/*栈满,追加存储空间*/
{
int
stackinvrement
=
STACKINCREMENT
S.base=(ElemType
*)
realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType))
if(!S.base)
return
0
/*存储分配失败*/
S.stacksize+=STACKINCREMENT
}
*S.top++=e
return
1
}
int
Pop(SqStack
&S,int
&e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/
{
if(S.top==S.base)
return
0
e=*--S.top
return
1
}
void
OutputStack(SqStack
&S)
{int
*q
q=S.top-1
for(int
i=0i
#include
typedef
struct
SNode
{
int
data
struct
SNode
*next
}SNode,*LinkStack
LinkStack
top
LinkStack
PushStack(LinkStack
top,int
x)
//入栈
{
LinkStack
s
s=(LinkStack)malloc(sizeof(SNode))
s->data=x
s->next=top
top=s
return
top
}
LinkStack
PopStack(LinkStack
top)
//退栈
{
LinkStack
p
if(top!=NULL)
{
p=top
top=top->next
free(p)
printf("退栈已完成\n")
return
top
}
else
printf("栈是空的,无法退栈!\n")
return
0
}
int
GetStackTop(LinkStack
top)
//取栈顶元素
{
return
top->data
}
bool
IsEmpty()//bool取值false和true,是0和1的区别,bool只有一个字节,BOOL为int型,bool为布尔型
{
return
top==NULL
?
true:false
}
void
Print()
{
SNode
*p
p=top
if(IsEmpty())
{
printf("The
stack
is
empty!\n")
return
}
while(p)
{
printf("%d
",
p->data)
p=p->next
}
printf("\n")
}
void
main()
{
int
x,a,b
char
m
do
{
printf("\n")
printf("###############链栈的基本操作##################\n")
printf("××××××××1.置空栈××××××××××\n")
printf("××××××××2.进栈×××××××××××\n")
printf("××××××××3.退栈×××××××××××\n")
printf("××××××××4.取栈顶元素××××××××\n")
printf("××××××××5.退出程序×××××××××\n")
printf("##############################################\n")
printf("\n请选择一个字符:")
scanf("%c",&m)
switch(m){
case
'1':
top=NULL
printf("\n栈已置空!")
break
case
'2':
printf("\n请输入要进栈的元素个数是:")
scanf("%d",&a)
printf("\n请输入要进栈的%d个元素:",a)
for(b=0b
评论
0
0
加载更多