Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include "Stack.h"
  2. #include "dStack.h"
  3. #include <iostream>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. void Push( StackItem** pHead, char c )
  9. {
  10. StackItem* v = (StackItem*)malloc( sizeof(StackItem) );
  11.  
  12. if( v ) // v != NULL
  13. {
  14. memset( v, 0, sizeof(StackItem) );
  15. v->cKey = c;
  16. v->pNext = *pHead;
  17. *pHead = v;
  18. }
  19. }
  20.  
  21. char Pop( StackItem** pHead )
  22. {
  23. char c = Top( *pHead );
  24.  
  25. if( !IsEmpty( *pHead ) ) //jesli stos znakowy nie jest pusty
  26. {
  27. Del( pHead );
  28. }
  29. else
  30. {
  31. cout << "ERROR!" << endl;
  32. }
  33.  
  34. return c;
  35. }
  36.  
  37. char Top( StackItem* pHead )
  38. {
  39. if( !IsEmpty(pHead) )
  40. {
  41. return pHead->cKey;
  42. }
  43.  
  44. return 0;
  45. }
  46.  
  47. void Del( StackItem** pHead )
  48. {
  49. if( !IsEmpty( *pHead ) )
  50. {
  51. StackItem* v = *pHead;
  52. *pHead = v->pNext;
  53.  
  54. free( v );
  55. }
  56. else
  57. {
  58. cout << "ERROR!" << endl;
  59. }
  60. }
  61.  
  62. int IsEmpty( StackItem* pHead )
  63. {
  64. return !pHead;
  65. }
  66.  
  67. void InitStack( StackItem** pHead )
  68. {
  69. *pHead = NULL;
  70. }
  71.  
  72. void FreeStack( StackItem** pHead )
  73. {
  74. while( !IsEmpty( *pHead ))
  75. {
  76. Del( pHead );
  77. }
  78.  
  79. cout << "Stack is Empty!" << endl;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement