Advertisement
moldovexc

doubly linkedList

May 28th, 2023
949
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 1 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Task {
  6.   char description[100];
  7.   struct Task* next;
  8.   struct Task* previous;
  9. };
  10.  
  11. struct Task* createTask(char description[]){
  12.  struct Task* newTask = (struct Task*)malloc(sizeof(struct Task));
  13.  strcpy(newTask->description, description);
  14.  newTask->next = NULL;
  15.  newTask->previous = NULL;
  16.  return newTask;
  17. }
  18.  
  19. void addTask(struct Task** head, struct Task* newTask){
  20.  if(*head == NULL) {
  21.    *head = newTask;
  22.  } else {
  23.    struct Task* currentElement = *head;
  24.  while(currentElement->next != NULL){
  25.   currentElement = currentElement->next;
  26.   }
  27.  currentElement->next = newTask;
  28.  newTask->previous = currentElement;
  29.  }
  30. }
  31.  
  32. struct Task* displayFirstTask(struct Task* head){
  33.  if (head == NULL){
  34.    printf("No tasks found!\n");
  35.  } else{
  36.   struct Task* firstTask = head;
  37.   while(firstTask->previous !=NULL){
  38.    firstTask = firstTask-> previous;
  39.   }
  40.   return firstTask;
  41.  }
  42. }
  43.  
  44. void deleteTask(struct Task** head, struct Task* task){
  45.  if (*head == task){
  46.    *head = task->next;
  47.  }
  48.  if(task->previous !=NULL){
  49.   task->previous->next = task->next;
  50.  }
  51.  if(task->next !=NULL){
  52.   task->next->previous = task->previous;
  53.  }
  54.  free(task);
  55. }
  56. void displayTasks(struct Task* head){
  57.  struct Task* currentTask = head;
  58.  if(currentTask == NULL){
  59.   printf("No tasks found!\n");
  60.  } else{
  61.   printf("Tasks:\n");
  62.   while(currentTask !=NULL){
  63.    printf("%s\n", currentTask->description);
  64.    currentTask = currentTask->next;
  65.   }
  66.  }
  67. }
  68.  void freeAllTasks(struct Task** head){
  69.   struct Task* currentTask = *head;
  70.   while(currentTask !=NULL){
  71.    struct Task* next = currentTask->next;
  72.    free(currentTask);
  73.    currentTask = next;
  74.   }
  75.   *head = NULL;
  76.  }
  77.  
  78.  int main(){
  79.    
  80.   //initialize queue
  81.   struct Task* tasks = NULL;
  82.  
  83.   //initialize tasks
  84.   struct Task* firstTask = createTask("Task 1");
  85.   struct Task* secondTask = createTask("Task 2");
  86.   struct Task* thirdTask = createTask("Task 3");
  87.  
  88.   //add tasks to queue
  89.   addTask(&tasks,firstTask);
  90.   addTask(&tasks->next, secondTask);
  91.   addTask(&tasks->next, thirdTask);
  92.  
  93.   // print  all tasks
  94.   displayTasks(tasks);
  95.   //print first element in fifo
  96.   struct Task* firstTaskInQueue = displayFirstTask(tasks);
  97.   if(firstTask !=NULL){
  98.    printf("First Task: %s\n", firstTaskInQueue -> description);
  99.  }
  100.   //delete specific task
  101.   deleteTask(&tasks,firstTask);
  102.   //show result
  103.   displayTasks(tasks);
  104.   //delete all tasks to save memory
  105.   freeAllTasks(&tasks);
  106.   //show result
  107.   displayTasks(tasks);
  108.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement