本文主要是介绍数据结构之栈的基本功能实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
栈是一种基本的数据结构,在很多地方都会用到。这次复习,通过以下代码实现了栈的一些基本功能
/*顺序栈的基本功能的实现*/
#include<stdio.h>#define TRUE 1
#define FALSE 0
#define Stack_Size 50
#define StackElement chartypedef struct
{StackElement elem[Stack_Size];int top;
}SeqStack;void InitStack(SeqStack *S)
{S->top = -1;printf("Init OK\n");
}void IsEmpty(SeqStack *S)
{if(S->top == -1)printf("the stack is null!!\n");elseprintf("the stack is not null\n");
}void IsFull(SeqStack *S)
{if(S->top == Stack_Size-1)printf("IS FULL!!\n");elseprintf("NOT FULL!!\n");
}void Push(SeqStack *S,char *data)
{S->top++;S->elem[S->top] = *data;//printf("PUSH OK\n");
}void Pop(SeqStack *S,char *data)
{*data = S->elem[S->top];S->top--;
}void GetData(SeqStack *S,char *data)
{*data = S->elem[S->top];
}int main()
{int choice;char data,x;SeqStack Stack;printf("==============顺序栈的基本功能的实现======\n");printf("1:初始化栈\n");printf("2:判断栈是否为空\n");printf("3:判断栈是否满\n");printf("4:让元素进栈\n");printf("5:让元素出栈\n");printf("6:取栈顶元素\n");printf("输入你的选择:\n");scanf("%d",&choice);while(choice){switch(choice){case 1:InitStack(&Stack);break;case 2:IsEmpty(&Stack);break;case 3:IsFull(&Stack);break;case 4:{printf("input your data:\n");scanf("%c",&data);Push(&Stack,&data);}break;case 5:Pop(&Stack,&x);printf("the poped data = %c\n",x);break;case 6:GetData(&Stack,&x);printf("top data = %c\n",x);break;}scanf("%d",&choice);}return 0;
}
这篇关于数据结构之栈的基本功能实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!