Advertisement
EXTREMEXPLOIT

Seminario III

May 27th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7.  
  8. typedef struct _node {
  9.     int value;
  10.     struct _node* next;
  11. } Node;
  12.  
  13. /*
  14.  *
  15.  */
  16.  
  17. void show_nodes(Node* node) {
  18.     while(node != NULL) {
  19.         printf("Node value: %d\n", node->value);
  20.         node = node->next;
  21.     }
  22. }
  23.  
  24. void clear_nodes(Node* node) {
  25.     while(node != NULL) {
  26.         Node* tmp = node->next;
  27.         free(node);
  28.         node = tmp;
  29.     }
  30. }
  31.  
  32. int ex17(){
  33.    
  34.    
  35.     Node* first = NULL;
  36.     Node* last = NULL;
  37.    
  38.     int finished = FALSE;
  39.     while(finished == FALSE) {
  40.         int value;
  41.         int qty = scanf("%d", &value);
  42.         if(qty <= 0) {
  43.             finished = TRUE;
  44.         } else {
  45.             Node* current = (Node*) malloc(sizeof(Node));
  46.             current->value = value;
  47.             current->next = NULL;
  48.            
  49.             if (first == NULL) {
  50.                 first = current;
  51.                 last = current;
  52.             } else {
  53.                 Node* oldfirst = NULL;
  54.                 oldfirst = first;
  55.                 first = current;
  56.                 first->next = oldfirst;
  57.  
  58.             }
  59.            
  60.         }
  61.        
  62.     }
  63.    
  64.     show_nodes(first);
  65.     clear_nodes(first);
  66.        
  67. }
  68.  
  69. int ex18(){
  70.     Node* first = NULL;
  71.     Node* last = NULL;
  72.    
  73.     int end = FALSE;
  74.     while(end == FALSE){
  75.         int value;
  76.         int qty = scanf("%d", &value);
  77.         if(qty <= 0){
  78.             end = TRUE;
  79.         }
  80.         else{
  81.             Node* current = (Node*) malloc(sizeof(Node));
  82.             current->value = value;
  83.             current->next = NULL;
  84.            
  85.             if (first == NULL){
  86.                 first = current;
  87.                 last = current;
  88.             }
  89.             else{
  90.                 last->next = current;
  91.                 last = current;
  92.             }
  93.         }
  94.     }
  95.    
  96.     show_nodes(first);
  97.     clear_nodes(first);
  98.    
  99. }
  100.  
  101. int ex19(){
  102.     Node* first = NULL;
  103.     Node* last = NULL;
  104.    
  105.     int end = FALSE;
  106.     while(end == FALSE){
  107.         int value;
  108.         int qty = scanf("%d", &value);
  109.         if(qty <= 0){
  110.             end = TRUE;
  111.         }
  112.         else{
  113.             Node* current = (Node*) malloc(sizeof(Node));
  114.             current->value = value;
  115.             current->next = NULL;
  116.            
  117.             if (first == NULL){
  118.                 first = current;
  119.                 last = current;
  120.             }
  121.             else{
  122.                 Node* compare_with = first;
  123.                 int search = TRUE;
  124.                     if (current->value < first->value){
  125.                         Node* oldfirst = NULL;
  126.                         oldfirst = first;
  127.                         first = current;
  128.                         first->next = oldfirst;
  129.                         }
  130.                     else{
  131.                         if (current->value > last->value){
  132.                             last->next = current;
  133.                             last = current;
  134.                             }
  135.                         else{
  136.                             while(search == TRUE){
  137.                             if (compare_with->next->value < current->value){
  138.                                 compare_with = compare_with->next;
  139.                             }
  140.                             else{
  141.                                 Node* next_node = NULL;
  142.                                 next_node = compare_with->next;
  143.                                 compare_with->next = current;
  144.                                 current->next = next_node;
  145.                                 search = FALSE;
  146.                                 }
  147.                            
  148.                             }
  149.                         }    
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.     show_nodes(first);
  155.     clear_nodes(first);
  156.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement