3.c

来自「数据结构栈操作」· C语言 代码 · 共 76 行

C
76
字号
#include"stdio.h"
#include"stdlib.h"
#define MAXSIZE 100
typedef int ElemType;
typedef struct{ElemType elem[MAXSIZE];
	       int top;
	       }SqStack;
void OutStack(SqStack p);
void InitStack(SqStack *p);
void Push(SqStack *p,ElemType x);
ElemType Pop(SqStack *p);
ElemType GetTop(SqStack p);
void main()
  {SqStack q;
    int i,y,cord;ElemType a;
    do{printf("\n");
       printf("\n  Menu \n");
       printf("\n---------------\n");
       printf("\n 1  InitStack  \n");
       printf("\n 2  Push \n");
       printf("\n 3  Pop  \n");
       printf("\n 4  GetTop  \n");
       printf("\n 5  End  \n");
       printf("\n---------------\n");
       printf("Please input your choose(1,2,3,4,5)");
scanf("%d",&cord);

 switch(cord)
	{case 1:{InitStack(&q);	OutStack(q);
		}break;
	case 2:{printf("Please Input The Data you want to PUSH: a=");scanf("%d",&a);
		Push(&q,a);OutStack(q);
		}break;
	case 3:{a=Pop(&q);printf("\n a=%d",a);
		OutStack(q);
		}break;
	case 4:{y=GetTop(q);printf("\n y=%d",y);
		OutStack(q);
		}break;
	case 5:exit(0);
		}
	}while(cord<=5);
  }

void InitStack (SqStack *p)
{p->top=0;
}
void Push(SqStack *p,ElemType x)
{if (p->top<MAXSIZE-1){p->top=p->top+1;p->elem[p->top]=x;}
	else printf("\n Overflow!");
}
ElemType Pop(SqStack *p)
{ElemType x;
if (p->top!=0){x=p->elem[p->top];
	p->top=p->top-1;
	return(x);
	}
}
ElemType GetTop(SqStack p)
{ElemType x;
if (p.top!=0)
{x=p.elem[p.top];
 return(x);
}
else {
	printf ("\n Underflow!");
	return(-1);
	}
}
void OutStack(SqStack p)
{int i,j;
 if (p.top==0) printf("\n The Stack is NULL.");
 for (i=p.top;i>0;i--)
	 printf ("\n %2d%6d",i,p.elem[i]);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?