daily pastebin goal
20%
SHARE
TWEET

Lista dwukierunkowa - pełna

GoralWMoro Dec 11th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Bron Bron;
  4. struct Bron{
  5.     int numer_broni;
  6.     Bron *prev;
  7.     Bron *next;
  8. };
  9.  
  10. Bron* dodaj_pierwszy(Bron *head, Bron* nowy){
  11.  
  12.     if(head == NULL){
  13.         nowy->next = NULL;
  14.         nowy->prev = NULL;
  15.         head = nowy;
  16.     }else{
  17.         nowy->prev = NULL;
  18.         nowy->next = head;
  19.         head->prev = nowy;
  20.         head = nowy;
  21.     }
  22.     return head;
  23.  
  24. }
  25.  
  26. Bron* dodaj_ostatni(Bron *head, Bron *nowy){
  27.    Bron *first = head;
  28.     while(first->next != NULL){
  29.         first = first->next;
  30.     }
  31.  
  32.     first->next = nowy;
  33.     nowy->prev = first;
  34.     nowy->next = NULL;
  35.  
  36.     return head;
  37.  
  38. }
  39.  
  40. Bron* dodaj_na_N(Bron *head, Bron *nowy, int poz){
  41.     Bron *first = head;
  42.     int i = 2;
  43.     while(first->next != NULL){
  44.         if(i == poz){
  45.             nowy->next = first->next;
  46.             first->next->prev = nowy;
  47.             nowy->prev = first;
  48.             first->next = nowy;
  49.         }
  50.         i++;
  51.         first = first->next;
  52.  
  53.     }
  54.     if(poz >= i){
  55.         head = dodaj_ostatni(head, nowy);
  56.     }
  57.     return head;
  58. }
  59.  
  60. Bron* usuwanie(Bron *head, int poz){
  61.     Bron *first = head;
  62.     if(poz == 0){
  63.         first->next->prev = NULL;
  64.         head = first->next;
  65.  
  66.     }else if(poz == 1){
  67.         while(first->next != NULL){
  68.             first = first->next;
  69.         }
  70.         first->prev->next = NULL;
  71.     }
  72.     free(first);
  73.     return head;
  74. }
  75.  
  76. Bron* dodaj_nowy(Bron *head, Bron* nowy, int poz){
  77.     if(poz == 1){
  78.        head = dodaj_pierwszy(head, nowy);
  79.     }else{
  80.         head = dodaj_na_N(head, nowy, poz);
  81.     }
  82.    return head;
  83. }
  84. int main() {
  85.     Bron *pierwsza = (Bron *) malloc(sizeof(Bron));
  86.     pierwsza->numer_broni = 123;
  87.     pierwsza->next = NULL;
  88.     pierwsza->prev = NULL;
  89.     Bron *nowa = (Bron *) malloc(sizeof(Bron));
  90.     Bron *nowa1 = (Bron *) malloc(sizeof(Bron));
  91.     Bron *nowa2 = (Bron *) malloc(sizeof(Bron));
  92.     Bron *nowa3 = (Bron *) malloc(sizeof(Bron));
  93.     nowa->numer_broni = 321;
  94.     nowa1->numer_broni = 56;
  95.     nowa2->numer_broni = 90;
  96.     nowa3->numer_broni = 5;
  97.     pierwsza = dodaj_pierwszy(pierwsza, nowa);
  98.     pierwsza = dodaj_ostatni(pierwsza, nowa1);
  99.     pierwsza = dodaj_ostatni(pierwsza, nowa3);
  100.     //pierwsza = usuwanie(pierwsza, 0);
  101.     pierwsza = dodaj_nowy(pierwsza, nowa2, 5);
  102.  
  103.  
  104.     Bron* first = pierwsza;
  105.     if(first != NULL){
  106.         while(first != NULL){
  107.             printf("Numer broni: %d \n", first->numer_broni);
  108.             first = first->next;
  109.         }
  110.     }
  111.     return 0;
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top