Advertisement
peterzig

[AISD] Stos

Sep 19th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5. struct Stack {
  6.     int capacity;
  7.     int size;
  8.     int *data;
  9. };
  10.  
  11. Stack *CreateStack(unsigned int Capacity) {
  12.     if (Capacity > 0) {
  13.         Stack *s = (Stack*)malloc(sizeof(Stack));
  14.         s->capacity = Capacity;
  15.         s->size = 0;
  16.         s->data = new int[Capacity];
  17.         return s;
  18.     }
  19.     return 0;
  20. }
  21.  
  22. Stack *DestroyStack(Stack *s)
  23. {
  24.     if (s == 0) return 0;
  25.     free(s->data);
  26.     free(s);
  27.     return 0;
  28. }
  29.  
  30. bool IsEmpty(const Stack *s)
  31. {
  32.     if (s == 0) return false;
  33.     if (s->size == 0) return true;
  34.     return false;
  35. }
  36.  
  37. bool Push(Stack *s, int value)
  38. {
  39.     if (s->size < s->capacity) {
  40.         s->data[s->size] = value;
  41.         s->size++;
  42.         return true;
  43.     }
  44.     return false;
  45. }
  46.  
  47. int Pop(Stack *s)
  48. {
  49.     if (s->size > 0) {
  50.         return s->data[--s->size];
  51.     }
  52.     return 0;
  53. }
  54.  
  55. int main() {
  56.     Stack *s = CreateStack(10);
  57.     for (int i = 0; i < 10; ++i) Push(s, (i + 1));
  58.     while (!IsEmpty(s)) {
  59.         printf("%d\n", Pop(s));
  60.     }
  61.     s = DestroyStack(s);
  62.     _getch();
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement