数据结构练习——入栈出栈的子函数
作者:互联网
入栈与出栈 的相关算法:
void push(Stack *st, char ch)
{
if (st->top == 20)
{
printf("栈已满,不能入栈\n");
return;
}
else
{
st->data[st->top]=ch;
st->top++;
}
}
void pop(Stack *st, char *ch)
{
if (st->top == 0)
{
printf("栈空,不能出栈\n");
return;
}
else
{
st->top--;
*ch=st->data[st->top];
}
}
主函数:
#include <stdio.h>
typedef struct _stack
{
char data[20];
int top;
} Stack;
void push(Stack *st, char ch);
void pop(Stack *st, char *ch);
int main(void)
{
int i;
char ch;
char str[20] = "!ssSuecc";
Stack st;
st.top = 0; //初始化栈
for (i = 0; i < 8; i++)
{
push(&st, str[i]);
if (i == 3 || i == 4 || i == 6)
{
pop(&st, &ch);
putchar(ch);
}
}
for (i = 0; i < 5; i++)
{
pop(&st, &ch);
putchar(ch);
}
putchar('\n');
return 0;
}
标签:ch,出栈,入栈,top,st,char,子函数,void,Stack 来源: https://blog.csdn.net/yooppa/article/details/120692643