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;
}