Member Avatar for Mouche

I'm working on a tasklist program to brush up on my C before I take a class. I have two structs, task and tasklist.

Here is the task struct:

typedef struct task
{
  char name[100];
  bool completed; /* bool is described as an enum in an included header file */
  date due_date; /* date is a described as a struct in an included header file */
} task;

I have the following working functions:

  • List item
  • init_tasklist(tasklist): initialize tasklist variables
  • add_task(): add task to the tasklist
  • delete_task(): delete the given task from the tasklist
  • print_tasklist(): print out the tasklist similar to a normal todo list with a checkbox, name and due date for each item
  • delete_tasklist(): delete the given tasklist
  • print_task(): print out the details of a given task

My question has to do with which variables to have as parameters to functions and what to return.

I would like to use this tasklist for a cli interface and then change it to a GUI, so I would like to make that an easy switch.

At first, I had add_task() malloc()-ing a new task and returning a pointer to that task, and then delete_tasklist() recursively went through and free()-ed the memory as it deleted the tasklist. I got this approach from my experience with OOP and Java.

What's the best way to design this in C?

Should the add_task() function take a task struct to add to the list or perhaps just the variables that should be in the task and have the add_task() function initialize it?

Should I write a get_task() function that returns the pointer to a task with the given name for functions like print_task() and delete_task(), which would take a task pointer, or should those take char pointers and find the task themselves?

Any guidance on how to design the I/O of these functions would be very helpful.

>>I would like to use this tasklist for a cli interface and then change it to a GUI

AFAIK CLR is only supported by c++, not C. So you might as well switch to c++ now if your intent is to use it in a c++/clr project.

Member Avatar for Mouche

I'm sorry, I meant "command line interface" -- just text in a terminal window.

Have you worked with a linked list ? I think all of your functions are some of the most basic operations on a linked list

I second. A linked list is the best option.

Member Avatar for Mouche

I'm actually using a linked list.

I accidentally left of the member variable

task *next;

from the task struct. My tasklist struct has

task *first

so all the functions manipulate that linked list.

So what is your problem then ?
Ad task is similar to add a node in the linked list
Delete task is delete a node from the linked list
Print task list is print all the node is the linked list

Member Avatar for Mouche

Did you read the last part of my message? I have working functions, but someone reviewed my code and said it was too much like OOP and that I should reorganize my functions.

Here's one question:

Should add_task() take a task pointer or instead, take a char pointer so you pass the name and have the function create the task struct and then add it?

Quick question are you storing the linked list globally ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.