Advertisement
alongouldman

Untitled

Jul 4th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 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;
  58. ptr to;
  59. if(*head == NULL){
  60. printf("\nno nodes" );
  61. return;
  62. }
  63. if((month1 > month2) || ((month1 == month2)&&(hour1 > hour2))){
  64. del_dates(head, month2,hour2,month1, hour1);
  65. return;
  66. }
  67. from = *head;
  68. for(;from;from = from->next){
  69. if((from->primaries_date.month == month1) && (from->primaries_date.hour == hour1)){
  70. break;
  71. }
  72. }
  73. if(!from) {
  74. printf("\nno such date: month %d, hour: %d",month1, hour1 );
  75. return;
  76. }
  77. for(fromprev = *head;fromprev->next != from;fromprev = fromprev->next)
  78. ;
  79. to = *head;
  80. for(;to;to = to->next){
  81. if((to->primaries_date.month == month2) && (to->primaries_date.hour == hour2)){
  82. break;
  83. }
  84. }
  85. if(!to) {
  86. printf("\nno such date: month %d, hour: %d",month2, hour2 );
  87. return;
  88. }
  89. if(from == *head){
  90. *head = to->next;
  91. return;
  92. }
  93. fromprev->next = to->next;
  94. }
  95.  
  96. #define NUM_STATES 10
  97. int main(){
  98. ptr list1 = NULL;
  99. int m,h,o;
  100. int month_to_delete1, month_to_delete2, hour_deleate1, hour_deleate2;
  101. char c;
  102. char state[MAX_STATE];
  103. int i;
  104. printf("\nPlease enter data, thanks" );
  105. for(i=0;i<NUM_STATES;i++){
  106. scanf("%d %d %d %s",&m,&h,&o,state);
  107. add2list(&list1, m,h,o,state);
  108. }
  109. /*
  110. while (scanf("%d %d %d %s",&m,&h,&o,state)!=EOF) {
  111. add2list(&list1, m,h,o,state);
  112. } */
  113. print_list(list1);
  114. printf("\n" );
  115. while((c=getchar()) != '\n') /*cleaning the buffer after the last scanf*/
  116. ;
  117. putchar(c);
  118. scanf("%d %d %d %d", &month_to_delete1, &hour_deleate1, &month_to_delete2, &hour_deleate2);
  119. del_dates(&list1, month_to_delete1,hour_deleate1,month_to_delete2, hour_deleate2);
  120. print_list(list1);
  121.  
  122.  
  123. freelist(&list1);
  124. print_list(list1);
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement