Mam taki fragment kodu, i nie mogę odgadnąć dlaczego mi nie chce działać jak powinien:
#include <cstdio>
#include <malloc.h>
typedef int Elem;
enum type { ZBIOR, TORBA };
typedef struct lista slista;
typedef struct lista * plista;
struct lista{
Elem e;
plista next;
};
struct Kolekcja{
type typ;
plista l;
};
void torba(Kolekcja * k){
k->typ = TORBA;
k->l = NULL;
}
void zbior(Kolekcja * k){
k->typ = ZBIOR;
k->l = NULL;
}
void wstaw(Kolekcja * k, Elem e){//implementacja listowa
plista nowy = (plista) malloc( sizeof(slista) );
nowy->e = e;
nowy->next = NULL;
plista p = k->l;
if(p != NULL){
//nigdy tu nie wchodzi, tak jakby na k->l nie ustawiano nowego elementu :) Chwile nie robilem ze wskaznikami, wiec prosze o pomoc
printf(">>\n");//debug
while( p != NULL) p = p->next;
}
p = nowy;
printf("Dodalem: %d\n", p->e );//debug
printf("Nastepny: %d\n", p->next);//debug
printf("%d\n", k->l);
}
int szukaj(Kolekcja *k, Elem e){
plista p = k->l;
if(p != NULL){
if(k->typ == ZBIOR){
while(p->next != NULL){
if(p->e == e) return 1;
p = p->next;
}
return 0;
}
else if(k->typ == TORBA){
int ile = 0;
while(p->next != NULL){
printf("[%d]\n", p->e);
if(p->e == e) ile++;
p = p->next;
}
return ile;
}
else return -1;//ktos cos nabroil
}
else return 10;
}
int main(){
struct Kolekcja t;
torba(&t);
wstaw(&t, 1);
wstaw(&t, 2);
printf("%d", szukaj(&t, 1) );
while(1);
}