Piszę programik, który ma wrzucać wiadomości do skrzynki według priorytetu.
Na razie wygląda to tak.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct wiadomosc{
char *txt;
int priorytet;
};
struct lista{
struct wiadomosc message;
struct lista * prev;
struct lista * next;
};
int wstaw(char*,int,int *,struct lista *); //wstawia wiadomosci w odpowiednie miejsce wedlug priorytetu
char* pobierz(); //zwraca wskazanie na tekst wiadomosci
int main()
{
bool wyjscie=false;
int inbox=0; //ile wiadomosci aktualnie w skrzynce
struct lista *pocz=NULL; //wskazanie na pierwsza wiadomosc w skrzynce
do
{char znak;
char * wiadomost;
printf("Co chcesz zrobic?\n");
printf("a) wstawic wiadomosc\n");
printf("b) przeczytac wiadomosc\n");
printf("c) oproznic skrzynke\n");
printf("d) wyjsc z poczty\n");
znak=getchar();
getchar();
switch(znak)
{case 'a':
{int prioryty;
fgets(wiadomost,100,stdin);
scanf("%d",&prioryty);
wstaw(wiadomost,prioryty,&inbox,pocz);}
case 'd': {wyjscie=true;}
}//switch
}//do_while
while(wyjscie==false);
getchar();
return 0;
}
int wstaw(char* msg,int prio,int *ile, struct lista* pocz)
{struct lista* akt=pocz;
struct lista* poprzednik=NULL;
if(*ile==100)
return 0;
(*ile)++;
struct lista *nowy=(struct lista *)malloc(sizeof(struct lista));
(nowy->message).txt=msg;
(nowy->message).priorytet=prio;
if(pocz==NULL)
{(pocz->message).txt=msg;
(pocz->message).priorytet=prio;
return 1;}
while(akt!=NULL&&prio<=(akt->message).priorytet)
{poprzednik=akt;
akt=(akt->next);}
(poprzednik->next)=nowy;
(nowy->prev)=poprzednik;
(nowy->next)=akt;
if(akt!=NULL)
(akt->prev)=nowy;
return 1;}
Niestety prawdopodobnie w momencie alokacji pamięci w funkcji wstaw program przestaje działać wyrzucając komunikat:
"Segmentation fault. Core dumped." Czy w tym miejscu jest błąd?