kamilosxd678

ListaDwukierunkowa

Mar 5th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. struct element{
  5.     struct element* previous;
  6.     struct element* next;
  7.     int number;
  8. } *first = NULL;
  9.  
  10. void print(struct element* input){
  11.     if(input != NULL){
  12.         printf("%d ", input->number);
  13.         if(input->next != NULL){
  14.             print(input->next);
  15.         }
  16.         else {
  17.             printf("\n");
  18.         }
  19.     }
  20.     else {
  21.         printf("LISTA JEST PUSTA! \n");
  22.     }
  23. }
  24.  
  25. void insert(int inputValue, struct element* inputElement, struct element* previous){
  26.     struct element* newfirst = (struct element *)malloc(sizeof(struct element));
  27.     if(inputElement == NULL){
  28.         // pusta lista lub koniec
  29.         struct element* tmp = (struct element *)malloc(sizeof(struct element));
  30.         tmp->previous = previous;
  31.         tmp->next = NULL;
  32.         tmp->number = inputValue;
  33.         if(inputElement == first){
  34.             first = tmp;
  35.         }
  36.         else {
  37.             tmp->previous->next = tmp;
  38.         }
  39.     }
  40.     else {
  41.         if(inputElement->number >= inputValue){
  42.             /* Znaleziony wiekszy - wstawiamy przed nim */
  43.             struct element* tmp = (struct element *)malloc(sizeof(struct element));
  44.             tmp->previous = previous;
  45.             tmp->next = inputElement;
  46.             tmp->number = inputValue;
  47.             if(tmp->previous != NULL){
  48.                 tmp->previous->next = tmp;
  49.             }
  50.             else {
  51.                 first = tmp;
  52.             }
  53.             tmp->next->previous = tmp;
  54.         }
  55.         else {
  56.             /* Pudlo - szukamy dalej */
  57.             insert(inputValue, inputElement->next, inputElement);
  58.         }
  59.     }
  60. }
  61.  
  62. void usun(int inputValue){
  63.     if(first == NULL){
  64.         return;
  65.     }
  66.     struct element* tmp = first;
  67.     while(tmp->number != inputValue){
  68.         if(tmp->next == NULL){
  69.             return;
  70.         }
  71.         tmp = tmp->next;
  72.     }
  73.     if(tmp->number == inputValue){
  74.         /* trafienie */
  75.         if(tmp == first){
  76.             if(tmp->next == NULL){
  77.                 first = NULL;
  78.             }
  79.             else {
  80.                 first = tmp->next;
  81.             }
  82.         }
  83.         else {
  84.             if(tmp->next != NULL){
  85.                 tmp->next->previous = tmp->previous;
  86.             }
  87.             if(tmp->previous != NULL){
  88.                 tmp->previous->next = tmp->next;
  89.             }
  90.         }
  91.         free(tmp);
  92.     }
  93. }
  94.  
  95. int main(){
  96.     int option;
  97.     int value;
  98.     int run = 1;
  99.     while(run){
  100.         printf("Wybierz opcje: 1 - insert, 2 - delete, 3 - wyswietl");
  101.         scanf("%d", &option);
  102.         if(option == 1){
  103.             printf("Podaj wartosc do wstawienia: ");
  104.             scanf("%d", &value);
  105.             insert(value, first, NULL);
  106.         }
  107.         else if(option == 2){
  108.             printf("Podaj wartosc do usuniecia: ");
  109.             scanf("%d", &value);
  110.             usun(value);
  111.         }
  112.         else if(option == 3){
  113.             printf("Zawartosc listy ");
  114.             print(first);
  115.         }
  116.         else {
  117.             run = 0;
  118.         }
  119.     }
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment