์คํ Stack
์คํ(Stack) : LIFO (Last-In-First-Out) = ํ์ ์ ์ถ
- ๋ฆฌ์คํธ์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ ํ ์๋ฃ๊ตฌ์กฐ but ๋ฆฌ์คํธ์ ๋ค๋ฅด๊ฒ ์๋ฃ์ ์ถ๊ฐ, ์ ๊ฑฐ๊ฐ ์คํ์ ๋งจ์(top)์์๋ง ์ด๋ฃจ์ด์ง
์คํ์ ์ฐ์ฐ
ํธ์(push) : ์คํ์ ๋งจ ์์ ์๋ก์ด ์๋ฃ๋ฅผ ์ถ๊ฐ, ์คํ์ ํฌ๊ธฐ๋ฅผ ์ด๊ณผํด pushํ๋ฉด ์ค๋ฒํ๋ก์ฐ
ํ(pop) : ์คํ์ ๋งจ ์์ ์๋ฃ๋ฅผ ์ ๊ฑฐํ๊ณ ๋ฐํ, ๋น ์คํ์ popํ๋ฉด ์ธ๋ํ๋ก์ฐ
ํผํฌ(peek) : ์คํ์ ๋งจ ์์ ์๋ฃ๋ฅผ ์ ๊ฑฐํ์ง ์๊ณ ๋ฐํ
๋ฐฐ์ด์ ์ด์ฉํ ์คํ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ArrayStackNodeType { // ๋
ธ๋
char data;
} ArrayStackNode;
typedef struct ArrayStackType { // ์คํ
int maxCount;
int curruntCount;
ArrayStackNode *pData;
} ArrayStack;
ArrayStack *creatArrayStack(int size)
{
ArrayStack *pReturn = NULL;
pReturn = (ArrayStack *)malloc(sizeof(ArrayStack));
memset(pReturn, 0, sizeof(ArrayStack));
pReturn->maxCount = size;
pReturn->pData = (ArrayStackNode *)malloc(sizeof(ArrayStackNode) * size);
memset(pReturn->pData, 0, sizeof(ArrayStackNode) * size);
return (pReturn);
}
int isArrayStackFull(ArrayStack *pStack)
{
int ret = 0;
if (pStack != NULL)
{
if (pStack->curruntCount == pStack->maxCount)
ret = 1;
}
return (ret);
}
int pushAS(ArrayStack *pStack, char data)
{
int ret = 0;
if (isArrayStackFull(pStack) == 0)
{
pStack->pData[pStack->curruntCount].data = data;
pStack->curruntCount++;
ret = 1;
}
else
printf("์ค๋ฅ, ์คํ์ด ๊ฐ๋ ์ฐผ์ต๋๋ค, pushAS()\n");
return (ret);
}
int isArrayStackEmpty(ArrayStack *pStack)
{
int ret = 0;
if (pStack != NULL)
{
if (pStack->curruntCount == 0)
ret = 1;
}
return (ret);
}
ArrayStackNode *popAS(ArrayStack *pStack)
{
ArrayStackNode *pReturn = NULL;
if (isArrayStackEmpty(pStack) == 0)
{
pReturn = (ArrayStackNode *)malloc(sizeof(ArrayStackNode));
if (pReturn != NULL)
{
pReturn->data = pStack->pData[pStack->curruntCount - 1].data;
pStack->curruntCount--;
}
else
printf("์ค๋ฅ, ๋ฉ๋ชจ๋ฆฌ ํ ๋น, popAS()\n");
}
return (pReturn); // ๋์ ํ ๋น์ฃผ์๋ฅผ ๋ฐํ-> ๋ฉ๋ชจ๋ฆฌ ํด์ ๋ ํธ์ถํ ๊ณณ์์
}
ArrayStackNode *peekAS(ArrayStack *pStack)
{
ArrayStackNode *pReturn = NULL;
if (pStack != NULL)
{
if (isArrayStackEmpty(pStack) == 0)
pReturn = &(pStack->pData[pStack->curruntCount - 1]);
}
return (pReturn); //peek์ ์๋ฃ๋ฅผ ์ ๊ฑฐํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ํด์ X
}
/*
pop : ๋
ธ๋ ๋์ ํ ๋น์ฃผ์ ๋ฐํ (ํธ์ถํ ์ชฝ์์ ๋ฉ๋ชจ๋ฆฌ ํด์ )
peek : ํ๋
ธ๋ ์ฃผ์๋ฐํ (๋ฉ๋ชจ๋ฆฌ ํด์ X)
*/
void deleteArrayStack(ArrayStack *pStack)
{
if (pStack != NULL)
{
if (pStack->pData != NULL)
free(pStack->pData);
free(pStack);
}
}
void displayArrayStack(ArrayStack *pStack)
{
int i = 0;
if (pStack != NULL)
{
int size = pStack->maxCount;
int top = pStack->curruntCount;
printf("์คํ ํฌ๊ธฐ: %d, ํ์ฌ ๋
ธ๋ ๊ฐ์:%d\n",
pStack->maxCount, pStack->curruntCount);
for (i = size - 1; i >= top; i--)
printf("[%d]-[Empty]\n", i);
for (i = top - 1; i >= 0; i--)
printf("[%d]-[%c]\n", i, pStack->pData[i].data);
}
}
int main(void)
{
ArrayStack *pStack = NULL;
ArrayStackNode *pNode = NULL;
pStack = creatArrayStack(6);
if (pStack != NULL)
{
pushAS(pStack, 'A');
pushAS(pStack, 'B');
pushAS(pStack, 'C');
pushAS(pStack, 'D');
displayArrayStack(pStack);
pNode = popAS(pStack);
if (pNode != NULL)
{
printf("pop ๊ฐ-[%c]\n", pNode->data);
free(pNode);
}
displayArrayStack(pStack);
pNode = peekAS(pStack);
if (pNode != NULL)
printf("peek ๊ฐ-[%c]\n", pNode->data);
displayArrayStack(pStack);
deleteArrayStack(pStack);
}
return (0);
}
ํฌ์ธํฐ๋ฅผ ์ด์ฉํ ์คํ
- ์คํ์ maxcount๋ฅผ ์ ํ์ง ์์๋ ๋จ
- ๋ฐฐ์ด์ ์ด์ฉํ ์คํ์ ํ๋ ธ๋ ์ ๊ทผ์ ๊ฑธ๋ฆฌ๋ ์๊ฐ ๋ณต์ก๋ : O(1)
ํฌ์ธํฐ๋ฅผ ์ด์ฉํ ์คํ์ ํ๋ ธ๋ ์ ๊ทผ์ ๊ฑธ๋ฆฌ๋ ์๊ฐ ๋ณต์ก๋ : O(n)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct LinkedStackNodetype {
char data;
struct LinkedStackNode *pLink; //๋งํฌ
} LinkedStackNode;
typedef struct LinkedStacktype {
int currentCount;
LinkedStackNode *pTop; // ํ ๋
ธ๋์ ํฌ์ธํฐ
} LinkedStack;
LinkedStack *createLinkedStack(void)
{
LinkedStack *pReturn = NULL;
pReturn = (LinkedStack *)malloc(sizeof(LinkedStack));
memset(pReturn, 0, sizeof(LinkedStack));
return (pReturn);
}
int pushLS(LinkedStack *pStack, char data)
{
int ret = 0;
LinkedStackNode *pNode = NULL;
if (pStack != NULL)
{
pNode = (LinkedStackNode *)malloc(sizeof(LinkedStackNode));
if (pNode != NULL)
{
pNode->data = data;
pNode->pLink = pStack->pTop; // ์๋ก ์ถ๊ฐํ ๋
ธ๋์ ์ด์ ๋
ธ๋๋ฅผ ๊ธฐ์กด ํ๋
ธ๋๋ก ์ง์
pStack->pTop = pNode; // ํ๋
ธ๋๋ฅผ ์๋ก ์ถ๊ฐํ ๋
ธ๋๋ก ๋ณ๊ฒฝ
pStack->currentCount++;
ret = 1;
}
else
printf("์ค๋ฅ, ๋ฉ๋ชจ๋ฆฌ ํ ๋น, pushLS()\n");
}
return (ret);
}
int isLinkedStackEmpty(LinkedStack *pStack)
{
int ret = 0;
if (pStack != NULL)
{
if (pStack->currentCount == 0)
ret = 1;
}
return (ret);
}
LinkedStackNode *popLS(LinkedStack *pStack)
{
LinkedStackNode *pReturn = NULL;
if (pStack != NULL)
{
if (isLinkedStackEmpty(pStack) == 0)
{
pReturn = pStack->pTop;
pStack->pTop = pReturn->pLink; // ์๋ก์ด ํ๋
ธ๋๋ฅผ ๊ธฐ์กด์ ํ๋
ธ๋ ์ด์ ๋
ธ๋๋ก ์ง์
pReturn->pLink = NULL; // ๋ฐํ ๋
ธ๋์ ์ด์ ๋
ธ๋ ์ ๋ณด๋ฅผ ์ด๊ธฐํ
pStack->currentCount--;
}
}
return (pReturn); // ๋์ ํ ๋น ์ฃผ์๋ฅผ ๋ฐํ-> ๋ฉ๋ชจ๋ฆฌ ํด์ ๋ ํธ์ถํ ๊ณณ์์
}
LinkedStackNode *peekLS(LinkedStack *pStack)
{
LinkedStackNode *pReturn = NULL;
if (pStack != NULL)
{
if (isLinkedStackEmpty(pStack) == 0)
pReturn = pStack->pTop;
}
return (pReturn); // ๋
ธ๋์ ์ฃผ์๋ฅผ ๋ฐํ-> ๋ฉ๋ชจ๋ฆฌ ํด์ X
}
void deleteLinkedStack(LinkedStack *pStack)
{
LinkedStackNode *pNode = NULL;
LinkedStackNode *pDelNode = NULL;
if (pStack != NULL)
{
pNode = pStack->pTop;
while (pNode != NULL)
{
pDelNode = pNode;
pNode = pNode->pLink;
free(pDelNode);
}
free(pStack);
}
}
void displayLinkedStack(LinkedStack *pStack)
{
LinkedStackNode *pNode = NULL;
int i = 1;
if (pStack != NULL)
{
printf("ํ์ฌ ๋
ธ๋ ๊ฐ์: %d\n", pStack->currentCount);
pNode = pStack->pTop;
while (pNode != NULL)
{
printf("[%d]-[%c]\n",
pStack->currentCount - i, pNode->data);
i++;
pNode = pNode->pLink;
}
}
}
int main(void)
{
LinkedStack *pStack = NULL;
LinkedStackNode *pNode = NULL;
pStack = createLinkedStack();
if (pStack != NULL)
{
pushLS(pStack, 'A');
pushLS(pStack, 'B');
pushLS(pStack, 'C');
pushLS(pStack, 'D');
displayLinkedStack(pStack);
pNode = popLS(pStack);
if (pNode != NULL)
{
printf("pop-[%c]\n", pNode->data);
free(pNode);
}
displayLinkedStack(pStack);
pNode = peekLS(pStack);
if (pNode != NULL)
printf("peek-[%c]\n", pNode->data);
displayLinkedStack(pStack);
deleteLinkedStack(pStack);
}
return (0);
}