Array based STACK
What is it?
- Array 기반의 Stack
- 기본적으로 Push (Insert), Pop (Delete)연산이 가능한 FILO (First-In Last-Out) 자료구조이다.
- 너무 간단한 코드라 자세한 설명은 생략한다.
- FILO란? 먼저 들어온 자료가 마지막에 나가는 것으로, 쌓아논 책을 위에서부터 습득하는 것을 연상하면 된다.
CODE
/****************************************************
* Array 기반의 STACK *******************************
* *********************************** st.c **********
* **************************************************/
#include<stdio.h>
/* 배열의 크기를 상수로 지정 */
#define MAX_BUFF 10
/* 배열에 들어가는 데이터 */
typedef struct sData
{
int skey; //키
char svalue; //값
} sData;
/* 크기가 MAX_BUFF인 스택 선언 */
struct sData stack[MAX_BUFF];
/* Information for Top of Stack */
int sTop;
/* Information for number of datas */
int sNum; // 데이터가 들어온 총 횟수
int countPop; // 삭제연산의 수
/* Stack Initiation */
void sInit()
{
/* Top을 1로, 나머지는 모두 초기화한다 */
sTop = 1;
sNum = 0;
countPop = 0;
for(int i=0; i<MAX_BUFF; i++)
{
stack[i].skey = '\0';
stack[i].svalue = '\0';
}
}
/****************************************************************
* Push 후 출력과 Pop 후 출력을 구별하는 게 좀 더 깔끔해서 구별함 *
* ***************************************************************/
/* Stack Print for Push */
void sPrintPush()
{
printf("\n Current stack's status is :\n\n");
printf(" KEY VALUE\n");
for(int i=sTop-1; i>=0; i--)
{
printf(" %d %s\n", stack[i].skey, &stack[i].svalue);
}
printf("\n");
printf("The number of data which you into : %d\n", sNum);
printf("The number of data which current exist : %d\n\n", sNum-countPop);
}
/* Stack Print for Pop */
void sPrintPop()
{
printf("\n Current stack's status is :\n\n");
printf(" KEY VALUE\n");
for(int i=sTop-2; i>=0; i--)
{
printf(" %d %s\n", stack[i].skey, &stack[i].svalue);
}
printf("\n");
printf("The number of data which you into : %d\n", sNum);
printf("The number of data which current exist : %d\n\n", sNum-countPop);
}
/* Stack Insert the data 'val' */
void sPush(char val)
{
if(sTop-1>=MAX_BUFF)
{
/* If the STACK is full */
printf("\nerror::stack overflow.\n");
}
else
{
sNum++;
stack[sTop-1].skey = sNum;
stack[sTop-1].svalue = val;
sPrintPush();
sTop++;
}
}
/* Stack Delete the data of top */
void sPop()
{
if(sTop-1==0)
{
/* If the STACK is empty */
printf("\nerror::stack is already empty.\n");
}
else
{
sTop--;
printf("Poped data : %s\n", &stack[sTop].svalue);
stack[sTop-1].skey = '\0';
stack[sTop-1].svalue = '\0';
countPop++;
sPrintPop();
}
}
/* MAIN FUNCTION */
int main()
{
sInit();
printf("\n\nIt's an array based STACK.\n\n");
int menu;
char push;
while(menu!=3)
{
printf("\nYou make a choice to menu.\n\n");
printf("Press '0' If you wanna Initiation.\n");
printf("Press '1' If you wanna push.\n");
printf("Press '2' Ifyou wanna pop.\n");
printf("Press '3' If you wanna exit.\n\n");
printf(" PRESS : ");
scanf("%d", &menu);
switch(menu)
{
case 0:
printf("You pressed '0' to Initiation.\n");
printf("So, the stack is cleared.\n");
sInit();
sPrintPop();
break;
case 1:
printf("You pressed '1' to Push.\n");
printf("Press the character which you want to push. : ");
scanf("%s", &push);
sPush(push);
break;
case 2:
printf("You pressed '2' to Pop.\n");
sPop();
break;
case 3:
printf("Exit......\n\n");
break;
default:
printf("It's wrong number. press again.\n\n");
break;
} // switch
} // while
return 0;
}
/**************************************************** * Array 기반의 STACK ******************************* * *********************************** st.c ********** * **************************************************/ #include<stdio.h> /* 배열의 크기를 상수로 지정 */ #define MAX_BUFF 10 /* 배열에 들어가는 데이터 */ typedef struct sData { int skey; //키 char svalue; //값 } sData; /* 크기가 MAX_BUFF인 스택 선언 */ struct sData stack[MAX_BUFF]; /* Information for Top of Stack */ int sTop; /* Information for number of datas */ int sNum; // 데이터가 들어온 총 횟수 int countPop; // 삭제연산의 수 /* Stack Initiation */ void sInit() { /* Top을 1로, 나머지는 모두 초기화한다 */ sTop = 1; sNum = 0; countPop = 0; for(int i=0; i<MAX_BUFF; i++) { stack[i].skey = '\0'; stack[i].svalue = '\0'; } } /**************************************************************** * Push 후 출력과 Pop 후 출력을 구별하는 게 좀 더 깔끔해서 구별함 * * ***************************************************************/ /* Stack Print for Push */ void sPrintPush() { printf("\n Current stack's status is :\n\n"); printf(" KEY VALUE\n"); for(int i=sTop-1; i>=0; i--) { printf(" %d %s\n", stack[i].skey, &stack[i].svalue); } printf("\n"); printf("The number of data which you into : %d\n", sNum); printf("The number of data which current exist : %d\n\n", sNum-countPop); } /* Stack Print for Pop */ void sPrintPop() { printf("\n Current stack's status is :\n\n"); printf(" KEY VALUE\n"); for(int i=sTop-2; i>=0; i--) { printf(" %d %s\n", stack[i].skey, &stack[i].svalue); } printf("\n"); printf("The number of data which you into : %d\n", sNum); printf("The number of data which current exist : %d\n\n", sNum-countPop); } /* Stack Insert the data 'val' */ void sPush(char val) { if(sTop-1>=MAX_BUFF) { /* If the STACK is full */ printf("\nerror::stack overflow.\n"); } else { sNum++; stack[sTop-1].skey = sNum; stack[sTop-1].svalue = val; sPrintPush(); sTop++; } } /* Stack Delete the data of top */ void sPop() { if(sTop-1==0) { /* If the STACK is empty */ printf("\nerror::stack is already empty.\n"); } else { sTop--; printf("Poped data : %s\n", &stack[sTop].svalue); stack[sTop-1].skey = '\0'; stack[sTop-1].svalue = '\0'; countPop++; sPrintPop(); } } /* MAIN FUNCTION */ int main() { sInit(); printf("\n\nIt's an array based STACK.\n\n"); int menu; char push; while(menu!=3) { printf("\nYou make a choice to menu.\n\n"); printf("Press '0' If you wanna Initiation.\n"); printf("Press '1' If you wanna push.\n"); printf("Press '2' Ifyou wanna pop.\n"); printf("Press '3' If you wanna exit.\n\n"); printf(" PRESS : "); scanf("%d", &menu); switch(menu) { case 0: printf("You pressed '0' to Initiation.\n"); printf("So, the stack is cleared.\n"); sInit(); sPrintPop(); break; case 1: printf("You pressed '1' to Push.\n"); printf("Press the character which you want to push. : "); scanf("%s", &push); sPush(push); break; case 2: printf("You pressed '2' to Pop.\n"); sPop(); break; case 3: printf("Exit......\n\n"); break; default: printf("It's wrong number. press again.\n\n"); break; } // switch } // while return 0; }
'IT > C - Programming' 카테고리의 다른 글
[C코드] :: MULTITHREAD QUEUE code (멀티 스레드를 이용한 큐 코드) (3) | 2019.05.24 |
---|---|
[C개념] :: 자료형(Data type) 별 크기 및 범위 (0) | 2018.09.28 |
[C코드] :: BINARY SEARCH code (이진 탐색 코드) (0) | 2018.03.28 |
[C코드] :: INSERTION SORT code (삽입 정렬 코드) (0) | 2018.03.27 |
[C코드] :: Array based QUEUE code (배열 기반 큐 코드) (0) | 2018.03.26 |