Advertisement
alongouldman

Untitled

Jul 4th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_STATE 50
  6.  
  7. typedef struct usa_primaries *ptr;
  8.  
  9. typedef struct usa_primaries{
  10. struct primaries_date{
  11. int month;
  12. int hour;
  13. }primaries_date;
  14. char state[MAX_STATE];
  15. int open;
  16. ptr next;
  17. }usa_primaries;
  18.  
  19. void add2list(ptr *head, int month, int hour,int open, char *state){
  20. ptr node = (ptr)malloc(sizeof(usa_primaries));
  21. if(!node){
  22. exit(1);
  23. }
  24. node->open = open;
  25. strcpy(node->state,state);
  26. node->primaries_date.month = month;
  27. node->primaries_date.hour = hour;
  28. if(*head == NULL){
  29. node->next = NULL;
  30. }
  31. else{
  32. node->next = *head;
  33. }
  34. *head = node;
  35. }
  36.  
  37. void freelist(ptr *head){
  38. ptr curr;
  39. if(*head != NULL){
  40. curr = (*head)->next;
  41. for(;curr;curr=curr->next){
  42. free(*head);
  43. *head = curr;
  44. }
  45. free(*head);
  46. }
  47. }
  48.  
  49. void print_list(ptr head){
  50. for(;head;head = head->next){
  51. printf("\nmonth: %d, hour: %d, state: %s",head->primaries_date.month, head->primaries_date.hour, head->state );
  52. }
  53. printf("\n");
  54. }
  55.  
  56. void del_dates(ptr *head, int month1, int hour1, int month2, int hour2){
  57. ptr from, fromprev, to;
  58. if(*head == NULL){
  59. printf("\nno nodes" );
  60. return;
  61. }
  62. if((month1 > month2) || ((month1 == month2)&&(hour1 > hour2))){
  63. del_dates(head, month2,hour2,month1, hour1);
  64. return;
  65. }
  66. from = *head;
  67. for(;from;from = from->next){
  68. if((from->primaries_date.month == month1) && (from->primaries_date.hour == hour1)){
  69. break;
  70. }
  71. }
  72. if(!from) {
  73. printf("\nno such date: month %d, hour: %d",month1, hour1 );
  74. return;
  75. }
  76. if(from != *head){
  77. fromprev = *head;
  78. for(;fromprev->next != from;fromprev = fromprev->next)
  79. ;
  80. }
  81.  
  82. to = *head;
  83. for(;to;to = to->next){
  84. if((to->primaries_date.month == month2) && (to->primaries_date.hour == hour2)){
  85. break;
  86. }
  87. }
  88. if(!to) {
  89. printf("\nno such date: month %d, hour: %d",month2, hour2 );
  90. return;
  91. }
  92. if(from == *head){
  93. *head = to->next;
  94. return;
  95. }
  96. fromprev->next = to->next;
  97. }
  98.  
  99. #define NUM_STATES 10
  100. int main(){
  101. ptr list1 = NULL;
  102. int m,h,o;
  103. int month_to_delete1, month_to_delete2, hour_deleate1, hour_deleate2;
  104. char c;
  105. char state[MAX_STATE];
  106. int i;
  107. printf("\nPlease enter data, thanks" );
  108. for(i=0;i<NUM_STATES;i++){
  109. scanf("%d %d %d %s",&m,&h,&o,state);
  110. add2list(&list1, m,h,o,state);
  111. }
  112. /*
  113. while (scanf("%d %d %d %s",&m,&h,&o,state)!=EOF) {
  114. add2list(&list1, m,h,o,state);
  115. } */
  116. print_list(list1);
  117. printf("\n" );
  118. while((c=getchar()) != '\n') /*cleaning the buffer after the last scanf*/
  119. ;
  120. putchar(c);
  121. scanf("%d %d %d %d", &month_to_delete1, &hour_deleate1, &month_to_delete2, &hour_deleate2);
  122. del_dates(&list1, month_to_delete1,hour_deleate1,month_to_delete2, hour_deleate2);
  123. print_list(list1);
  124.  
  125.  
  126. freelist(&list1);
  127. print_list(list1);
  128.  
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement