Hyluss

Stos

Jul 2nd, 2016
555
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. //by Pyjter&Pasierbix
  2.  
  3. #include "stdafx.h"
  4. #include <conio.h>
  5. #include <Windows.h>
  6. struct Stack {
  7.     int capacity;
  8.     int size;
  9.     int *data;
  10. };
  11.  
  12. Stack *CreateStack(unsigned int Capacity) {
  13.     if (Capacity > 0) {
  14.         Stack *s = new Stack;
  15.         s->capacity = Capacity;
  16.         s->size = 0;
  17.         s->data = new int[Capacity];
  18.         return s;
  19.     }
  20.     return 0;
  21. }
  22.  
  23. Stack *DestroyStack(Stack *s) {
  24.     if (s==0) return 0;
  25.     delete[]s->data;
  26.     delete s;
  27.     return 0;
  28. }
  29.  
  30. bool IsEmpty(const Stack *s) {
  31.     if (s==0) return false;
  32.     if(s->size==0) return true;
  33.     return false;
  34. }
  35.  
  36. bool Push(Stack *s, int value) {
  37.     if (s->size < s->capacity) {
  38.         s->data[s->size] = value;
  39.         s->size++;
  40.         return true;
  41.     }
  42.     return false;
  43. }
  44.  
  45. int Pop(Stack *s) {
  46.     if (s->size > 0) {
  47.         return s->data[--s->size];
  48.     }
  49.     return 0;
  50. }
  51.  
  52. int main() {
  53.     int wielkosc, wartosc,wybor;
  54.     printf("Podaj wielkosc stosu: \n");
  55.     scanf("%d",&wielkosc);
  56.     Stack *s = CreateStack(wielkosc);
  57.     printf("Wielkosc stosu to : %d\n",wielkosc);
  58.     do{
  59. system ("cls");
  60.     printf("Co bys ciciol zrobic?\n");
  61.     printf("1.Dodej na stos\n");
  62.     printf("2.Sciagnij ze stosu\n");
  63.     printf("3.Wyswietl stos\n");
  64.     printf("4.Czy stos je pusty?\n");
  65.     printf("5.Zdupiom !\n");
  66.     scanf("%d",&wybor);
  67.    
  68.     switch(wybor)
  69.     {
  70.     case 1:
  71.         if (s->size >= s->capacity)
  72.             {
  73.                 printf("Tys je gupi! Juz placu na stosie ni ma!");getch();break;
  74.             }
  75.         else{   printf("Podaj co chcesz dodac na stos: ");
  76.             scanf("%d",&wartosc);
  77.             Push(s, wartosc);
  78.             break;}
  79.     case 2:
  80.             printf("%d\n", Pop(s));
  81.             getch();
  82.             break;
  83.     case 3:
  84.             for(int i=s->size;i>0;i--)
  85.             {
  86.             printf("%d\n", s->data[i-1]);
  87.             }
  88.             getch();
  89.             break;
  90.     case 4:
  91.         if(IsEmpty(s))printf("Stos jest pusty!");
  92.         else printf("Stos nie jest pusty!");
  93.         getch();
  94.         break;
  95.     case 5:
  96.         s = DestroyStack(s);
  97.         return 0;
  98.    
  99.     case 6:
  100.         exit (1);
  101.  }
  102.     }while(true);
  103.    
  104.  
  105.     getch();
  106.  
  107.     return 0;
  108. }
  109. //by Pyjter&Pasierbix
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment