栈的基本操作及进制转换

版权声明:学习交流:qq709079239 https://blog.csdn.net/qq_43267360/article/details/82872951
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 1024
#define SIZEPLUS  20
#define OK 1
#define OVERFLLOW 0
#define ERROR 0
typedef int ElemType ;
typedef int Status ;


//栈的存储表示
typedef struct{
	ElemType *base;
	ElemType *top;
	int stacksize;//当前已分配的存储空间,以元素为单位
}Stack;



Status InitStack(Stack &S){ //创建一个空栈S
	S.base=(ElemType * )malloc( MAXSIZE * sizeof (ElemType));
	if(!S.base) exit(OVERFLLOW);//存储分配失败
	S.top=S.base;
	S.stacksize=MAXSIZE;
	return OK;
}
  
  
Status StackEmpty(Stack S) //若栈s为空栈,则返回ok;否则返回error; 
  { if(S.top==S.base)
    return OK;
	else return ERROR; 
  
  } 
  
Status ClearStack(Stack &S) //把S置为空栈 
  { S.top=S.base;
    return OK;
   } 
 

Status GetTop(Stack S,ElemType &e){
	//若栈不空,用e返回s的栈顶元素,并返回ok;否则返回ERROR
	if(S.top==S.base) return ERROR;
	 e=*(S.top-1); //非空栈的指针始终在栈顶元素的下一个位置上 
	 return e;
}

Status Push(Stack &S, ElemType e)
{
	if(S.top-S.base>=S.stacksize)
	{
		S.base=(ElemType*)realloc (S.base,(S.stacksize+MAXSIZE) * sizeof (ElemType));
		if(!S.base) exit(0);
			S.top=S.base+S.stacksize;
		S.stacksize+=SIZEPLUS;
	}
	*S.top++=e;
	return OK;
}//入栈插入



Status Pop(Stack &S,ElemType &e) //若栈不空,则删除S的栈顶元素,用e返回其值,并返回ok;否则返回error
{if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
 } 

Status StackTraverse(Stack S){
    if(S.top==S.base){return ERROR; }    
	while(S.base <S.top ){        
		printf("%d\t",*(S.base++));}   
	    printf("\n");    
		return OK;
}


Status StackLength(Stack S){//栈的长度
    if(S.base==S.top){
        return 0;
    }
    return S.top-S.base;
}


//进制转换
int conversion(	Stack &S){int e;

InitStack(S);
	int X,R;
	printf("请输入一个十进制数和要转换的进制");
	scanf("%d",&X);
	scanf("%d",&R);
	while(X){
	   Push(S,X % R);
	   X=X/R;
	   }
	while(!StackEmpty(S)){
		Pop(S,e);
		printf("%d",e);}
	return OK;

	
}





void main()
{
	while(1)
	{
		system("cls");
		printf("请用数字键选择操作\n");
		printf("1. 初始化顺序栈 \n");
		printf("2. 元素入栈\n");
		printf("3. 检查顺序栈是否为空 \n");
		printf("4. 把S置为空栈 \n");
		printf("5. 把栈顶元素弹出\n");
		printf("6. 取栈顶元素 \n");
		printf("7. 输出顺序栈中元素\n");
		printf("8. 求栈的长度\n");
		printf("9. 进制转换\n");
		printf("0.退出\n\n");


		Stack stack;
		int choose;
		int a[5]={1,2,3,4,5};
		int e,q;
		int popElem=NULL;
		printf("选择:");
		scanf("%d",&choose);
		switch(choose)
		{
			case 1:
				InitStack(stack);
				system("pause");
				break;

			case 2: int n;
				for(n=0;n<5;n++)
				Push(stack,a[n]);
				printf("已入栈!\n");
				system("pause");
				break;


			case 3:
				int i;
				i=StackEmpty(stack);
				if(i==1)
				printf("空栈\n");
				else printf("不是空栈\n");
				system("pause");
				break;


			case 4:
				ClearStack(stack);
				printf("已经置空\n");
				system("pause");
				break;

			case 5:
				Pop(stack,popElem);
				printf("%d",popElem);
				system("pause");
				break;

			case 6:
				e=GetTop(stack,e);
				printf("栈顶元素是:%d\n",e);
				system("pause");
				break;

			case 7:
				StackTraverse(stack);
				system("pause");
				break;

		
			case 8:
				q=StackLength(stack);
				printf("栈的长度为%d\n:",q);
				system("pause");
				break;
			case 9:
				conversion(stack);
				 system("pause");
				 break;


			case 0:
				exit(0);
				break;

			default:
				printf("输入错误!");
				system("pause");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43267360/article/details/82872951