Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <assert.h>
  5. #include <stdbool.h>
  6.  
  7.  
  8. unsigned int ns[]={10,20,50,100,500,1000,2500,5000,10000,15000};
  9.  
  10. struct node {
  11. int key;
  12. struct node *next;
  13.  
  14. };
  15.  
  16.  struct node *head = NULL;
  17.  struct node*wsk;
  18.  struct node*prev;
  19.  
  20.  
  21.  struct node* list_insert(int value) {
  22.  
  23.     struct node*createnode=malloc(sizeof(*createnode));
  24.     createnode->key=value;
  25.     createnode->next=head;
  26.     head=createnode;
  27.     return createnode;
  28. }
  29.  
  30.  
  31. struct node* list_search(int value) {
  32.    struct node*wsk=head;
  33.     while(wsk!=NULL&&wsk->key!=value){
  34.         wsk=wsk->next;
  35.     }
  36.  
  37.     return wsk;
  38.  
  39. }
  40.  
  41. void list_delete(int value) {
  42.     if (head->key==value){
  43.         head=head->next;
  44.     }
  45.     else{
  46.    prev=head;
  47.     wsk=head->next;
  48.     while(wsk!=NULL&&wsk->key!=value){
  49.         prev=wsk;
  50.         wsk=wsk->next;
  51.     }
  52.     if(wsk!=NULL){
  53.             prev->next=wsk->next;
  54.  
  55.     }
  56.  
  57.     }
  58.  
  59. }
  60.  
  61. unsigned int list_size() {
  62.     int size=0;
  63.     wsk=head;
  64.     while(wsk!=NULL){
  65.         size+=1;
  66.         wsk=wsk->next;
  67.     }
  68.     return size;
  69.  
  70. }
  71.  
  72.  
  73.  
  74. void fill_increasing(int *t, int n) {
  75.     for (int i = 0; i < n; i++) {
  76.         t[i] = i;
  77.     }
  78. }
  79.  
  80.  
  81. void shuffle(int *t, int n) {
  82.     for (int i = n - 1; i > 0; i--) {
  83.         int j = rand() % i;
  84.         int temp = t[i];
  85.         t[i] = t[j];
  86.         t[j] = temp;
  87.     }
  88. }
  89. int main(){
  90. bool no_yes[] = { false, true };
  91.  
  92.     for (unsigned int i = 0; i < sizeof(no_yes) / sizeof(*no_yes); i++) {
  93.         bool enable_shuffle = no_yes[i];
  94.  
  95.         for (unsigned int j = 0; j < sizeof(ns) / sizeof(*ns); j++) {
  96.             unsigned int n = ns[j];
  97.           int *t = malloc(n * sizeof(*t));
  98.             fill_increasing(t, n);
  99.  
  100.             // if true, reorder array elements randomly
  101.             if (enable_shuffle) {
  102.                 shuffle(t, n);
  103.             }
  104.  
  105.             // insert elements in the order present in array `t`
  106.             clock_t insertion_time = clock();
  107.             for (unsigned int k = 0; k < n; k++) {
  108.                 struct node *iter = list_insert(t[k]);
  109.                 assert(iter != NULL);       // inserted element cannot be NULL
  110.                 assert(iter->key == t[k]);  // inserted element must contain the expected value
  111.             }
  112.             insertion_time = clock() - insertion_time;
  113.  
  114.             // reorder array elements before searching
  115.             shuffle(t, n);
  116.  
  117.             // search for every element in the order present in array `t`
  118.             clock_t search_time = clock();
  119.             for (unsigned int k = 0; k < n; k++) {
  120.                 struct node *iter = list_search(t[k]);
  121.                 assert(iter != NULL);       // found element cannot be NULL
  122.                 assert(iter->key == t[k]);  // found element must contain the expected value
  123.             }
  124.             search_time = clock() - search_time;
  125.  
  126.             // reorder array elements before deletion
  127.             shuffle(t, n);
  128.  
  129.             // delete every element in the order present in array `t`
  130.             for (unsigned int k = 0, l = n; k < n; k++, l--) {
  131.                 assert(list_size() == l);   // list size must be equal to the expected value
  132.                 list_delete(t[k]);
  133.             }
  134.             assert(list_size() == 0);       // after all deletions, the list size is zero
  135.             assert(head == NULL);           // after all deletions, the list's head is NULL
  136.  
  137.           free(t);
  138.  
  139.  
  140.                     printf("%d\t%s\t%f\t%f\n", n, enable_shuffle ? "true" : "false",
  141.                     (double)insertion_time / CLOCKS_PER_SEC,
  142.                     (double)search_time / CLOCKS_PER_SEC);
  143.         }
  144.     }
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement