Lista jednokierunkowa problem z dodawaniem elementu

0

https://zapodaj.net/6505b51034531.png.html
Mam takie polecenie jak na obrazku

#include <stdio.h>
typedef struct Element{
    int liczba;
    struct Element *next;
}Element;

typedef struct Handler{
    Element *beg, *end;
}Handler;
beg=NULL;
end=NULL;
void push_back(struct Handler *h, int value)
{
    Element *nowa=(Element*)malloc(sizeof(Element));
    if(beg==NULL)
    {
        beg=nowa;
        end=nowa;
    }
    else
    {
        Element *temp=beg;
        while(temp->next)
        {
            temp=temp->next;
        }
        temp->next=nowa;
        nowa->next=NULL;
    }
}
int main()
{
Handler *new=(Handler*)malloc(sizeof(Handler));
push_back(new,4);
push_back(new,6);

return 0;
}

Jednak coś chyba nie działa jak należy. Ktoś mógłby mi dać jakieś wskazówki co źle robię :) Z góry dziękuję
Bo tu mam narzuconą tą strukturę Handler i nie wiem w sumie po co mi ona jest. Jak się do niej odwoływać.

0
beg=NULL;
end=NULL;

Co to niby ma robić? beg i end istnieją tylko w ramach konkretnej instancji struktury. Inaczej mówiąc zamiast

if(beg==NULL)

powinno być

if (h->beg == NULL)

albo krócej

if (!h->beg)



Handler *new=(Handler*)malloc(sizeof(Handler));
push_back(new,4);

Stworzyłeś nowy Handler, ale nie inicjalizowałeś beg i end na NULL (mają niezainicjalizowaną wartość). Ten kod, który napisałeś do tego celu jest niepoporawny (patrz wyżej).

0
#include <stdio.h>
#include <stdlib.h>
typedef struct Element{
    int liczba;
    struct Element *next;
}Element;

typedef struct Handler{
    Element *beg, *end;
}Handler;

void push_back(struct Handler *h, int value)
{
    Element *nowa=(Element*)malloc(sizeof(Element));
    nowa->liczba=value;
    if(h->beg==NULL)
    {
        printf("beg=null");
        h->beg=nowa;
        h->end=nowa;
    }
    else
    {
        printf("beg!=null");
        Element *temp=h->beg;
        while(temp->next)
        {
            temp=temp->next;
        }
        temp->next=nowa;
        h->end=nowa;
        nowa->next=NULL;
    }
}
int main()
{
Handler *new=(Handler*)malloc(sizeof(Handler));
new->beg=NULL;
new->end=NULL;
push_back(new,4);
push_back(new,6);
printf("%d",new->beg->liczba);

return 0;
}

Poprawiłem na coś takiego jednak nadal coś nie działa :(

0

A gdzie zapisujesz tę wartość liczbową, którą dodajesz do węzła?
I następnym razem bądź trochę konkretniejszy niż "nie działa".

0

Zmieniłem że dodaje tę wartość

0

Bo jak tworzysz nowy węzeł, to nie ustawiasz next na NULL.

0
#include <stdio.h>
#include <stdlib.h>
typedef struct Element{
    int liczba;
    struct Element *next;
}Element;

typedef struct Handler{
    Element *beg, *end;
}Handler;

void push_back(struct Handler *h, int value)
{
    Element *nowa=(Element*)malloc(sizeof(Element));
    nowa->next=NULL;
    nowa->liczba=value;
    if(h->beg==NULL)
    {
        h->beg=nowa;
        h->end=nowa;
    }
    else
    {
        Element *temp=h->beg;
        while(temp->next)
        {
            temp=temp->next;
        }
        temp->next=nowa;
        h->end=nowa;
        nowa->next=NULL;
    }
}
int main()
{
Handler *new=(Handler*)malloc(sizeof(Handler));
new->beg=NULL;
new->end=NULL;
push_back(new,4);
push_back(new,6);
printf("%d",new->beg->liczba);
printf("%d",new->beg->next->liczba);
return 0;
}

Dobra teraz wszystko działa, dzięki :)
Przy inicjalizowaniu nowego elemetnu wskaźnik na nastepny nie był ustawiany.

1 użytkowników online, w tym zalogowanych: 0, gości: 1