Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. struct data{
  6.   char task[200];
  7.   int priority;
  8.   struct data *next;
  9.   struct data *prev;
  10. } *head, *tail, *curr;
  11.  
  12. /*--------------------PUSHCODE--------------------------------------*/
  13.  
  14. void insert(char task[], int priority, int menu){
  15.  
  16.   struct data *add = (struct data*)malloc(sizeof(struct data));
  17.  
  18.   strcpy(add->task, task);
  19.   add->priority = priority;
  20.  
  21.   //IF NO NODES YET
  22.   if(head==NULL){
  23.     head = tail = add;
  24.     head->prev = NULL;
  25.     tail->next = NULL;
  26.   }
  27.   //IF MENU = 1, PUSHFRONT
  28.   else if(menu == 1){
  29.     add->next = head;
  30.     add->prev = NULL;
  31.     head->prev = add;
  32.     head = add;
  33.   }
  34.   //IF MENU = 2, PUSHMID
  35.   else if(menu == 2){
  36.     curr = head;
  37.     while(curr != NULL && curr->priority <= priority){
  38.       curr = curr->next;
  39.     }
  40.     curr->prev->next = add;
  41.     add->prev = curr->prev;
  42.     curr->prev = add;
  43.     add->next = curr;
  44.   }
  45.   //IF MENU = 3, PUSHBACK
  46.   else if(menu == 3){
  47.     add->prev = tail;
  48.     add->next = NULL;
  49.     tail->next = add;
  50.     tail = add;
  51.   }
  52. }
  53.  
  54. /*-------------------DELETECODE---------------------------------------*/
  55.  
  56.  
  57. void newDelete(){
  58.   if(head==NULL) return;
  59.   if(curr==head && curr==tail){
  60.     free(curr);
  61.     head = tail = NULL;
  62.   }
  63.   else if(curr==head){
  64.     head = head->next;
  65.     free(curr);
  66.     head->prev = NULL;
  67.   }
  68.   else if(curr==tail){
  69.     tail = tail->prev;
  70.     free(curr);
  71.     tail->next = NULL;
  72.   }
  73.   else{
  74.     curr->prev->next = curr->next;
  75.     curr->next->prev = curr->prev;
  76.     free(curr);
  77.   }
  78. }
  79.  
  80. /*---------------------SEARCHCODE------------------------------------*/
  81.  
  82. void search(int priority){
  83.   curr = head;
  84.   while(curr!=NULL && curr->priority<=priority){
  85.     if(curr->priority==priority)
  86.       printf("    %d  |  %s\n", curr->priority, curr->task);
  87.     curr = curr->next;
  88.   }
  89. }
  90.  
  91. /*-----------------UPDATECODE----------------------------------------*/
  92. void update(char task[]){
  93.   curr = head;
  94.   while(curr!=NULL && strcmp(task, curr->task)!=0){
  95.     curr = curr->next;
  96.   }
  97.   if(curr==NULL) printf("Task is not in the list!\n");
  98.   else{
  99.     char task[200]; int priority;
  100.     printf("    %d  |  %s\n", curr->priority, curr->task);
  101.     newDelete();
  102.     printf("Enter new task name: ");
  103.     scanf("%[^\n]", task); getchar();
  104.     printf("Enter new priority: ");
  105.     scanf("%d", &priority); getchar();
  106.     insert(task, priority, 2);
  107.   }
  108. }
  109.  
  110. /*----------------LIST-------------------------------------*/
  111. void list(){
  112.   printf(". . TODO LIST . .\n\n");
  113.   if(head == NULL){
  114.     printf("    No more task left!\n");
  115.   }
  116.   else{
  117.     curr = head;
  118.     while(curr != NULL){
  119.       printf("    %d  |  %s\n", curr->priority, curr->task);
  120.       curr = curr->next;
  121.     }
  122.   }
  123.   puts("");
  124. }
  125.  
  126. /*-----------------------DRIVERCODE--------------------------------------*/
  127.  
  128. int main(){
  129.  
  130.   int choice = 0;
  131.   do{
  132.     list();
  133.     printf("1. Insert New Task\n");
  134.     printf("2. Do Task\n");
  135.     printf("3. Search task on priority\n");
  136.     printf("4. Update Existing Task\n");
  137.     printf("5. Exit\n");
  138.     printf(".. Choice : ");
  139.     scanf("%d", &choice); getchar();
  140.  
  141.     if(choice == 1){
  142.       int menu = 0;
  143.       char task[200];
  144.       int priority;
  145.       printf("Select priority:\n");
  146.       printf("1. Insert urgent task\n");
  147.       printf("2. Insert task\n");
  148.       printf("3. Insert extra task\n");
  149.       printf(".. Choice : ");
  150.       scanf("%d", &menu); getchar();
  151.       if(menu == 1 || menu == 3){
  152.         printf("Enter task name: ");
  153.         scanf("%[^\n]", task); getchar();
  154.         menu == 1 ? priority = 1 : priority = 99;
  155.         insert(task, priority, menu);
  156.       }
  157.       else if(menu==2){
  158.         printf("Enter task name: ");
  159.         scanf("%[^\n]", task); getchar();
  160.         do{
  161.           printf("Enter priority: ");
  162.           scanf("%d", &priority); getchar();
  163.         }while(priority<=0 || priority >= 100);
  164.         insert(task, priority, menu);
  165.       }
  166.     }
  167.  
  168.     else if(choice == 2){
  169.       curr = head;
  170.       newDelete();
  171.     }
  172.  
  173.     else if(choice == 3){
  174.       int priority;
  175.       printf("Enter priority: ");
  176.       scanf("%d", &priority); getchar();
  177.       search(priority);
  178.       puts("");
  179.     }
  180.  
  181.     else if(choice == 4){
  182.       char task[200];
  183.       printf("Enter task name: ");
  184.       scanf("%[^\n]", task); getchar();
  185.       update(task);
  186.     }
  187.  
  188.  
  189.   }while(choice!=5);
  190.  
  191.  
  192.   return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement