Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct data{
- char task[200];
- int priority;
- struct data *next;
- struct data *prev;
- } *head, *tail, *curr;
- /*--------------------PUSHCODE--------------------------------------*/
- void insert(char task[], int priority, int menu){
- struct data *add = (struct data*)malloc(sizeof(struct data));
- strcpy(add->task, task);
- add->priority = priority;
- //IF NO NODES YET
- if(head==NULL){
- head = tail = add;
- head->prev = NULL;
- tail->next = NULL;
- }
- //IF MENU = 1, PUSHFRONT
- else if(menu == 1){
- add->next = head;
- add->prev = NULL;
- head->prev = add;
- head = add;
- }
- //IF MENU = 2, PUSHMID
- else if(menu == 2){
- curr = head;
- while(curr != NULL && curr->priority <= priority){
- curr = curr->next;
- }
- curr->prev->next = add;
- add->prev = curr->prev;
- curr->prev = add;
- add->next = curr;
- }
- //IF MENU = 3, PUSHBACK
- else if(menu == 3){
- add->prev = tail;
- add->next = NULL;
- tail->next = add;
- tail = add;
- }
- }
- /*-------------------DELETECODE---------------------------------------*/
- void newDelete(){
- if(head==NULL) return;
- if(curr==head && curr==tail){
- free(curr);
- head = tail = NULL;
- }
- else if(curr==head){
- head = head->next;
- free(curr);
- head->prev = NULL;
- }
- else if(curr==tail){
- tail = tail->prev;
- free(curr);
- tail->next = NULL;
- }
- else{
- curr->prev->next = curr->next;
- curr->next->prev = curr->prev;
- free(curr);
- }
- }
- /*---------------------SEARCHCODE------------------------------------*/
- void search(int priority){
- curr = head;
- while(curr!=NULL && curr->priority<=priority){
- if(curr->priority==priority)
- printf(" %d | %s\n", curr->priority, curr->task);
- curr = curr->next;
- }
- }
- /*-----------------UPDATECODE----------------------------------------*/
- void update(char task[]){
- curr = head;
- while(curr!=NULL && strcmp(task, curr->task)!=0){
- curr = curr->next;
- }
- if(curr==NULL) printf("Task is not in the list!\n");
- else{
- char task[200]; int priority;
- printf(" %d | %s\n", curr->priority, curr->task);
- newDelete();
- printf("Enter new task name: ");
- scanf("%[^\n]", task); getchar();
- printf("Enter new priority: ");
- scanf("%d", &priority); getchar();
- insert(task, priority, 2);
- }
- }
- /*----------------LIST-------------------------------------*/
- void list(){
- printf(". . TODO LIST . .\n\n");
- if(head == NULL){
- printf(" No more task left!\n");
- }
- else{
- curr = head;
- while(curr != NULL){
- printf(" %d | %s\n", curr->priority, curr->task);
- curr = curr->next;
- }
- }
- puts("");
- }
- /*-----------------------DRIVERCODE--------------------------------------*/
- int main(){
- int choice = 0;
- do{
- list();
- printf("1. Insert New Task\n");
- printf("2. Do Task\n");
- printf("3. Search task on priority\n");
- printf("4. Update Existing Task\n");
- printf("5. Exit\n");
- printf(".. Choice : ");
- scanf("%d", &choice); getchar();
- if(choice == 1){
- int menu = 0;
- char task[200];
- int priority;
- printf("Select priority:\n");
- printf("1. Insert urgent task\n");
- printf("2. Insert task\n");
- printf("3. Insert extra task\n");
- printf(".. Choice : ");
- scanf("%d", &menu); getchar();
- if(menu == 1 || menu == 3){
- printf("Enter task name: ");
- scanf("%[^\n]", task); getchar();
- menu == 1 ? priority = 1 : priority = 99;
- insert(task, priority, menu);
- }
- else if(menu==2){
- printf("Enter task name: ");
- scanf("%[^\n]", task); getchar();
- do{
- printf("Enter priority: ");
- scanf("%d", &priority); getchar();
- }while(priority<=0 || priority >= 100);
- insert(task, priority, menu);
- }
- }
- else if(choice == 2){
- curr = head;
- newDelete();
- }
- else if(choice == 3){
- int priority;
- printf("Enter priority: ");
- scanf("%d", &priority); getchar();
- search(priority);
- puts("");
- }
- else if(choice == 4){
- char task[200];
- printf("Enter task name: ");
- scanf("%[^\n]", task); getchar();
- update(task);
- }
- }while(choice!=5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement