Can anybody help me with this? I am writing a program for my assignment in college. We are working with lists. I have a problem with pointer initialization. The pointer is pointing at the first element of the list. I have a structure
Code:
typedef struct cvor
{
int broj;
struct cvor *sledeci;
}Tcvor;
and a pointer type to the structure declared like this
Code:
typedef Tcvor *Pcvor;
This is the declaration of the pointer to the first element of the list.
I encounter a problem with printing and deleting a number from the list (and I can't know if the numbers are in the list at all). When I choose the option to run the selected function windows prints a message that the program has encountered an problem and needs to be shut down. After some debugging I realised that the pointer " *glava " was uninitialised even though the function for initialization did run.
Code:
void Inicijalizacija(Pcvor *glava)
{
*glava=NULL;
}
My friend wrote a program with same declarations and initialization function and it works. He worked in Ubuntu.
The enviroment that I am working in is Code Blocks 8.02 for windows.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct cvor
{
int broj;
struct cvor *sledeci;
}Tcvor;
typedef Tcvor *Pcvor;
void Inicijalizacija(Pcvor *glava);
void Osnovnimeni(char *izbor);
void Unos(Pcvor *glava);
void Brisanje(Pcvor *glava);
void Listanje(Pcvor *glava);
void Oslobodimemoriju(Pcvor *glava);
main()
{
Pcvor glava;
char izbor;
Inicijalizacija(&glava);
do
{
Osnovnimeni(&izbor);
switch(izbor)
{
case '1': Unos(&glava);
break;
case '2': Brisanje(&izbor);
break;
case '3': Listanje(&izbor);
break;
case '4': ;
}
}
while(izbor!='4');
Oslobodimemoriju(&glava);
}
void Inicijalizacija(Pcvor *glava)
{
*glava=NULL;
}
void Osnovnimeni(char *izbor)
{
char znak;
printf("\n\n\t1) Dodaj\n\t2) Brisi\n\t3) Listaj\n\t4) Kraj?");
do
{
printf("\n\t");
*izbor = getchar();
}while((*izbor > '4') || (*izbor < '1'));
}
void Unos(Pcvor *glava)
{
Pcvor tek,pret,novi;
int br;
novi = (Tcvor *)malloc(sizeof(Tcvor));
printf("\n\n\tUnesite broj: ");
scanf("%d", &br);
novi->broj=br;
novi->sledeci=NULL;
if(*glava == NULL)
{
*glava = novi;
return;
}
tek=*glava;
pret=*glava;
while(tek != NULL)
{
pret = tek;
tek = tek->sledeci;
}
pret->sledeci=novi;
}
void Brisanje(Pcvor *glava)
{
Pcvor tek,pret;
int br;
printf("\n\n\tUnesite broj koji zelite da obrisete: ");
scanf("%d", &br);
if(*glava == NULL)
{
printf("\n\n\tLista je prazna!!!");
return;
}
tek=*glava;
pret=*glava;
while(tek!=NULL && (tek->broj!=br))
{
pret=tek;
tek=tek->sledeci;
}
if(tek == NULL)
printf("\n\n\tZnaka nema u listi!!");
else
{
if(tek==*glava)
{
*glava=tek->sledeci;
tek->sledeci=NULL;
}
else
{
pret->sledeci=tek->sledeci;
tek->sledeci=NULL;
}
free(tek);
}
}
void Listanje(Pcvor *glava)
{
Pcvor tek;
if(*glava == NULL)
{
printf("\n\n\tLista je prazna!!!");
return;
}
printf("\n\n\tPrikaz liste:");
tek = *glava;
while(tek != NULL)
{
printf("%d\n",tek->broj);
tek=tek->sledeci;
}
}
void Oslobodimemoriju(Pcvor *glava)
{
Pcvor tek;
while(*glava != NULL)
{
tek=*glava;
*glava=tek->sledeci;
free(tek);
}
}
Here's the entire code. It maybe has some errors but I can'c correct them without correcting the pointer problem.