Advertisement
Guest User

DStack

a guest
Jun 4th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. // DStack.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <malloc.h>
  6.  
  7. typedef struct
  8. {
  9.     int value;
  10.     float coefficient;
  11.     char* token;
  12.  
  13. }Node;
  14.  
  15. typedef struct DStackTAG
  16. {
  17.     DStackTAG *Next;
  18.     Node _Node;
  19.  
  20. }DStack;
  21.  
  22. void Initial(DStack **DS);
  23. bool Push(DStack **DS, int value, float coefficient, char* token);
  24. bool Pop(DStack **DS, int *value, float *coefficient, char** token);
  25. void PrintStack(DStack *DS);
  26. void Deallocate(DStack  **DS);
  27.  
  28.  
  29. void Initial(DStack **DS)
  30. {   *DS = NULL;  }
  31.  
  32. bool Push(DStack **DS, int value, float coefficient, char* token)
  33. {
  34.     DStack *NewItem;
  35.     NewItem = (DStack *)malloc(sizeof(DStack));
  36.    
  37.     if (NewItem == NULL)
  38.         return false;
  39.     else
  40.     {
  41.         // Values
  42.         NewItem->_Node.value = value;
  43.         NewItem->_Node.coefficient = coefficient;
  44.         NewItem->_Node.token = token;
  45.  
  46.         // Link
  47.         NewItem->Next = *DS;
  48.         *DS = NewItem;
  49.     }
  50. }
  51.  
  52. bool Pop(DStack **DS, int* value, float* coefficient, char** token)
  53. {
  54.     if (*DS == NULL)
  55.         return false;
  56.  
  57.     DStack *Temp = *DS;
  58.    
  59.     // Export values
  60.     *value = (*DS)->_Node.value;
  61.     *coefficient = (*DS)->_Node.coefficient;
  62.     *token = (*DS)->_Node.token;
  63.    
  64.     *DS = (*DS)->Next;
  65.  
  66.     // Free memory
  67.     free(Temp);
  68.     return true;
  69. }
  70.  
  71. void PrintStack(DStack *DS)
  72. {
  73.     DStack *Temp = DS;
  74.  
  75.     printf("\nDynamic Stack\n");
  76.     printf("=================================");
  77.    
  78.     printf("\n");
  79.  
  80.     // Values
  81.     Temp = DS;
  82.     printf("(values):   ");
  83.     while(Temp != NULL)
  84.     {
  85.         printf(" %d \t", Temp->_Node.value);
  86.         Temp = Temp->Next;
  87.     }
  88.     printf("\n");
  89.  
  90.     // Coefficient
  91.     Temp = DS;
  92.     printf("(coefficient):  ");
  93.     while(Temp != NULL)
  94.     {
  95.         printf(" %3.3f \t", Temp->_Node.coefficient);
  96.         Temp = Temp->Next;
  97.     }
  98.     printf("\n");
  99.  
  100.     // Token
  101.     Temp = DS;
  102.     printf("(token):    ");
  103.     while(Temp != NULL)
  104.     {
  105.         printf(" %s\t", Temp->_Node.token);
  106.         Temp = Temp->Next;
  107.     }
  108.  
  109.     printf("\n");
  110. }
  111.  
  112. void Deallocate(DStack **DS)
  113. {
  114.     DStack *Temp = *DS;
  115.  
  116.     while(*DS != NULL)
  117.     {
  118.         *DS = (*DS)->Next;
  119.         free(Temp);
  120.         Temp = *DS;
  121.     }
  122. }
  123.  
  124. int _tmain(int argc, _TCHAR* argv[])
  125. {
  126.     DStack *MyStack;
  127.     int value;
  128.     float coefficient;
  129.     char* token;
  130.  
  131.     Initial(&MyStack);
  132.  
  133.     Push(&MyStack, 10, 3.12, "Italy");
  134.     Push(&MyStack, 20, 7.14, "Greece");
  135.     Push(&MyStack, 30, 2.21, "Obama");
  136.     Push(&MyStack, 40, 9.39, "Athens");
  137.     Push(&MyStack, 50, 5.21, "Insomnia");
  138.     Push(&MyStack, 60, 2.39, "Athens");
  139.  
  140.     PrintStack(MyStack);
  141.  
  142.     printf("\nRemoving from stack...\n");
  143.     Pop(&MyStack, &value, &coefficient, &token);
  144.     printf("value: %d - coefficient: %3.3f - token: %s \n", value, coefficient, token);
  145.     Pop(&MyStack, &value, &coefficient, &token);
  146.     printf("value: %d - coefficient: %3.3f - token: %s \n", value, coefficient, token);
  147.    
  148.  
  149.     printf("\nRe-Print Stack");
  150.     PrintStack(MyStack);
  151.  
  152.     printf("\n--------------> DEALLOCATING STACK <--------------");
  153.     Deallocate(&MyStack);
  154.     PrintStack(MyStack);
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement