Facebook
From dd, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 153
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <stddef.h>
  6.  
  7. struct wezel
  8. {
  9.         char imie[20];
  10.         int wiek;
  11.         struct wezel *nastepny;
  12.         struct wezel *poprzedni;
  13.  
  14. };
  15.  
  16. struct wezel *dodaj(struct wezel *ogon )
  17. {
  18.   struct wezel *nowy;
  19.   nowy = (struct wezel*)malloc(sizeof(struct wezel));
  20.   ogon->nastepny = nowy;
  21.  nowy->poprzedni=ogon;
  22.   nowy->nastepny = NULL;
  23.  
  24.   return(nowy);
  25.  
  26. }
  27.  
  28.  
  29.  
  30. struct wezel *usun( struct wezel *glowa )
  31. {
  32.         struct wezel *temp;
  33.         struct wezel *przed_ogon;
  34.         temp = glowa;
  35.         if(temp->nastepny == NULL)
  36.         {
  37.                 free(temp);
  38.                 return NULL;
  39.         }
  40.        
  41.         while( temp->nastepny->nastepny !=NULL )
  42.         {
  43.                 temp = temp->nastepny;
  44.     }
  45.         przed_ogon = temp;
  46.         free(przed_ogon->nastepny);
  47.         przed_ogon->nastepny=NULL;
  48.         return przed_ogon;
  49. }
  50. void wyswietl(struct wezel *glowa)
  51. {
  52.         struct wezel *temp;
  53.         temp = glowa;
  54.         while(temp!=NULL)
  55.         {
  56.                 printf("%s %d\n",temp->imie,temp->wiek);
  57.                 temp=temp->nastepny;
  58.         }
  59. }
  60.  
  61. void wyswietl_aktualny(struct wezel *aktualny)
  62. {
  63.         printf("Aktualny element:\n\n");
  64.         printf("%s %d\n\n",aktualny->imie,aktualny->wiek);
  65. }
  66.  
  67. int main(int argc, char *argv[])
  68. {
  69. char k;
  70. struct wezel *glowa;
  71. struct wezel *ogon;
  72. struct wezel *aktualny;
  73. glowa = (struct wezel*)malloc(sizeof(struct wezel));
  74. glowa->nastepny=NULL;
  75. glowa->poprzedni=NULL;
  76. printf("Podaj imie i wiek pierwszej osoby:\n");
  77. scanf("%s%d",&glowa->imie,&glowa->wiek);
  78.  
  79. ogon=glowa;
  80.  
  81.  
  82. printf("Czy dodac nastepny element: y/n\n");
  83. k=getch();
  84.  
  85. while(k=='y')
  86. {
  87.    ogon = dodaj(ogon);
  88.    printf("Podaj imie i wiek nastepnej osoby:\n");
  89.    scanf("%s%d",&ogon->imie,&ogon->wiek);
  90.    
  91.    printf("Czy dodac nastepny element: y/n\n");
  92.    k=getch();
  93. }
  94.  
  95.  
  96. printf("\nElementy listy wypisane od poczatku do konca:\n");
  97. wyswietl(glowa);
  98.  
  99. printf("\nprzegladanie listy po jednym elemencie:\n\n");
  100.  
  101.  aktualny = glowa;
  102.  wyswietl_aktualny(aktualny);
  103.  k=NULL;
  104. do
  105. {
  106.    if(k=='n')
  107.    {
  108.                 if(aktualny->nastepny==NULL)
  109.                 {
  110.                         printf("Brak nastepnego elementu\n\n");
  111.                         wyswietl_aktualny(aktualny);
  112.                 }
  113.                 else
  114.                 {
  115.                         aktualny = aktualny->nastepny;
  116.                         wyswietl_aktualny(aktualny);
  117.                 }      
  118.    }
  119.    
  120.     if(k=='p')
  121.    {
  122.                 if(aktualny->poprzedni==NULL)
  123.                 {
  124.                         printf("Brak nastepnego elementu\n\n");
  125.                         wyswietl_aktualny(aktualny);
  126.                 }
  127.                 else
  128.                 {
  129.                         aktualny = aktualny->poprzedni;
  130.                         wyswietl_aktualny(aktualny);
  131.                 }      
  132.    }
  133.    
  134.    
  135.    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");
  136.    k=getch();
  137. }
  138. while(k!='z');
  139.  
  140. system("Pause");
  141. return 0;
  142. }
  143.  
  144.