elektryk798

AiSD_Lab4

May 4th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct element
  5. {
  6.     int k;
  7.     struct element*prev;
  8.     struct element*next;
  9. };
  10. void lista_wyswietl(struct element*head);
  11. struct element *lista_dodaj(struct element*head,struct element*nowy);
  12. struct element *lista_szukaj(struct element*head,int wartosc);
  13. struct element *lista_usun(struct element*head,struct element*x);
  14. main()
  15. {
  16.     struct element *head=NULL, *nowy=NULL,*y=NULL;
  17.     char z;
  18.     int liczba,co;
  19.     while(1)
  20.     {
  21.         printf("\nco chcesz zrobic?");
  22.         printf("\nd - dodac");
  23.         printf("\ns - szukac");
  24.         printf("\nu - usunac");
  25.         printf("\no - odwrocic liste");
  26.         printf("\nw - wyswietlic");
  27.         printf("\nq - wyjsc\n");
  28.         fflush(stdin);
  29.         z=getchar();
  30.         switch(z)
  31.         {
  32.         case'd':
  33.             nowy=(struct element*)malloc(sizeof(struct element));
  34.             printf("\npodaj wartosc elementu do wstawienia: ");
  35.             scanf("%d",&liczba);
  36.             nowy->k=liczba;
  37.             head=lista_dodaj(head,nowy);
  38.             break;
  39.         case'w':
  40.             lista_wyswietl(head);
  41.             break;
  42.         case's':
  43.             fflush(stdin);
  44.             scanf("%d",&co);
  45.             printf("%p",lista_szukaj(head,co));
  46.             break;
  47.         case'u':
  48.             fflush(stdin);
  49.             scanf("%d",&co);
  50.             y=lista_szukaj(head,co);
  51.             if (y!=NULL)
  52.             {
  53.                 head=lista_usun(head,y);
  54.             }
  55.         break;
  56.         case'q':
  57.             return 0;
  58.         }
  59.     }
  60. }
  61. void lista_wyswietl(struct element *head)
  62. {
  63.     struct element*x=head;
  64.     while(x!=NULL)
  65.     {
  66.         printf("%d ",x->k);
  67.         x=x->next;
  68.     }
  69. }
  70. struct element *lista_dodaj(struct element*head,struct element*nowy)
  71. {
  72.     nowy->prev=NULL;
  73.     nowy->next=head;
  74.     if(head!=NULL)
  75.         head->prev=nowy;
  76.     head=nowy;
  77.     return head;
  78. }
  79. struct element *lista_szukaj(struct element*head,int wartosc)
  80. {
  81.     struct element*x=head;
  82.     while(x!=NULL)
  83.     {
  84.         if(x->k==wartosc)
  85.             return x;
  86.         x=x->next;
  87.     }
  88.     return NULL;
  89. }
  90. struct element *lista_usun(struct element*head,struct element*x)
  91. {
  92.             if(x->prev!=NULL)
  93.                 x->prev->next=x->next;
  94.             else
  95.                 head=x->next;
  96.             if(x->next!=NULL)
  97.                 x->next->prev=x->prev;
  98.             free(x);
  99.             return head;
  100.  
  101. }
  102. struct element *lista_odwroc(struct element*head)
  103. {
  104.    
  105. }
Add Comment
Please, Sign In to add comment