本文主要是介绍C语言顺序栈的入栈_出栈_判空_取栈顶元素_遍历栈内元素,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
顺序栈的入栈出栈操作
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>#define TURE 1;
#define FALSE 0;
#define STACK_INIT_SIZE 100 //设栈中元素有100个
#define STACK_INCREMENT 10typedef struct
{// 栈底指针,若为空NULL则表示栈结构不存在char *base;// 栈顶指针,始终指向栈顶元素的下一个位置,//初始时top==base,可作为空栈标记char *top;// 栈的长度,元素为单位int StackSize;}SeqStack;/*栈的初始化*/
int InitStack(SeqStack *S)
{S->base = (char *)malloc( STACK_INIT_SIZE * sizeof(char) );//S->base为NULL,则!S->base为真if( !S->base ){printf("空间已满\n");return FALSE;}else{//空栈S->top = S->base;S->StackSize = STACK_INIT_SIZE;}return TURE;
}/*判断栈是否为空*/
int IsEmpty(SeqStack *S)
{//栈顶指针等于栈顶指针,表明为空栈if( S->top == S->base ){return TURE;}else{return FALSE;}
}/*入栈操作*/
int Push(SeqStack *S, char x)
{if( ( S->top ) - ( S->base ) == S->StackSize ){//栈已满,用realloc追加空间,应该是在原内存块后面继续追加空间的,//但如果原内存块后面没有空间了,就得重新分配一块内存空间了//realloc返回一个指针,如果请求失败,则返回NULL。S->base = (char *)realloc(S->base, (S->StackSize + STACK_INCREMENT) * sizeof(char) );if( S->base == NULL ){return FALSE;}//更新栈顶指针位置S->top = S->base + S->StackSize;S->StackSize = S->StackSize + STACK_INCREMENT;}//将元素x插入栈顶指针所指向的内存位置*S->top = x;//栈顶指针向上移动S->top++;return TURE;
}/*出栈操作*/
int Pop(SeqStack *S, char *x)
{//若栈不空,则删除栈顶元素,并用x返回其值//并返回TRUE,否则返回FALSE//栈为空if( S->top == S->base ){return FALSE;}else{S->top--;//*代表取指针所存内存地址的内容*x = *S->top;return TURE;}}/*取栈顶元素*/
int GetTop(SeqStack *S, char *x)
{//若栈不空,则将栈顶元素保存至x,并返回TURE//否则返回FALSE//空栈if( S->top == S->base ){return FALSE;}else{//*代表取指针所指内存地址里的内容*x = *( S->top - 1 );return TURE;}}/*遍历输出栈中所有元素*/
void PrintStack(SeqStack *S)
{printf("\n打印栈元素:");char *i;//空栈if( S->top == S->base ){printf("空栈!\n");}else{for(i=S->base; i<S->top; i++){printf("%c ", *i);}printf("\n");}
}int Create26LetterSeqStack(SeqStack *S)
{int i;for(i=1; i<=26; i++){Push(S, i+'a'-1);}return TURE;
}int main()
{int n, flag;char x;SeqStack s, t;// 26字母顺序栈// 顺序栈的初始化flag = InitStack(&t);if( flag ){printf("顺序栈初始化成功!\n");}else{printf("顺序栈初始化失败!\n");}//创建26字母顺序栈flag = Create26LetterSeqStack(&t);if( flag ){printf("\n26字母顺序栈创建成功!\n");}else{printf("\n26字母顺序栈创建失败!\n");}//遍历栈元素PrintStack(&t);//取栈顶元素flag = GetTop(&t, &x);if( flag ){printf("\n栈顶元素为:%c\n", x);}else{printf("\n取栈顶元素失败!\n");}//遍历栈元素PrintStack(&t);//出栈操作flag = Pop(&t, &x);if( flag ){printf("\n出栈元素为:%c\n", x);}else{printf("\n出栈失败!\n");}//遍历栈元素PrintStack(&t);printf("\n----------------------\n");//栈的初始化,成功返回TUREflag = InitStack(&s);if( flag ){printf("顺序栈初始化成功!\n");}else{printf("顺序栈初始化失败!\n");}//入栈操作printf("\n请输入入栈元素个数:");scanf("%d", &n);getchar(); //吃掉回车,具体请百度printf("\n请依次输入入栈元素:\n");for(int i = 0; i < n; i++){x = getchar();getchar();flag = Push(&s, x);if( !flag ){printf("入栈失败!\n");}}//取栈顶元素flag = GetTop(&s, &x);if( flag ){printf("\n栈顶元素为:%c\n", x);}else{printf("\n取栈顶元素失败!\n");}//遍历栈元素PrintStack(&s);//出栈操作flag = Pop(&s, &x);if( flag ){printf("\n出栈元素为:%c\n", x);}else{printf("\n出栈失败!\n");}//遍历栈元素PrintStack(&s);//判断栈是否为空printf("\n栈是否为空?:");flag = IsEmpty(&s);if( flag ){printf("空栈!\n");}else{printf("非空!\n");}}
这篇关于C语言顺序栈的入栈_出栈_判空_取栈顶元素_遍历栈内元素的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!