⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop.cpp

📁 自己写的数据结构代码
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>


#define OK 1
#define ERROR -1
typedef int SElemType;
typedef int Status;
#define STACK_INIT_SIZE 100000
#define STACKINCREMENT 2

struct SqStack
{
	SElemType *base;
	SElemType *top;
	int stacksize;
};

Status InitStack(SqStack &S)
{
	S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
	S.top=S.base;
	S.stacksize=STACK_INIT_SIZE;
	return OK;
}

Status Push(SqStack &S,SElemType e)
{
	if(S.top-S.base>=S.stacksize)
	{
		S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
		
		S.top=S.base+S.stacksize;
		S.stacksize=S.stacksize+STACKINCREMENT;
	}
	*S.top++=e;
	
	return OK;
}

Status Pop(SqStack &S,SElemType &e)
{
	if(S.top==S.base) return ERROR;
	
	e=* --S.top;
	return OK;
}

Status StackLength(SqStack S)
{
	return S.top-S.base;
}
Status GetTop(SqStack S,SElemType &e)
{
	if(S.top==S.base) return ERROR;
	e=*(S.top-1);
	return OK;
}

int main()
{
	SqStack s;
	int choose,e;
	if(InitStack(s)==OK)
	{
		while(1)
		{
			printf("1:push 2:pop 3:length of stack 4:gettop 5:exit please choose:\n");
			scanf("\n%d",&choose);
			switch(choose)
			{
			case 1:scanf("\n%d",&e);
				if(Push(s,e)==OK)
				printf("%d is pushed.\n",e);
				break;
			case 2:if(Pop(s,e)==OK)
				   printf("%d is popped.\n",e);
				else
					printf("the stack is empty.\n");
				break;
			break;
			case 3:printf("Length of the stack is %d\n",StackLength(s));
				break;
			case 4:if(GetTop(s,e)==OK)
					   printf("%d is top.\n",e);
				else
					printf("The stack is empty.\n");
				break;
			case 5:return 0;
			}
		}
	}
	return 0;
}

⌨️ 快捷键说明

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