기초/자료구조

[010]스택(구조체)

Yoiiin 2022. 4. 3. 01:16
SMALL
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
#define MAX_STRING 100

/* 스택에 저장되는 데이터를 구조체로 정의 */
typedef struct
{
    int student_no;
    char name[MAX_STRING];
    char address[MAX_STRING];
} element;

element stack[MAX_STACK_SIZE];
int top=-1;

//공백상태 검출
int is_empty()
{
	return (top == -1);
}

//포화상태 검출
int is_full()
{
	return (top == (MAX_STACK_SIZE - 1));
}

//삽입
void push(element item)
{
    if(is_full()){
    	fprintf(stderr, "스택 포화 에러\n");
    	return;
    }
    else stack[++top] = item;
}

//삭제
element pop()
{
    if (is_empty()){
    	fprintf(stderr, "스택 공백 에러\n");
        exit(1);
    }
    else return stack[top--];
}

//피크
element peek()
{
    if (is_empty()){
    	fprintf(stderr, "스택 공백 에러\n");
        exit(1);
    }
    else return stack[top];
}

int main(void)
{
    element ie={ 2019001, "Hong", "Seoul" };
    element oe;
    
    push(ie);
    oe=pop();
    
    printf("학번 : %d\n", oe.student_no);
    printf("이름 : %s\n", oe.name);
    printf("주소 : %s\n", oe.address);
    return 0;
}
* 실행결과 *

학번 : 20190001
이름 : Hong
주소 : Seoul

 

 

 

 

 

 

 

 

 

 

LIST