Sta je lose u ovom Insertu greska sljedeca:
warning C4033: 'insert' must return a value
warning C4172: returning address of local variable or temporary
warning C4715: 'insert' : not all control paths return a value
uradio sam return p u insert funkciji;
ali mozda kod pozivanja insert nesto ne valja jer ne snimam nigdje ali kad stavim w= insert (5,w) onda mi pokrene baci rezultat 5 i zakuca se program...
Code:
#include <stdio.h>
#include <math.h>
#include <malloc.h>
typedef struct TNode
{
int data;
struct TNode * pleft;
struct TNode * pright;
}
TNode;
void* insert(int x, TNode* p);
int min(TNode * p);
int max(TNode * p);
double avg(TNode * p);
int sum(TNode * p);
int count(TNode * p);
int main(int argc, char * argv[])
{
TNode* w = NULL;
insert(5, w); // w = insert(5, w); pokusao sam ovo ali ne radi
insert(3, w);
insert(7, w);
insert(4, w);
insert(9, w);
insert(1, w);
insert(6, w);
insert(2, w);
printf("min: %d\n", min(w));
printf("max: %d\n", max(w));
printf("sum: %d\n", sum(w));
printf("cnt: %d\n", count(w));
printf("avg: %f\n", avg(w));
return 0;
}
int min(TNode * p)
{
if (p->pleft == NULL) return p->data;
return min(p->pleft);
}
int max(TNode * p)
{
if (p->pright == NULL) return p->data;
return max(p->pright);
}
double avg(TNode * p)
{
return sum(p) / (double)count(p);
}
int sum(TNode * p)
{
if (p == NULL) return 0;
return p->data + sum(p->pleft) + sum(p->pright);
}
int count(TNode * p)
{
if (p == NULL) return 0;
return 1 + count(p->pleft) + count(p->pright);
}
void* insert(int x, TNode* p)
{
if (p == NULL)
{ // freier Platz
TNode * neu = (TNode*)malloc(sizeof(TNode));
if (neu == NULL) return;
neu->data = x;
neu->pleft = neu->pright = NULL;
p = neu;
}
else
{ // besetzt
if (x < p->data)
insert(x, p->pleft);
else
insert(x, p->pright);
}
return p ;
}
#include <stdio.h>
#include <math.h>
#include <malloc.h>
typedef struct TNode
{
int data;
struct TNode * pleft;
struct TNode * pright;
}
TNode;
void* insert(int x, TNode* p);
int min(TNode * p);
int max(TNode * p);
double avg(TNode * p);
int sum(TNode * p);
int count(TNode * p);
int main(int argc, char * argv[])
{
TNode* w = NULL;
insert(5, w); // w = insert(5, w); pokusao sam ovo ali ne radi
insert(3, w);
insert(7, w);
insert(4, w);
insert(9, w);
insert(1, w);
insert(6, w);
insert(2, w);
printf("min: %d\n", min(w));
printf("max: %d\n", max(w));
printf("sum: %d\n", sum(w));
printf("cnt: %d\n", count(w));
printf("avg: %f\n", avg(w));
return 0;
}
int min(TNode * p)
{
if (p->pleft == NULL) return p->data;
return min(p->pleft);
}
int max(TNode * p)
{
if (p->pright == NULL) return p->data;
return max(p->pright);
}
double avg(TNode * p)
{
return sum(p) / (double)count(p);
}
int sum(TNode * p)
{
if (p == NULL) return 0;
return p->data + sum(p->pleft) + sum(p->pright);
}
int count(TNode * p)
{
if (p == NULL) return 0;
return 1 + count(p->pleft) + count(p->pright);
}
void* insert(int x, TNode* p)
{
if (p == NULL)
{ // freier Platz
TNode * neu = (TNode*)malloc(sizeof(TNode));
if (neu == NULL) return;
neu->data = x;
neu->pleft = neu->pright = NULL;
p = neu;
}
else
{ // besetzt
if (x < p->data)
insert(x, p->pleft);
else
insert(x, p->pright);
}
return p ;
}