Siemka. Do wieczora muszę wysłać profesorowi program ze stosu, jednak mam problem z napisaniem 2 funkcji ( clear i isEmpty). Siedzę już nad tym jakiś czas i chyba nie rozumiem.
#include <stdio.h>
#include <stdlib.h>
#define N 5
int push(int tab[], int i); //odklada element na wierzcholek stosu
void print(int tab[], int i); //wypisuje wszystkie elementy stosu
int pop(int tab[], int i); //zdejmuje element z wierzcholka stosu
int size(int tab[], int dlugosc); //zwraca aktualny rozmiar stosu
void clear(int tab[], int i); //czysci stos
bool isEmpty(int tab[], int *p); //zwraca prawde jesli stos jest pusty, w przecwinym razie falsz
int getElement(int tab[], int *p, int indeks); //zwraca wartosc elementu znajdujacego sie pod przekazanym indeksem
int main()
{
int tab[N];
int i=0;
int x;
int *p;
int dlugosc=0;
p = &tab[0];
int indeks=0;
do{
printf("Wybierz jedna z funkcji programu\n");
printf("1 - push\n");
printf("2 - pop\n");
printf("3 - size\n");
printf("4 - clear\n");
printf("5 - isEmpty\n");
printf("6 - print\n");
printf("7 - getElement\n");
printf("8 - Koniec programu\n");
printf("\n");
do{
scanf("%d", &x);
printf("\nWybrales opcje: %d\n", x);
printf("\n");
}while(x<0 || x>8);
switch(x)
{
case 1:
{
printf("\nWybrano funkcje push\n");
printf("i: %d\n", i);
if(i==N-1)
printf("Stos jest pelny\n");
else
if(i<N)
{
i = push(tab,i);
printf("Wartosc zostala polozona na stosie\n");
printf("\n");
if(i==N-1)
printf("Stos jest pelny\n");
else
i++;
}
break;
}
case 2:
{
printf("\nWybrano funkcje push\n");
if(i==N-1)
printf("i: %d\n", i);
else
i--;
pop(tab, i);
break;
}
case 3:
{
dlugosc = size(tab, dlugosc);
printf("Rozmiar stosu to: %d\n", dlugosc);
break;
}
case 4:
{
clear(tab, i);
printf("Tablica zostala wyczyszczona\n");
printf("\n");
break;
}
case 5:
{
i = isEmpty(tab, p);
printf("\n");
break;
}
case 6:
{
if(i==0)
printf("Stos jest pusty\n");
else
print(tab, i);
printf("\n");
break;
}
case 7:
{
printf("Podaj indeks argumentu ktory ma zwrocic funkcja\n");
scanf("%d", &indeks);
indeks = getElement(tab, p, indeks);
printf("%d", indeks);
break;
}
}
}while(x!=8);
return 0;
}
int push(int tab[], int i)
{
printf("Podaj wartosc %d: ", i);
scanf("%d", &tab[i]);
return i;
}
int pop(int tab[], int i)
{
printf("Ostatnia wartosc na stosie to: %d\n", tab[i]);
i--;
printf("Obecnie na stosie ostatnia liczba jest: %d\n", tab[i]);
printf("\n");
return i;
}
void print(int tab[], int i)
{
int j;
for(j=0;j<i;j++)
printf("%d %d\n", j, tab[j]);
}
int size(int tab[], int dlugosc)
{
dlugosc = sizeof(tab)/sizeof(tab[0]);
return dlugosc;
}
void clear(int tab[], int i)
{
for(i=0;i<N;i++)
free(tab);
}
bool isEmpty(int tab[], int *p)
{
if(p==NULL)
{
printf("Stos jest pusty\n");
return 1;
}
else
{
printf("Na stosie znajduja sie elementy\n");
return 0;
}
}
int getElement(int tab[], int *p, int indeks)
{
}
A i funkcja size zwraca mi cały czas 2. Proszę o pomoc w doprowadzeniu tego kodu do ładu. Z góry dziękuję.