Advertisement
ibanezzaro

Linked List SEQ SORT

Feb 19th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.88 KB | None | 0 0
  1. /*
  2.  * llwmf.c
  3.  *
  4.  *  Created on: Feb 13, 2016
  5.  *      Author: Dario
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stddef.h>
  10. #include <stdbool.h>
  11.  
  12. //STRUCT LIST DEFINITION
  13.  
  14.  
  15. typedef struct list
  16. {
  17.     int value;
  18.     struct list* next_ptr;
  19. }tylist;
  20.  
  21. //FUNCTION PROTOTYPES
  22.  
  23. tylist init(tylist**);
  24. void insert_at_beg(tylist**, int);
  25. void insert_at_end(tylist**, int);
  26. bool delete_at_beg(tylist**);
  27. bool delete_at_end(tylist**);
  28. void mid_insert(tylist**, int);
  29. void mid_delete(tylist**);
  30. bool search_list(tylist**, int);
  31. void visit_list(tylist**);
  32. void ord_insert(tylist**, int);
  33. void action(int, tylist**);
  34. int choicer();
  35. void seq_sort(tylist **);
  36. int get_dimension(tylist *);
  37. int get_value(tylist *ptrptr, int);
  38. void swap(tylist ** ptrptr, int ,int);
  39.  
  40. //MAIN FUNCT
  41.  
  42. int main()
  43. {
  44.  
  45.     tylist *ptrptr;
  46.     ptrptr = NULL;
  47.     printf("\nI'm going to initialize the list now\n");
  48.     action(choicer(), &ptrptr);
  49.     return 0;
  50. }
  51.  
  52. //FUNCTION DECLARATIONS
  53.  
  54. tylist init(tylist** ptrptr)
  55. {
  56.     *ptrptr = NULL;
  57.     return **ptrptr;
  58. }
  59.  
  60. void insert_at_beg(tylist** ptrptr, int value)
  61. {
  62.     if(*ptrptr != NULL)                             //Fanno la stessa cosa in pratica
  63.     {
  64.         tylist* tmp_ptr;
  65.         tmp_ptr = *ptrptr;
  66.         *ptrptr = (tylist*)malloc(sizeof(tylist));
  67.         (*ptrptr)->value = value;
  68.         (*ptrptr)->next_ptr = tmp_ptr;
  69.     }
  70.     else
  71.     {
  72.         *ptrptr = (tylist*)malloc(sizeof(tylist));
  73.         (*ptrptr)->value = value;
  74.         (*ptrptr)->next_ptr = NULL;
  75.     }
  76. }
  77.  
  78. void insert_at_end(tylist** ptrptr, int value)
  79. {
  80.     if(*ptrptr != NULL)
  81.     {
  82.         while(*ptrptr != NULL)
  83.         {
  84.             ptrptr = &((*ptrptr)->next_ptr);
  85.         }
  86.         insert_at_beg(ptrptr, value);
  87.     }
  88.     else
  89.     {
  90.         printf("\nList is empty, will insert %d at beginning\n", value);
  91.         insert_at_beg(ptrptr, value);
  92.     }
  93. }
  94.  
  95. bool delete_at_beg(tylist** ptrptr)
  96. {
  97.     if(*ptrptr == NULL)
  98.     {
  99.         printf("\nList is empty, there's no value to delete\n");
  100.         return false;
  101.     }
  102.     else
  103.     {
  104.         tylist * tmp_ptr;
  105.         tmp_ptr = *ptrptr;
  106.         (*ptrptr) = (*ptrptr)->next_ptr;
  107.         printf("\nYou are going to cancel %d from the list\n"
  108.                 "\n%d has been deleted\n", tmp_ptr->value, tmp_ptr->value);
  109.         return true;
  110.     }
  111. }
  112.  
  113. bool delete_at_end(tylist** ptrptr)
  114. {
  115.     if(*ptrptr == NULL)
  116.     {
  117.         printf("\nList is empty, there's no value to delete\n");
  118.         return false;
  119.     }
  120.     else if((*ptrptr)->next_ptr == NULL)
  121.     {
  122.         printf("\nThere were only two value on the list, one it's %d and you are going to cancel it\n", (*ptrptr)->value);
  123.         free(*ptrptr);
  124.         *ptrptr = NULL;
  125.         return true;
  126.     }
  127.     else if(*ptrptr != NULL)
  128.     {
  129.         while((*ptrptr)->next_ptr->next_ptr != NULL)
  130.         {
  131.             ptrptr = &((*ptrptr)->next_ptr);
  132.         }
  133.         tylist* tmp_ptr;
  134.         tmp_ptr = (*ptrptr)->next_ptr;
  135.         printf("\nYou are going to delete %d\n", tmp_ptr->value);
  136.         (*ptrptr)->next_ptr = NULL;
  137.         free(tmp_ptr);
  138.         return true;
  139.     }
  140.     else
  141.     {
  142.         printf("\nThere's something wrong!\n");
  143.         return false;
  144.     }
  145. }
  146.  
  147. void mid_insert(tylist** ptrptr, int value)
  148. {
  149.     int count, i, moved_value;
  150.     char response;
  151.     printf("\nSpecify in which position you want to insert your value (position 0 not valid):\n");
  152.     scanf("%d", &count);
  153.     if(count == 1)
  154.     {
  155.         insert_at_beg(ptrptr, value);
  156.     }
  157.     else
  158.     {
  159.         for(i = 1; i < count;)
  160.         {
  161.             ptrptr = &((*ptrptr)->next_ptr);
  162.             if(*ptrptr == NULL)
  163.             {
  164.                 printf("\nYou've reached the end of the list, do you want to insert anyway in position %d? (Y\\N)\n", i);
  165.                 scanf(" %c", &response);
  166.                 if(response == 'Y' || response == 'y')
  167.                 {
  168.                     printf("\nI'm going to insert your value (%d) at the end of the list\n", value);
  169.                     insert_at_end(ptrptr, value);
  170.                 }
  171.                 else
  172.                 {
  173.                     printf("\nBadass\n");
  174.                 }
  175.             }
  176.             else
  177.             {
  178.                 i++;
  179.             }
  180.         }
  181.         moved_value = (*ptrptr)->value;
  182.         insert_at_beg(ptrptr, value);
  183.         printf("\nYou've inserted %d before %d", value, moved_value);
  184.     //alla fine del for siamo nel punto giusto in cui vogliamo inserire un nuovo valore
  185.     }
  186. }
  187.  
  188.  
  189. void mid_delete(tylist** ptrptr) //SPOSTA TUTTO INDIETRO CHE ATRIMENTI VA IN SEGFAULT, PERCHE' PUNTA A QUALCOSA OLTRE NULL
  190. {
  191.     int count, i;
  192.     i = 1;
  193.     printf("\nSpecify in which position you want to delete some value (position 0 not valid):\n");
  194.     scanf("%d", &count);
  195.     if(count == 1)
  196.     {
  197.         delete_at_beg(ptrptr);
  198.     }
  199.     else if(count > 1)
  200.     {
  201.         if((*ptrptr)->next_ptr->next_ptr == NULL)  //caso 2 elementi in lista
  202.         {
  203.             printf("\nYou are deleting the last element of the list (%d)\n", (*ptrptr)->next_ptr->value);
  204.             (*ptrptr)->next_ptr = NULL;
  205.         }
  206.         else
  207.         {
  208.             while(i < count)
  209.             {
  210.                 ptrptr = &((*ptrptr)->next_ptr);
  211.                 if((*ptrptr)->next_ptr == NULL)
  212.                 {
  213.                     i = count;
  214.                 }
  215.                 else
  216.                 {
  217.                     i++;
  218.                 }
  219.             }
  220.             delete_at_beg(ptrptr);
  221.         }
  222.     }
  223.  
  224.  
  225.         /*i = 1;
  226.         while(i < count || deleted == false)
  227.         {
  228.             ptrptr = &((*ptrptr)->next_ptr);
  229.             if((*ptrptr)->next_ptr == NULL)
  230.             {
  231.                 printf("\nYou've reached the end of the list, do you want to delete the end value (%d)?\n (Y\\N?):", (*ptrptr)->value);
  232.                 scanf(" %c", &response);
  233.                 if(response == 'Y' || response == 'y')
  234.                 {
  235.                     deleted = true;
  236.                     printf("\nYou are going to cancel %d from the list\n"
  237.                            "\n%d has been deleted\n", (*ptrptr)->value, (*ptrptr)->value);
  238.                     *ptrptr = NULL;
  239.                 }
  240.                 else
  241.                 {
  242.                     deleted = true;
  243.                     printf("\nNope!\n");
  244.                 }
  245.  
  246.             }
  247.             else
  248.             {
  249.                 i++;
  250.             }
  251.         }
  252.         if(deleted == false)
  253.         {
  254.             moved_value = (*ptrptr)->value;
  255.             delete_at_beg(ptrptr);
  256.             printf("\nYou've deleted %d in position %d", moved_value, count);
  257.         }
  258.     }*/
  259. }
  260.  
  261.  
  262. bool search_list(tylist **ptrptr, int value)
  263. {
  264.     char response;
  265.     printf("\nIs this an ordered list?\nIf is ordered type Y (y will work too), otherwise type any other character: ");
  266.     scanf(" %c", &response);
  267.  
  268.     if(response == 'Y' || response == 'y')
  269.     {
  270.         if((*ptrptr)->value == value)
  271.         {
  272.             printf("\nYour value is at the beginning of the list\n");
  273.             return true;
  274.         }
  275.         else
  276.         {
  277.             int count = 1;
  278.             while((*ptrptr)->value < value)
  279.             {
  280.                 ptrptr = &((*ptrptr)->next_ptr);
  281.                 count++;
  282.             }
  283.             if((*ptrptr)->value == value)
  284.             {
  285.                 printf("\nLooks like your value (%d) was in position %d\n", value, count);
  286.                 return true;
  287.             }
  288.             else
  289.             {
  290.                 printf("\nLooks like your value isn't in the list, the closest one i've found is %d\n", (*ptrptr)->value);
  291.                 return false;
  292.             }
  293.         }
  294.     }
  295.     else if(response != 'Y' || response != 'y')
  296.     {
  297.         int count = 1;
  298.         printf("\nSo the list isn't ordered, i'm going to look right through it\n");
  299.         while((*ptrptr)->value != value && *ptrptr != NULL)
  300.         {
  301.             ptrptr = &((*ptrptr)->next_ptr);
  302.             count++;
  303.         }
  304.         if((*ptrptr) == NULL)
  305.         {
  306.             printf("\nLooks like your value (%d) isn't in the list\n", value);
  307.             return false;
  308.         }
  309.         else
  310.         {
  311.             printf("\nI've find your value (%d), was in position %d", value, count);
  312.             return true;
  313.         }
  314.     }
  315.     else
  316.     {
  317.         printf("There's some error somewhere over the rainbow");
  318.         return false;
  319.     }
  320. }
  321.  
  322. void visit_list(tylist **ptrptr) //TODO L'OTTAVO VALORE INSERITO RESETTA LA FUNZIONE, RESTITUENDO GLI ULTIMI DUE VALORE ED UNO 0 AL TERZO POSTO
  323. {
  324.     while((*ptrptr) != NULL)
  325.     {
  326.         printf(" %d", (*ptrptr)->value);
  327.         ptrptr = &((*ptrptr)->next_ptr);
  328.     }
  329. }
  330.  
  331. void ord_insert(tylist** ptrptr, int value)
  332. {
  333.     int count = 1;
  334.     printf("\nI'm going to assume you've ordered the list\n");
  335.     while((*ptrptr)->value < value && (*ptrptr)->next_ptr != NULL)
  336.     {
  337.         ptrptr = &((*ptrptr)->next_ptr);
  338.         count++;
  339.     }
  340.     if((*ptrptr)->next_ptr == NULL)
  341.     {
  342.         ptrptr = &((*ptrptr)->next_ptr);
  343.     }
  344.     insert_at_beg(ptrptr, value);
  345.     printf("\nYour value (%d) was inserted in position %d\n", value, count);
  346.  
  347.     /*if((*ptrptr)->value > value)
  348.     {
  349.         printf("\nLooks like your value was the smallest among all others\n");
  350.         insert_at_beg(ptrptr, value);
  351.     }*/
  352.  
  353. }
  354.  
  355. int choicer()
  356. {
  357.     int choice;
  358.     printf("\nWhat do you want to do?\n"
  359.                 "1: Insert a value at beginning\n"
  360.                 "2: Insert a value at end\n"
  361.                 "3: Delete a value at beginning\n"
  362.                 "4: Delete a value at end\n"
  363.                 "5: Insert a value in some middle position\n"
  364.                 "6: Delete a value in some middle position\n"
  365.                 "7: Search for a value\n"
  366.                 "8: Print your list\n"
  367.                 "9: Insert a value in order\n"
  368.                 "10: Sequential sort\n"
  369.                 "0: Quit\n"
  370.                 "Type the number corresponding your choice: ");
  371.     scanf("%d", &choice);
  372.     return choice;
  373. }
  374.  
  375. void action(int choice, tylist **ptrptr)
  376. {
  377.     switch(choice)
  378.     {
  379.     int value;
  380.     case 1:
  381.         printf("\nValue: ");
  382.         scanf("%d", &value);
  383.         insert_at_beg(ptrptr, value);
  384.         action(choicer(), ptrptr);
  385.         break;
  386.     case 2:
  387.         printf("\nValue: ");
  388.         scanf("%d", &value);
  389.         insert_at_end(ptrptr, value);
  390.         action(choicer(), ptrptr);
  391.         break;
  392.     case 3:
  393.         delete_at_beg(ptrptr);
  394.         action(choicer(), ptrptr);
  395.         break;
  396.     case 4:
  397.         delete_at_end(ptrptr);
  398.         action(choicer(), ptrptr);
  399.         break;
  400.     case 5:
  401.         printf("\nValue: ");
  402.         scanf("%d", &value);
  403.         mid_insert(ptrptr, value);
  404.         action(choicer(), ptrptr);
  405.         break;
  406.     case 6:
  407.         mid_delete(ptrptr);
  408.         action(choicer(), ptrptr);
  409.         break;
  410.     case 7:
  411.         printf("\nValue: ");
  412.         scanf(" %d", &value);
  413.         search_list(ptrptr, value);
  414.         action(choicer(), ptrptr);
  415.         break;
  416.     case 8:
  417.         visit_list(ptrptr);
  418.         action(choicer(), ptrptr);
  419.         break;
  420.     case 9:
  421.         printf("\nValue: ");
  422.         scanf(" %d", &value);
  423.         ord_insert(ptrptr, value);
  424.         action(choicer(), ptrptr);
  425.         break;
  426.     case 10:
  427.         seq_sort(ptrptr);
  428.         break;
  429.     case 0:
  430.         free(*ptrptr);
  431.         break;
  432.     }
  433. }
  434.  
  435. void seq_sort(tylist ** ptrptr)
  436. {
  437.     int i, n1, n2, N, first_tmp_val, second_tmp_val;
  438.     N = get_dimension(*ptrptr);
  439.     for(i = 0; i < N - 1; i++)
  440.     {
  441.         for(n1 = 1, n2 = 0; n1 < N - i; n1++)
  442.         {
  443.             first_tmp_val = get_value(*ptrptr, n1);
  444.             second_tmp_val = get_value(*ptrptr, n2);
  445.             if(first_tmp_val > second_tmp_val)
  446.             {
  447.                 n2 = n1;
  448.             }
  449.         }
  450.         swap(ptrptr, n2, N - i);
  451.         visit_list(ptrptr);             // <------------|
  452.     }                                   //      |       |
  453. }                                       //      |       |
  454.                                         //      |       |
  455. void swap(tylist ** ptrptr, int n1,int n2)      //where n1 and n2 are movement from first element to swapping elements
  456. {
  457.     int i;
  458.     for(i = 0; i < n1; i++)
  459.     {
  460.         ptrptr = &((*ptrptr)->next_ptr);
  461.     }
  462.     (*ptrptr)->value = get_value(*ptrptr, n2);
  463.     for(i = 0; i <= n2 - n1; i++)
  464.     {
  465.         ptrptr = &((*ptrptr)->next_ptr);
  466.     }
  467.     (*ptrptr)->value = get_value(*ptrptr, n1);
  468. }
  469.  
  470. /*int get_first_val(tylist ** ptrptr, n1)
  471. {
  472.     int i, first_tmp_val;
  473.     for(i = 0; i < n1; i++)
  474.     {
  475.         ptrptr = &((*ptrptr)->next_ptr);
  476.     }
  477.     first_tmp_val = (*ptrptr)->value;
  478.     return first_tmp_val;
  479. }
  480.  
  481. int get_second_val(tylist ** ptrptr, n2)
  482. {
  483.     int i, second_tmp_val;
  484.     for(i = 0; i < n2; i++)
  485.     {
  486.         ptrptr = &((*ptrptr)->next_ptr);
  487.     }
  488.     second_tmp_val = (*ptrptr)->value;
  489.     return second_tmp_val;
  490. }*/
  491.  
  492. int get_dimension(tylist * ptrptr)
  493. {
  494.     int i = 1;
  495.     while((ptrptr)->next_ptr != NULL)
  496.     {
  497.         ptrptr = ptrptr->next_ptr;
  498.         i++;
  499.     }
  500.     return i;
  501. }
  502.  
  503. int get_value(tylist *ptrptr, int n)
  504. {
  505.     int i, val;
  506.     for(i = 1; i < n - 1; i++)
  507.     {
  508.         ptrptr = ptrptr->next_ptr;
  509.     }
  510.     val = ptrptr->value;
  511.     return val;
  512. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement