본문 바로가기

Papers/C/C++

Stack ADT(Abstract Data Type)

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

stackADT.h

convert.c


#include <stdio.h>

#include <stdlib.h>
#include "stacksADT.h"
int main (void)
{
// Local Definitions
 unsigned int    num;
          int*   digit;
          STACK* stack;
// what do i work about?
 puts("==================================");
 puts("Convert Decimal to Binary");
 puts("==================================\n");
// Statements
 // Create Stack
 stack = createStack ();
 // prompt and read a number
 printf ("\nEnter an integer : ");
 scanf("%d", &num);
 // create 0s and 1s and push them into the stack
 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
 // Binary number created. Now print it
 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");
 // Destroying Stack
 destroyStack (stack);
 return 0;
} // main