Facebook
From Kamilek, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 239
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. struct element {
  6.     int k;
  7.     char tekst[50];
  8.     struct element *prev;
  9.     struct element *next;
  10.     int id;
  11. };
  12. void lista_wyswietl(struct element *head);
  13. struct element *lista_dodaj(struct element *head, struct element *nowy);
  14. struct element *lista_usun(struct element *head, struct element *nowy);
  15. struct element *lista_posz(struct element *head, int klucz);
  16. struct element *lista_odwroc(struct element *head);
  17. main()
  18. {
  19.     struct element *head=NULL, *nowy=NULL;
  20. char z;
  21. int liczba, klucz, index=0;
  22. while(1)
  23. {
  24.     printf("nco chcesz zrobic?");
  25.     printf("nd-dodac");
  26.     printf("ns-szukac");
  27.     printf("nu-usunac");
  28.     printf("no-odwrocic liste");
  29.     printf("nw-wyswietlic");
  30.     printf("nq-wyjscn");
  31.     fflush(stdin);
  32.     z=getchar();
  33.  
  34.     switch(z)
  35.     {
  36.         case 'd': nowy=(struct element*)malloc(sizeof(struct element));
  37.                   printf("npodaj wartosc elementu do wstawienia:  ");
  38.                   scanf("%d",&liczba);
  39.  
  40.                   nowy->k=liczba;
  41.                   nowy->id=index++;
  42.                   if(nowy->id%2!=0)
  43.                   {
  44.                     printf("nPodaj tekst do wstawienia: ");
  45.                     fflush(stdin);
  46.                     scanf("%s",nowy->tekst);
  47.                   }
  48.                   else strcpy(nowy->tekst," ");
  49.                   head=lista_dodaj(head,nowy);
  50.                   printf("%d",head->id);
  51.                   break;
  52.         case 'w': lista_wyswietl(head);
  53.                   break;
  54.         case 'u' :
  55.                   break;
  56.         case 'o' :head=lista_odwroc(head);
  57.                   break;
  58.         case 's' :fflush(stdin);
  59.                   printf("nPodaj wartosc klucza jaka chcesz wyszukac");
  60.                   scanf("%d",&klucz);
  61.                   printf("%d",*lista_posz(head, klucz));
  62.                   break;
  63.         case 'q': return 0;
  64.     }
  65. }
  66. }
  67.  
  68. void lista_wyswietl(struct element *head)
  69. {
  70.     struct element *x=head;
  71.     while(x!=NULL)
  72.     {
  73.         printf("%d",x->k);
  74.         printf("%sn",x->tekst);
  75.         x=x->next;
  76.  
  77.     }
  78. }
  79. struct element *lista_dodaj(struct element *head, struct element *nowy)
  80. {
  81. nowy->prev=NULL;
  82. nowy->next=head;
  83. if(head!=NULL)
  84.     head->prev=nowy;
  85.     head=nowy;
  86.     return head;
  87. }
  88.  
  89. struct element *lista_usun(struct element *head, struct element *x)
  90. {
  91.     if(x->prev!=NULL)
  92.         (x->prev)->next=x->next;
  93.     else head=x->next;
  94.     if (x->next!=NULL)
  95.         (x->next)->prev=x->prev;
  96.  
  97. }
  98.  
  99. struct element *lista_posz(struct element *head, int klucz)
  100. {
  101.     struct element *x=NULL;
  102.     x=head;
  103.     while(x!=NULL && x->k!=klucz)
  104.         x=x->next;
  105.     return x;
  106.  
  107. }
  108. struct element *lista_odwroc(struct element *head)
  109. {
  110.     struct element *x=NULL;
  111.     struct element *temp=NULL;
  112.     struct element *temp1=NULL;
  113.     x=head;
  114.     while(x!=NULL)
  115.     {temp1=(struct element*)malloc(sizeof(struct element));
  116.     temp1->k=x->k;
  117.     temp=lista_dodaj(temp,temp1);
  118.     x=x->next;
  119.     }
  120.     return temp;
  121. }
  122.