Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. void push(StackItem** pStack, char c )
  2. {
  3.     StackItem* p = (StackItem*)malloc(sizeof(StackItem)); // malloc musi się zgadzać z tym na co rzutuje
  4.     if (p)
  5.     {
  6.         memset(p, 0, sizeof(StackItem)); // mallock zawsze z memesetem zero (w sensie wypełniania zerami)
  7.         p->cKey  = c;
  8.         p->pNext = *pStack;
  9.         *pStack = p;
  10.     }
  11.     //else cout << "ERROR: stack overflow!!!\n\n";
  12. }
  13. //-------------------------
  14. char pop(StackItem** pStack)
  15. {
  16.   char c = 0;
  17.   if (c = top(*pStack)) // stos ZNAKOWY nie pusty gdy c != 0
  18.   {
  19.       del(pStack);
  20.   }
  21.   //else cout << "pop(): ERROR: stack underflow!!\n\n";
  22.  
  23.   return c;
  24. }
  25. //-------------------------
  26. char top(StackItem* pStack)
  27. {
  28.     if (!isEmpty(pStack))
  29.         return pStack->cKey;                    // - można używać, ale nie będziemy (*pStack).cKey;
  30.     /*else
  31.         cout << "ERROR: stack underflow!!\n\n";  */
  32.     return 0;                                   // dla stosu liczbowego wypisac komunikat o bledzie
  33. }
  34. //-------------------------
  35. void del (StackItem** pStack )
  36. {
  37.     if (!isEmpty(*pStack))
  38.     {
  39.         StackItem* p = *pStack;
  40.         *pStack = p->pNext;
  41.         free(p); // funkcja zwalniająca pamięć
  42.     }
  43.     else
  44.          cout << "del(): ERROR: stack underflow!!\n\n";
  45. }
  46. //-------------------------
  47. int  isEmpty(StackItem* pStack)
  48. {
  49.   return !pStack;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement