Stack ADT Definitions
// Stack ADT Type Definitions
typedef struct node
{
void* dataPtr;
struct node* link;
} STACK_NODE;
typedef struct
{
int count;
STACK_NODE* top;
} STACK;
ADT Create Stack
STACK* createStack (void)
{
// Local Definitions
STACK* stack;
printf("===== createStack()\n");
// Statements
stack = (STACK*)malloc(sizeof(STACK));
if(stack)
{
stack->count = 0;
stack->top = NULL;
} // if
printf("\t stack: %x, stck->count: %x, stack->top: %x\n", stack, stack->count, stack->top);
return stack;
} // createStack
Push Stack
bool pushStack (STACK* stack, void* dataInPtr)
{
// Local Definitions
STACK_NODE* newPtr;
newPtr = (STACK_NODE*) malloc(sizeof(STACK_NODE));
if (!newPtr)
return false;
newPtr->dataPtr = dataInPtr;
newPtr->link = stack->top;
stack->top = newPtr;
(stack->count)++;
printf("===== pushStack()\n");
printf("\t stack->count: %d, stack->top: %x\n", stack->count , stack->top);
printf("\t dataInPtr: %x, *dataInPtr: %d\n", dataInPtr, *((int*)dataInPtr));
printf("\t newPtr->dataPtr: %x, *newPtr->dataPtr: %d, newPtr->link: %x\n",
newPtr->dataPtr, *((int*)newPtr->dataPtr), newPtr->link);
return true;
} // pushStack
ADT Pop Stack
void* popStack (STACK* stack)
{
// Local Definitions
void* dataOutPtr;
STACK_NODE* temp;
printf("===== popStack()\n");
printf("\t stack->count: %d, stack->top: %x\n", stack->count , stack->top);
printf("\t stack->top->dataPtr: %x, *stack->top->dataPtr: %d\n",
stack->top->dataPtr, *(int*)stack->top->dataPtr);
// Statements
if (stack->count == 0)
dataOutPtr = NULL;
else
{
temp = stack->top;
dataOutPtr = stack->top->dataPtr;
stack->top = stack->top->link;
free (temp);
(stack->count)--;
} // else
printf("\t dataOutPtr: %x, *dataPtr: %d\n", dataOutPtr, *(int*)dataOutPtr);
return dataOutPtr;
} // popStack
Retrieve Stack Top
void* stackTop (STACK* stack)
{
// Statements
if (stack->count == 0)
return NULL;
else
return stack->top->dataPtr;
} // stackTop
Empty Stack
bool emptyStack (STACK* stack)
{
if (stack->top == 0) return true;
printf("\n===== emptyStack()\n");
printf("\t stack->count: %d, stack->top: %x\n", stack->count, stack->top);
// Statements
return (stack->count == 0);
} // emptyStack
Full Stack
bool fullStack (STACK* stack)
{
// Local Definitions
STACK_NODE* temp;
// Statements
if ((temp = (STACK_NODE*)malloc (sizeof(*(stack->top)))))
{
free (temp);
return false;
} // if
// malloc failed
return true;
} // fullStack
Stack Count
int stackCount (STACK* stack)
{
// Statements
return stack->count;
} // stackCount
Destroy Stack
STACK* destroyStack (STACK* stack)
{
// Local Definitions
STACK_NODE* temp;
printf("===== destroyStack()\n");
printf("\t stack->count: %d, stack->top: %x\n", stack->count, stack->top);
// Statements
if (stack)
{
// Delete all nodes in stack
while (stack->top != NULL)
{
printf("\t stack->count: %d, stack->top: %x\n", stack->count, stack->top);
// Delete data entry
free (stack->top->dataPtr);
temp = stack->top;
stack->top = stack->top->link;
free (temp);
} // while
// Stack now empty. Destroy stack head node.
free (stack);
} // if stack
return NULL;
} // destroyStack
#include <stdio.h>
#include <stdlib.h>
#include "stacksADT.h"
{
// Local Definitions
unsigned int num;
int* digit;
STACK* stack;
puts("==================================");
puts("Convert Decimal to Binary");
puts("==================================\n");
// Create Stack
stack = createStack ();
printf ("\nEnter an integer : ");
scanf("%d", &num);
while (num > 0)
{
digit = (int*) malloc (sizeof(int));
*digit = num % 2;
pushStack(stack, digit);
printf ("===== main()\n");
printf ("\t &num: %x, num: %d\n", &num, num);
printf ("\t digit: %x, *digit: %d\n\n", digit, *digit);
num = num /2;
} // while
printf ("\nThe binary number is : ");
while (!emptyStack (stack))
{
digit = (int*)popStack(stack);
printf ("===== main()\n");
printf ("\t binary number: %1d\n", *digit);
} // while
printf ("\n");
destroyStack (stack);
return 0;
} // main
'Papers > C/C++' 카테고리의 다른 글
Windows XP에서 Visual Studio 2008 설치 후 나타나는 컴파일 에러 (0) | 2010.10.07 |
---|---|
void pointer vs null pointer (0) | 2010.08.12 |
스택과 큐의 용도 (0) | 2010.04.06 |