Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DStack.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <malloc.h>
- typedef struct
- {
- int value;
- float coefficient;
- char* token;
- }Node;
- typedef struct DStackTAG
- {
- DStackTAG *Next;
- Node _Node;
- }DStack;
- void Initial(DStack **DS);
- bool Push(DStack **DS, int value, float coefficient, char* token);
- bool Pop(DStack **DS, int *value, float *coefficient, char** token);
- void PrintStack(DStack *DS);
- void Deallocate(DStack **DS);
- void Initial(DStack **DS)
- { *DS = NULL; }
- bool Push(DStack **DS, int value, float coefficient, char* token)
- {
- DStack *NewItem;
- NewItem = (DStack *)malloc(sizeof(DStack));
- if (NewItem == NULL)
- return false;
- else
- {
- // Values
- NewItem->_Node.value = value;
- NewItem->_Node.coefficient = coefficient;
- NewItem->_Node.token = token;
- // Link
- NewItem->Next = *DS;
- *DS = NewItem;
- }
- }
- bool Pop(DStack **DS, int* value, float* coefficient, char** token)
- {
- if (*DS == NULL)
- return false;
- DStack *Temp = *DS;
- // Export values
- *value = (*DS)->_Node.value;
- *coefficient = (*DS)->_Node.coefficient;
- *token = (*DS)->_Node.token;
- *DS = (*DS)->Next;
- // Free memory
- free(Temp);
- return true;
- }
- void PrintStack(DStack *DS)
- {
- DStack *Temp = DS;
- printf("\nDynamic Stack\n");
- printf("=================================");
- printf("\n");
- // Values
- Temp = DS;
- printf("(values): ");
- while(Temp != NULL)
- {
- printf(" %d \t", Temp->_Node.value);
- Temp = Temp->Next;
- }
- printf("\n");
- // Coefficient
- Temp = DS;
- printf("(coefficient): ");
- while(Temp != NULL)
- {
- printf(" %3.3f \t", Temp->_Node.coefficient);
- Temp = Temp->Next;
- }
- printf("\n");
- // Token
- Temp = DS;
- printf("(token): ");
- while(Temp != NULL)
- {
- printf(" %s\t", Temp->_Node.token);
- Temp = Temp->Next;
- }
- printf("\n");
- }
- void Deallocate(DStack **DS)
- {
- DStack *Temp = *DS;
- while(*DS != NULL)
- {
- *DS = (*DS)->Next;
- free(Temp);
- Temp = *DS;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- DStack *MyStack;
- int value;
- float coefficient;
- char* token;
- Initial(&MyStack);
- Push(&MyStack, 10, 3.12, "Italy");
- Push(&MyStack, 20, 7.14, "Greece");
- Push(&MyStack, 30, 2.21, "Obama");
- Push(&MyStack, 40, 9.39, "Athens");
- Push(&MyStack, 50, 5.21, "Insomnia");
- Push(&MyStack, 60, 2.39, "Athens");
- PrintStack(MyStack);
- printf("\nRemoving from stack...\n");
- Pop(&MyStack, &value, &coefficient, &token);
- printf("value: %d - coefficient: %3.3f - token: %s \n", value, coefficient, token);
- Pop(&MyStack, &value, &coefficient, &token);
- printf("value: %d - coefficient: %3.3f - token: %s \n", value, coefficient, token);
- printf("\nRe-Print Stack");
- PrintStack(MyStack);
- printf("\n--------------> DEALLOCATING STACK <--------------");
- Deallocate(&MyStack);
- PrintStack(MyStack);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement