#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define W 100
/*
Zaimplementuj stos używając tablicy. Funkcje muszą przyjmować wskaźnik do tablicy przechowującej elementy stosu.
Jak można zaimplementować stos tak żeby funkcje top, pop, push nie potrzebowały przy każdym wywołaniu znać długości tablicy elementów?
*/
int top(int *stos);
int pop(int *stos);
void push(int *stos, int elem);
int main(int argc, char *argv[]) {
int stos[W]; //maksymalna możliwa wielkość kopca
stos[0] = 0; //obecna wielkośc kopca (wartownik)
int operacja;
int elem;
while(1) {
printf("Jaką operację chcesz wykonać?\n 1 - podam Ci element, który jest na szczycie stosu.\n 2 - usunę element ze szczytu stosu.\n 3 - dodam podany przez Ciebie element na szczyt stosu. 0 -koniec\nCo więc wybierasz?\n\nWykonaj: ");
scanf("%d", &operacja);
if(operacja==1) {
if(stos[0]==0) printf("\nStos jest pusty!");
else printf("\nElement na szcycie stosu to: %d",top(stos));
}
else if(operacja==2) {
if(stos[0]==0) printf("\nStos jest pusty, nie można usunąć żadnego elementu!");
else printf("\nElement usunięto, na stosie pozostało %d elementów", pop(stos));
}
else if(operacja==3) {
printf("\n\nJaki element chcesz dodaj do stosu?\nDodaj: ");
scanf("%d", &elem);
if(stos[0]<W) push(stos,elem);
else printf("\nStos jest przepełniony!");
}
else if(operacja==0)
break;
else printf("\n\nNie ma takiej operacji!");
}
return 0;
}
int top(int *stos) {
int i=stos[0];
return stos[i];
}
int pop(int *stos) {
int i=stos[0];
stos[0]=i-1;
return i-1;
}
void push(int *stos, int elem) {
int i=stos[0];
stos[i+1]=elem;
stos[0]=i+1;
}
nie rozumiem jak dziala to int i=stos[0];
jakim cudem to w funkcji np. top sprawia ze dziala ona poprawnie?