Stack using C
Stack using C
PROGRAM : For Stack using C
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 4
- struct stack
- {
- int a[MAX];
- int top;
- }st;
- int stfull(){
- if(st.top>=MAX-1)return 1;
- else return 0;
- }
- int push(int val){
- st.top++;
- st.a[st.top]=val;
- }
- int stempty(){
- if(st.top==-1)return 1;
- else return 0;
- }
- int pop(){
- st.top--;
- return 0;
- }
- int display(){
- int i;
- if(stempty()==1)printf("\nstack is empty\n");
- else{
- printf("\n");
- for(i=st.top;i>=0;i--){
- printf("|%d|",st.a[i]);
- }
- }
- return 0;
- }
- /* Main function */
- int main()
- {
- int val;
- int choice;
- st.top=-1;
- label:
- printf("\nselect the choice\n");
- printf("1=push\n2=pop\n3=peep\n4=display\n5=clear screen\n");
- scanf("%d",&choice);
- switch(choice){
- case 1:
- if(stfull()==1){
- printf("\nstack is full\n");
- }
- else{
- printf("\nenter value=");
- scanf("%d",&val);
- push(val);
- }
- goto label;
- break;
- case 2:
- if(stempty()==1){printf("\nstack is empty\n");}
- else {
- printf("\n%d deleted\n",st.a[st.top]);
- val=pop();
- }
- goto label;
- break;
- case 3: printf("\n a[st.top]=%d",st.a[st.top]);
- goto label;
- break;
- case 4:
- display();
- goto label;
- break;
- case 5:
- system("cls");
- goto label;
- break;
- default:
- exit(0);
- }
- return 0;
- }
Comments
Post a Comment