In this assignment, you are asked to implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings.

struct node_t {
char* str;
struct node_t* prev;
struct node_t* next;
}

To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer to the tail node of the list (or NULL if the list is empty). Optionally you may also keep an integer count, which indicates the current number of nodes in the list.

You should implement the following functions in mylist.c file (a template file is provided). The functions are declared in the header file mylist.h (the header file is provided and you're not allowed to modify this file).

1.insert(char* str), which creates a node with the given string. Important: you should use the malloc() function twice: once to allocate the space for the node, and the other to allocate the space for copying the string (str) passed in as the argument. You should then insert the newly created node into the doubly-linked list in increasing order (using the standard strcmp() function from libc for string comparison). It's OK to have nodes with the same string.

2.delete (char* str), which removes the node that has the given string. You should search for the node by following the linked list in order; once the node is found, you should reclaim the node by calling the free() function twice: once to reclaim the space occupied by the string, and the other to reclaim the node itself. If the node is not found, the operation can be simply ignored.

3.list(int reverse_order), which prints out all the strings stored in the linked list in order. If reverse_order is true (i.e., non-zero), print the strings from tail to head. Otherwise, print the strings from head to tail.

A main function is provided in main.c file (this file is provided and you are not allowed to modify this file). The main function takes command from standard input. If it's 'insert, the main function will call the insert() function with the string to follow. If its delete, the main function will call the delete() function with the string to follow. If its list', the main function will call the list() function with the integer value (true or false) to follow. Type quit or exit to terminate the program.

An example output of the program is as follows:

$ ./hw3
CMD> list
command: list(0)
< empty >
CMD> insert hello
command: insert('hello')
CMD> insert good morning
command: insert('good morning')
CMD> insert bye
command: insert('bye')
CMD> insert good afternoon
command: insert('good afternoon')
CMD> list
command: list(0)
0: bye
1: good afternoon
2: good morning
3: hello
CMD> delete bye
command: delete('bye')
CMD> list 1
command: list(1)
2: hello
1: good morning
0: good afternoon
CMD> delete whatever
command: delete('whatever')
CMD> delete good afternoon
command: delete('good afternoon')
CMD> list
command: list(0)
0: good morning
1: hello
CMD> exit
Bye!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.