A sentimental robot

Stack 본문

Data Structure

Stack

GOD03219 2018. 1. 3. 13:23

#include<stdio.h>

int Push(struct Stack* s, int data);
int Pop(struct Stack* s, int* data);
struct Stack
{
 int data[5];
 int top;

};

int Push(struct Stack*s, int data){


 if(s->top>=5) return 0;

 

 s->data[s->top++]=data;

 

/*

s->data[s->top]=data;
 s->top++;

*/
 return 1;

 
}
int Pop(struct Stack *s, int *data){

/*

s->top--;
 *data=s->data[s->top];

*/
 *data=s->data[--s->top];
 return 1;


}

void main()
{
 Stack s = {0};
 int data;

 Push(&s,10);
 Push(&s,20);
 Push(&s,30);
 Push(&s,40);
 Push(&s,50);
 Push(&s,60);

 Pop(&s,&data); printf("data = %d\n",data); //50
 Pop(&s,&data); printf("data = %d\n",data); //40
 Pop(&s,&data); printf("data = %d\n",data); //30
 Push(&s,70);
 Push(&s,80);
 Push(&s,90);

 Pop(&s,&data); printf("data = %d\n",data); //90
 Pop(&s,&data); printf("data = %d\n",data); //80
 Pop(&s,&data); printf("data = %d\n",data); //70
 Pop(&s,&data); printf("data = %d\n",data); //20
 Pop(&s,&data); printf("data = %d\n",data); //10

 


}

'Data Structure' 카테고리의 다른 글

Single LinkedList Exercise3  (0) 2018.01.03
Map에 대하여..  (0) 2018.01.03
Linear Queue  (0) 2018.01.03
Babygin  (0) 2018.01.03
Baseball Game(computer vs user)  (0) 2018.01.03