Witam, oto program, z operacjami na listach dwukierunkowych. wg. mmie wszystko jest poprawnie, ale program nie odwraca listy
Oto kod:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct element
{
int k;
struct element *prev;
struct element *next;
};
void lista_wyswietl( struct element *head);
struct element* lista_dodaj(struct element *head, struct element *nowy);
struct element* lista_szukaj(struct element *head, int sz);
struct element* lista_usun(struct element *head, struct element *x);
struct element* lista_odwroc(struct element *head);
int main()
{
struct element *head=NULL, *nowy=NULL, *temp=NULL;
int liczba;
char z;
while(1)
{
printf("\nd-dodaj\n");
printf("w-wyswietl\n");
printf("s-szukaj\n");
printf("q=wyjscie\n");
z=getchar();
switch(z)
{
case 'w': lista_wyswietl(head);
getch();
break;
case 'd': nowy=(struct element *)malloc(sizeof(struct element));
printf("Podaj wartosc do dodania \n");
scanf("%d", &liczba);
nowy->k=liczba;
head = lista_dodaj(head, nowy);
break;
case 's': printf("Podaj element do szukania \n");
scanf("%d", &liczba);
temp=lista_szukaj(head, liczba);
if(temp!=NULL)
printf("Znaleziono element\n");
else
printf("Nie znaleziono elementu\n");
getch();
case 'u': lista_wyswietl(head);
printf("Podaj element do usuniecia\n");
scanf("%d", &liczba);
temp=lista_szukaj(head, liczba);
if(temp!=NULL)
head=lista_usun(head, temp);
break;
case 'o': if(head!=NULL)
{
head = lista_odwroc(head);
printf("Lista odwrócona\n");
lista_wyswietl(head);
}
break;
case 'q':
return 0;
break;
}
system("cls");
}
}
void lista_wyswietl( struct element *head)
{
struct element *x=head;
while(x!=NULL)
{
printf("%d ", x->k);
x=x->next;
}
}
struct element* lista_dodaj(struct element *head, struct element *nowy)
{
nowy->next=head;
if(head!=NULL)
head->prev=nowy;
head=nowy;
nowy->prev=NULL;
}
struct element* lista_szukaj(struct element* head, int sz)
{
struct element *x=head;
while(x!=NULL &&x->k!=sz)
x=x->next;
return x;
}
struct element* lista_usun(struct element* head, struct element* x)
{
struct element *temp;
if(x->prev!=NULL)
x->prev->next=x->next;
else
head=x->next;
if(x->next!=NULL)
x->next->prev=x->prev;
return head;
}
struct element* lista_odwroc(struct element* head)
{
struct element *temp=NULL, *x=NULL, *y=NULL;
x=head;
while(x!=NULL)
y=(struct element*)malloc(sizeof(struct element));
y->k=x->k;
temp=lista_dodaj(temp, y);
x=x->next;
return temp;
}
Gdzie jest problem?
Są jeszcze jakieś błędy? czy program dobrze napisany?
MAcie jakieś fajne ćwiczenia z C z list dwukierunkowych?
Z góry dziękuję za pomoc.