Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. // $Id: $
  2.  
  3. #include <assert.h>
  4. #include <libgen.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. //typeduf enum { FALSE = 0, TRUE = 1} bool;
  9. typedef struct node *node_ref;
  10. struct node {
  11. double item;
  12. node_ref link;
  13. };
  14.  
  15. void insert(node_ref *head, double number ) {
  16. node_ref curr = *head;;
  17. node_ref prev = NULL;
  18.  
  19. while(curr != NULL) {
  20. if (curr->item < number) break;
  21. prev = curr;
  22. curr = curr->link;
  23. }
  24.  
  25. node_ref tmp = malloc(sizeof(struct node));
  26. assert (tmp != NULL);
  27. tmp->item = number;
  28. // assert (tmp->item != NULL);
  29. tmp->link = curr;
  30. if(prev==NULL) *head = tmp;
  31. else prev->link =tmp;
  32. }
  33.  
  34.  
  35. int main (int argc, char **argv) {
  36. char *progname = basename (argv[0]);
  37. char err_buffer [256];
  38. char err_buf_fmt[16];
  39. sprintf (err_buf_fmt, "%%%lds", sizeof err_buffer - 1);
  40. printf ("err_buf_fmt = \"%s\"\n", err_buf_fmt);
  41.  
  42. node_ref head =NULL;
  43.  
  44. for (;;) {
  45.  
  46. // Try to read a double number. Print an error message if a
  47. // word is not recognized as a number. Stop at end of file.
  48.  
  49. double number;
  50. int scancount = scanf ("%lg", &number);
  51.  
  52. if (scancount == EOF) {
  53. printf ("%s: EOF\n", progname);
  54. break;
  55. }else if (scancount == 1) {
  56. insert(&head, number);
  57. }else {
  58. scancount = scanf (err_buf_fmt, err_buffer);
  59. assert (scancount == 1);
  60. printf ("%s: bad number \"%s\"\n", progname, err_buffer);
  61. };
  62. };
  63.  
  64. // return EXIT_SUCCESS;
  65.  
  66. // int max = argc < 2 ? 10 : atoi (argv[1]);
  67. // printf ("%s: looping %d times\n", progname, max);
  68. /* for (int count = 0; count < max; ++count) {
  69. node_ref new = malloc (sizeof (struct node));
  70. assert (new != NULL);
  71. new->item = drand48() * 1e6;
  72. new->link = head;
  73. head = new;
  74. }*/
  75.  
  76.  
  77. //
  78. // Loop down the list, printing out each entry in debug mode.
  79. //
  80. printf ("&head= %p\n", (void*) &head);
  81. printf ("head= %p\n", (void*) head);
  82. for (node_ref curr = head; curr != NULL; curr = curr->link) {
  83. printf ("%p -> struct node {item= %.15g, link= %p}\n",
  84. (void*) curr, curr->item, (void*) curr->link);
  85. }
  86. printf ("NULL= %p\n", (void*) NULL);
  87.  
  88. //
  89. // Free up all of the nodes.
  90. //
  91. while (head != NULL) {
  92. node_ref old = head;
  93. head = head->link;
  94. free (old);
  95. }
  96.  
  97. //
  98. // Deliberately cause some memory leaks and throw away result.
  99. //
  100. for (int leaks = 0; leaks < 4; ++leaks) malloc (256);
  101. malloc (4096);
  102.  
  103. return EXIT_SUCCESS;
  104. }
  105.  
  106. /*
  107. //TEST// valgrind --leak-check=full --log-file=numlist.lisval \
  108. //TEST//./numlist >numlist.lisout 2>&1
  109. //TEST// mkpspdf numlist.ps numlist.c* numlist.lis*
  110. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement