Advertisement
Guest User

Untitled

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