Advertisement
Guest User

test_list

a guest
Nov 13th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <termios.h>
  5. #include <unistd.h>
  6. #include "list.h"
  7.  
  8. /* Compute the number of elements of the list */
  9. int
  10. list_size(list_elem_t * p_list)
  11. {
  12. int nb = 0;
  13. while (p_list != NULL) {
  14. nb += 1;
  15. p_list = p_list->next;
  16. }
  17. return nb;
  18. }
  19.  
  20. /* Print the elements of the list */
  21. void
  22. print_list(list_elem_t* p_list) {
  23. list_elem_t * pl = p_list;
  24. printf("The list contains %d element(s)\n", list_size(p_list));
  25. while(pl != NULL) {
  26. printf("[%d]",pl->value);
  27. pl = pl->next;
  28. if (pl != NULL) {
  29. printf("->");
  30. }
  31. }
  32. }
  33.  
  34. /* Compute the number of memory allocations */
  35.  
  36. int
  37. main(int argc, char **argv)
  38. {
  39. list_elem_t * la_liste = NULL; // The pointer to the head of the list
  40. char menu[] =
  41. "\n Program of chained list \n"\
  42. " 'h/t' : Insert an element to the head/tail of the list\n"\
  43. " 'f' : search of a list element\n"\
  44. " 's' : suppression of a list element\n"\
  45. " 'r' : reverse the order of the list elements\n"\
  46. " 'x' : exit the program\n"\
  47. " What is your choice ?";
  48. int choice=0; // choice from the menu
  49. int value=0; // inserted value
  50.  
  51. printf("%s",menu);
  52. fflush(stdout);
  53.  
  54. while(1) {
  55. fflush(stdin);
  56. choice = getchar();
  57. printf("\n");
  58.  
  59. switch(choice) {
  60. case 'H' :
  61. case 'h' :
  62. printf("Value of the new element ? ");
  63. scanf("%d",&value);
  64. if (insert_head(&la_liste,value)!=0) {
  65. printf("Error : impossible to add the element %d\n",value);
  66. };
  67. break;
  68.  
  69. case 'T' :
  70. case 't' :
  71. printf("Value of the new element ? ");
  72. scanf("%d",&value);
  73. if (insert_tail(&la_liste,value)!=0) {
  74. printf("Error : impossible to add the element %d\n",value);
  75. };
  76. break;
  77.  
  78.  
  79. case 'F' :
  80. case 'f' :
  81. printf("Value of the element's index ? ");
  82. scanf("%d",&value);
  83. list_elem_t * tmp = malloc (sizeof (list_elem_t));
  84. tmp =find_element(la_liste,value);
  85. if (tmp==NULL)
  86. {
  87. printf("couldn't find element");
  88. }
  89. else
  90. {
  91. printf("element's value : %d ", tmp->value);
  92. }
  93.  
  94. break;
  95.  
  96.  
  97.  
  98. case 's' :
  99. case 'S' :
  100. printf("Value of the element to remove");
  101. scanf("%d",&value);
  102. if (remove_element(&la_liste,value)!=0) {
  103. printf("Error : impossible to remove the element %d\n",value);
  104. };
  105. break;
  106.  
  107.  
  108. case 'r' :
  109. case 'R' :
  110. printf("Value of the element to remove ");
  111. if(la_liste == NULL)
  112. {
  113. printf("Error : impossible to reverse the list\n");
  114. }
  115. else
  116. {
  117. reverse_list(&la_liste);
  118. }
  119. break;
  120.  
  121.  
  122. case 'x' :
  123. case 'X' :
  124. return 0;
  125.  
  126. default:
  127. break;
  128. }
  129. print_list(la_liste);
  130.  
  131. getchar(); // to consume a return character and avoid double display of the menu
  132. printf("%s\n",menu);
  133. }
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement