SHARE
TWEET

Untitled

a guest Feb 2nd, 2016 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct _elem {
  6.   char* key;
  7.   struct _elem *next;
  8. } elem;
  9.  
  10. int compare(const void *a, const void *b){
  11. return strcmp(*(char**)a, *(char**)b);
  12. }
  13.  
  14. elem* InserisciCoda(char *s){
  15. elem *new;
  16. new=(elem*)malloc(sizeof(elem));
  17. new->key=s;
  18. new->next=NULL;
  19. return new;
  20. }
  21.  
  22. elem* RimuoviPrimo(elem *head){
  23. elem *curr = head;
  24. head=head->next;
  25. free(curr);
  26. return head;
  27. }
  28.  
  29. void OrdinaLista(elem *head){
  30.     int len, i;
  31.     elem *curr = head;
  32.     while(curr!=NULL) len++;
  33.     curr=head;
  34.     char **a;
  35.     a = (char **) malloc(len*sizeof(char*));
  36.     i=0;
  37.     while(i<len && curr!=NULL){
  38.        a[i] = curr->key;
  39.        i++;
  40.    }
  41.     qsort(a,len,sizeof(char*),compare);
  42.     for(i=0;i<len;i++) printf("$%s\n",a[i]);
  43.     for(i=0;i<len;i++) free(a[i]);
  44. }
  45.  
  46. int main () {
  47. elem *head = (elem*)malloc(sizeof(elem));
  48. elem *first = (elem*)malloc(sizeof(elem));
  49. elem *curr = (elem*)malloc(sizeof(elem));
  50. char s[101];
  51. int x = -1;
  52. first=NULL;
  53.  
  54. scanf("%d",&x);
  55. scanf("%s",s);
  56. head->key=s;
  57. head->next=NULL;
  58. head->prec=NULL;
  59. first=head;
  60.  
  61. while(x!=0){
  62.   scanf("%d", &x);
  63.   if (x==1){
  64.     scanf("%s", s);
  65.     head->next=InserisciCoda(head, s);
  66.     head=head->next;
  67.     }
  68.   if (x==2)
  69.    first=RimuoviPrimo(first);
  70. }
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