[C] Problem z kodem - dodawanie posortowane do listy dwukierunkowej, usuwanie itd.

0

Jestem początkującym programistą i muszę napisać program, który będzie dodawać w kolejności niemalejącej liczby oraz będzie mógł wykonać parę operacji na tej liście dwukierunkowej. Czy ktoś mógłby zerknąć na mój kod i pomóc mi z błędami?

#include <stdio.h>
#include <stdlib.h>

typedef struct _lista
{
    int x;
    struct _lista *next, *prev;
} lista;

void dodaj (lista *head, int dane)
{
    lista *nowy, *tmp;
    nowy = (lista*)malloc(sizeof(lista));
    nowy->x = dane;
    if ((*head) == NULL)
    {
        (*head) = nowy;
        (*head)->next = NULL;
        (*head)->prev = NULL;
    }
    else
    {
        lista *a, *b;
        a = (*head);
        b = NULL;
        while ( a && a->x < dane)
        {
            b = a;
            a = a->next;
        }
        nowy->next = a;
        nowy->prev = b;
        if (a =! NULL) a->prev = nowy;
        if (b =! NULL) b->next = nowy;
        if ((*head)->prev)(*head) = (*head)->prev;
    }
}

lista *tmp;
lista *b;
b=NULL;
lista *nowy;
nowy=(lista*)malloc(sizeof(lista));
tmp=(lista*)malloc(sizeof(lista));
nowy->next=NULL;
nowy->prev=NULL;
if ((*head)==NULL) (*head)=nowy;
else
{
    while (tmp->x < dane && tmp->next)
    {
        b=tmp;
        tmp=tmp->next;
    }

    nowy->next = tmp;
    nowy->prev = b;
    if (b) b->next = nowy;
    if (tmp) tmp->prev = nowy;
    nowy->x=dane;

}


};

void usun(lista **head, int dane)
{
    lista *tmp = (*head);
    while (tmp->next =! NULL)
    {
        if (tmp->x == dana)
        {
            if (tmp == (*head))
            {
                tmp->next = (*head);
                tmp->next->prev = NULL;
                free(tmp);
            }
            else if (tmp->next = NULL)
            {
                tmp->prev->next = NULL;
                free(tmp);
            }
            else
            {
                tmp->prev->next = tmp->next;
                tmp->next->prev = tmp->prev;
                free(tmp);
            }
        }
    }
}

void wypisz(lista *head)
{
    lista *tmp;
    tmp=head;
    while (tmp->next)
    {
        printf("%d", tmp->x);
        tmp = tmp->next;
    }
}

int main()
{
    lista *head;
    dodaj(&head, 1);
    dodaj(&head, 5);
    dodaj(&head, 4);
    dodaj(&head, 5);
    dodaj(&head, 3);
    wypisz(head);
    return 0;
}

0

pierwsze co mi się rzuciło to *head==NULL . Gwiazdka to operator wyłuskania wartości spod wcześniej zadeklarowanego wskaźnika więc jeśli chcesz sprawdzić czy adres pamięci jest NULL to head==NULL. Zaraz Ci może poprawię ten kod:)

Poza tym masz mnóstwo błędów typu }; lub kod jest globalnie kiedy powinien być wewnątrz funkcji. Poprawię Ci to żeby się kompilowało ale nad logiką to sam musisz pomyśleć:)
I pamiętaj, że != a nie =!

Poprawiony kompiluje się ale od razu wywala core dumped ... Dzisiaj już więcej nie zdołam pomóc. moze jutro albo środa

#include <stdio.h>
#include <stdlib.h>

typedef struct _lista
{
int x;
struct _lista *next, *prev;
} lista;

void dodaj (lista *head, int dane)
{
lista *nowy, tmp;
nowy = (lista
)malloc(sizeof(lista));
nowy->x = dane;
if (head == NULL)
{
head = nowy;
head->next = NULL;
head->prev = NULL;
}
else
{
lista *a, *b;
a = head;
b = NULL;
while ( a && a->x < dane)
{
b = a;
a = a->next;
}
nowy->next = a;
nowy->prev = b;
if (a != NULL) a->prev = nowy;
if (b != NULL) b->next = nowy;
if (head->prev) head = head->prev;
}

lista *b=NULL;

nowy->next=NULL;
nowy->prev=NULL;
if (head==NULL) head=nowy;
else
{
     while (tmp->x < dane && tmp->next)
     {
        b=tmp;
        tmp=tmp->next;
     }

nowy->next = tmp;
nowy->prev = b;
if (b) b->next = nowy;
if (tmp) tmp->prev = nowy;
nowy->x=dane;

}

}

void usun(lista **head, int dane)
{
lista *tmp = (*head);
while (tmp->next != NULL)
{
if (tmp->x == dane)
{
if (tmp == (*head))
{
tmp->next = (*head);
tmp->next->prev = NULL;
free(tmp);
}
else if (tmp->next = NULL)
{
tmp->prev->next = NULL;
free(tmp);
}
else
{
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
free(tmp);
}
}
}
}

void wypisz(lista *head)
{
lista *tmp;
tmp=head;
while (tmp->next)
{
printf("%d", tmp->x);
tmp = tmp->next;
}
}

int main()
{
lista *head;
dodaj(&*head, 1);
dodaj(&*head, 5);
dodaj(&*head, 4);
dodaj(&*head, 5);
dodaj(&*head, 3);
wypisz(head);
return 0;
}

0

Obejrzyj podcast gynvaela o pointerach/linked listach, ew. znajdz na tym forum bylo tego mnostwo.

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