창고 의 기본 조작 - 데이터 구조
                                            
 2121 단어  데이터 구조
                    
기본 작업: 스 택 초기 화, 스 택 에 들 어가 고 스 택 에서 나 오 며 스 택 꼭대기 요 소 를 가 져 와 빈 스 택 인지 판단 하고 스 택 을 비 웁 니 다.
예 코드:
#include
#include
#include
#define Initsize 100
#define Increase 100
#define ERROR 0
#define OK 1
#define OVERFLOW -1
typedef int Status;
typedef int Elemtype;
typedef struct SqStack
{
	Elemtype *base,*top;
	int stacksize;
}SqStack;
//init stack
Status InitStack(SqStack &s)
{
	s.base=(Elemtype*)malloc(Initsize*sizeof(Elemtype));
	if(!s.base) return ERROR;
 	s.top=s.base;
	s.stacksize=Initsize;
	return OK;
}
//push stack
Status Pushstack(SqStack &s,Elemtype e)
{
	Elemtype *temp;
	if(s.top-s.base+1>=s.stacksize)//stack full
	{
		temp=(Elemtype*)realloc(temp,(Increase+s.stacksize)*sizeof(Elemtype)); 
		if(!temp) return ERROR;
		s.base=temp;
		s.top=s.base+s.stacksize;  //remalloc changes the s.top's address,relocate s.top pointer
		s.stacksize+=Increase; 
	}    
	*s.top=e;
	s.top++;
	return OK;
}
//stack if empty
bool EmptyStack(SqStack s)
{
	if(s.base==s.top) return true;
	else return false;
}
//print stack
Status DisplayStack(SqStack s)
{
	if(EmptyStack(s)) printf("Stack is empty!
");
	else
	{
		while(s.top!=s.base)
		{
			s.top--;
			printf("%d ",*s.top);
		}
		printf("
");
	}
	return OK;
}
//top elem pop stack 
Status PopStackelem(SqStack &s)
{
	if(EmptyStack(s))
	{
		printf("Stack is empty!
");
		return ERROR;
	}
	s.top--;
	return OK;
}  
//get top elem
Elemtype GetStacktop(SqStack s,Elemtype &e)
{
	if(EmptyStack(s))
	{
		printf("Stack is empty!
");
		return ERROR;
	} 
	s.top--;
	e=*s.top;
	return OK;	
}
Status ClearStack(SqStack &s)
{
	s.top=s.base;
	s.stacksize=0;
	return OK;
}
int main()
{
	SqStack stack;
	int n,i;
	Elemtype e;
	InitStack(stack);
	printf("Input n and the num:");
	scanf("%d",&n);
	for(i=0;i   이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.