Hyluss icon

Stos

Hyluss | PRO | 07/02/16 07:00:39 PM UTC | 0 ⭐ | 489 👁️ | Never ⏰ | []
C |

2.26 KB

|

None

|

0 👍

/

0 👎

//by Pyjter&Pasierbix
 
#include "stdafx.h"
#include <conio.h>
#include <Windows.h>
struct Stack {
    int capacity;
    int size;
    int *data;
};
 
Stack *CreateStack(unsigned int Capacity) {
    if (Capacity > 0) {
        Stack *s = new Stack;
        s->capacity = Capacity;
        s->size = 0;
        s->data = new int[Capacity];
        return s;
    }
    return 0;
}
 
Stack *DestroyStack(Stack *s) {
    if (s==0) return 0;
    delete[]s->data;
    delete s;
    return 0;
}
 
bool IsEmpty(const Stack *s) {
    if (s==0) return false;
    if(s->size==0) return true;
    return false;
}
 
bool Push(Stack *s, int value) {
    if (s->size < s->capacity) {
        s->data[s->size] = value;
        s->size++;
        return true;
    }
    return false;
}
 
int Pop(Stack *s) {
    if (s->size > 0) {
        return s->data[--s->size];
    }
    return 0;
}
 
int main() {
    int wielkosc, wartosc,wybor;
    printf("Podaj wielkosc stosu: \n");
    scanf("%d",&wielkosc);
    Stack *s = CreateStack(wielkosc);
    printf("Wielkosc stosu to : %d\n",wielkosc);
    do{
system ("cls");
    printf("Co bys ciciol zrobic?\n");
    printf("1.Dodej na stos\n");
    printf("2.Sciagnij ze stosu\n");
    printf("3.Wyswietl stos\n");
    printf("4.Czy stos je pusty?\n");
    printf("5.Zdupiom !\n");
    scanf("%d",&wybor);
   
    switch(wybor)
    {
    case 1:
        if (s->size >= s->capacity)
            {
                printf("Tys je gupi! Juz placu na stosie ni ma!");getch();break;
            }
        else{   printf("Podaj co chcesz dodac na stos: ");
            scanf("%d",&wartosc);
            Push(s, wartosc);
            break;}
    case 2:
            printf("%d\n", Pop(s));
            getch();
            break;
    case 3:
            for(int i=s->size;i>0;i--)
            {
            printf("%d\n", s->data[i-1]);
            }
            getch();
            break;
    case 4:
        if(IsEmpty(s))printf("Stos jest pusty!");
        else printf("Stos nie jest pusty!");
        getch();
        break;
    case 5:
        s = DestroyStack(s);
        return 0;
    
    case 6:
        exit (1);
 }
    }while(true);
   
 
    getch();
 
    return 0;
}
//by Pyjter&Pasierbix

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎