Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int minutes;
  8.     int hours;
  9.  
  10.     Node* next;
  11. };
  12.  
  13. Node* create_node(int hours, int minutes)
  14. {
  15.     Node* n = (Node*)malloc(sizeof(Node));
  16.     n->minutes = minutes;
  17.     n->hours = hours;
  18.     return n;
  19. }
  20.  
  21. void delete_node(Node* node)
  22. {
  23.     free(node);
  24. }
  25.  
  26. int main()
  27. {
  28.     int n;
  29.     printf("Enter list length: ");
  30.     scanf("%d", &n);
  31.  
  32.     int minutes, hours;
  33.     printf("Enter time: ");
  34.     scanf("%d:%d", &hours, &minutes);
  35.  
  36.     Node* head = create_node(hours, minutes);
  37.  
  38.     for (int i = 1; i < n; ++i)
  39.     {
  40.         int minutes, hours;
  41.         printf("Enter time: ");
  42.         scanf("%d:%d", &hours, &minutes);
  43.  
  44.         Node *p = head;
  45.         while (p)
  46.         {
  47.             if (p->hours < hours || (p->hours == hours && p->minutes <= minutes))
  48.                 break;
  49.        
  50.             if (p->next == NULL)
  51.                 break;
  52.  
  53.             p = p->next;
  54.         }
  55.  
  56.         Node *t = p->next;
  57.         p->next = create_node(hours, minutes);
  58.         p->next->next = t;
  59.     }
  60.  
  61.     Node *p;
  62.     while (head)
  63.     {
  64.         p = head->next;
  65.         delete_node(head);
  66.         head = p;      
  67.     }
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement