#include #include #include #include #include struct wezel { char imie[20]; int wiek; struct wezel *nastepny; struct wezel *poprzedni; }; struct wezel *dodaj(struct wezel *ogon ) { struct wezel *nowy; nowy = (struct wezel*)malloc(sizeof(struct wezel)); ogon->nastepny = nowy; nowy->poprzedni=ogon; nowy->nastepny = NULL; return(nowy); } struct wezel *usun( struct wezel *glowa ) { struct wezel *temp; struct wezel *przed_ogon; temp = glowa; if(temp->nastepny == NULL) { free(temp); return NULL; } while( temp->nastepny->nastepny !=NULL ) { temp = temp->nastepny; } przed_ogon = temp; free(przed_ogon->nastepny); przed_ogon->nastepny=NULL; return przed_ogon; } void wyswietl(struct wezel *glowa) { struct wezel *temp; temp = glowa; while(temp!=NULL) { printf("%s %d\n",temp->imie,temp->wiek); temp=temp->nastepny; } } void wyswietl_aktualny(struct wezel *aktualny) { printf("Aktualny element:\n\n"); printf("%s %d\n\n",aktualny->imie,aktualny->wiek); } int main(int argc, char *argv[]) { char k; struct wezel *glowa; struct wezel *ogon; struct wezel *aktualny; glowa = (struct wezel*)malloc(sizeof(struct wezel)); glowa->nastepny=NULL; glowa->poprzedni=NULL; printf("Podaj imie i wiek pierwszej osoby:\n"); scanf("%s%d",&glowa->imie,&glowa->wiek); ogon=glowa; printf("Czy dodac nastepny element: y/n\n"); k=getch(); while(k=='y') { ogon = dodaj(ogon); printf("Podaj imie i wiek nastepnej osoby:\n"); scanf("%s%d",&ogon->imie,&ogon->wiek); printf("Czy dodac nastepny element: y/n\n"); k=getch(); } printf("\nElementy listy wypisane od poczatku do konca:\n"); wyswietl(glowa); printf("\nprzegladanie listy po jednym elemencie:\n\n"); aktualny = glowa; wyswietl_aktualny(aktualny); k=NULL; do { if(k=='n') { if(aktualny->nastepny==NULL) { printf("Brak nastepnego elementu\n\n"); wyswietl_aktualny(aktualny); } else { aktualny = aktualny->nastepny; wyswietl_aktualny(aktualny); } } if(k=='p') { if(aktualny->poprzedni==NULL) { printf("Brak nastepnego elementu\n\n"); wyswietl_aktualny(aktualny); } else { aktualny = aktualny->poprzedni; wyswietl_aktualny(aktualny); } } printf("Wybierz co dalej: \n n-nastepny element \n p-poprzedni element \n u-usun biezacy element \n d-dodaj nowy za tym elementem \n z-zakoncz\n\n"); k=getch(); } while(k!='z'); system("Pause"); return 0; }