์ž๋ฃŒ๊ตฌ์กฐ

์Šคํƒ Stack

์•„์˜ณ์ด 2021. 8. 15. 19:29

์Šคํƒ(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);
}