C 언어 시 뮬 레이 션 스 택

1657 단어 C 언어
헤더 파일:
#include
#include
#include

#define N 100

struct stack
{
	int a[N];
	int top;	//    

};
//   
typedef struct stack Stack;

//       ,        。
void init(Stack *p);		//    
int isempty(Stack *p);		//       
int isfull(Stack *p);		//      
int gettop(Stack *p);		//    
void push(Stack *p,int key);		//  
void pop(Stack *p);			//  
void show(Stack *p);	//        

stack.c  주 함수:
#include"stack.h"


//     
void init(Stack *p)
{
	p->top = -1;	//-1    
	//        ,         ,        
	memset(p->a, 0, sizeof(int)*N);	//        ,        0

}
//       
int isempty(Stack *p)
{
	if (p->top == -1)
	{
		return 1; //  
	}
	else
	{
		return 0;	//   
	}
}
//       
int isfull(Stack *p)
{
	if (p->top == N-1)
	{
		return 1;	//  
	}
	else
	{
		return 0;	//    
	}
}
//      
int gettop(Stack *p)
{
	return p->a[p->top];
}
//  ,key      
void push(Stack *p, int key)
{
	
	//   1       	
	if (isfull(p))
	{
		printf("  ,    !");
		return;
	}
	else
	{
		//      1
		p->top = p->top + 1;
		//  		
		p->a[p->top] = key;
	}
}
void pop(Stack *p)
{
	//       
	if (isempty(p))
	{
		printf("   ,    !");
		return;
	}
	else
	{
		printf("%d
", p->a[gettop(p)]); p->top = p->top - 1; } } // void show(Stack *p) { while (!isempty(p)) { printf("%3d", p->a[p->top]); p->top -= 1; } } void main() { Stack stack; // init(&stack); for (int i = 0; i < 5; i++) { push(&stack,i); } show(&stack); system("pause"); }

좋은 웹페이지 즐겨찾기