logo

Crowdly

Browser

Додати до Chrome

Doubly Linked List  In this exercise you will implement a generic doubly linked...

✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.

Doubly Linked List 

In this exercise you will implement a generic doubly linked list in C. 

Each node in the list contains a pointer to a payload (of type void *), a pointer to the next node, and a pointer to the previous node. 

The list itself keeps track of its head, its tail, and the number of elements currently stored. 

You should write your code in multiple files: 

  • LinkedList.h contains the function prototypes and type definitions. 
  • LinkedList.c contains the functions that you are going to implement. 
  • example_program.c contains the main function that tests the linked list. 

You’ll have to implement the below functions (Note that the LinkedList is Dynamically allocated): 

  • // Allocates and initializes an empty list. Returns a pointer to it. 
    • LinkedList *LL_Create(); 
  • // Frees all nodes in the list. 
    • void LL_Destroy(LinkedList *list) 
  • // Appends a new node with the given payload to the end of the list. Returns 1 on success, 0 on failure. 
    • int LL_Append(LinkedList *list, void *payload); 
  • // Prepends a new node with the given payload to the front of the list. Returns 1 on success, 0 on failure. 
    • int LL_Prepend(LinkedList *list, void *payload); 
  • // Displays the contents of the list. The print_fn callback  is used to print each payload using either printInt or printString. 
    • void LL_Display(const LinkedList *list, void (*print_fn)(void *)); 
  • // Prints a payload assumed to be an integer pointer. 
    • void printInt(void *payload); 
  • // Prints a payload assumed to be a string (char *). 
    • void printString(void *payload); 

To demonstrate how LL_Display can be used, you will provide two printing functions that match the required function pointer type (printInt and printString). These functions can be passed to LL_Display depending on the type of data stored in the list. 

Compile the code on a Linux VM. Show screenshots of the compilation process and run the application (if you weren't able to paste thes screenshots here, send the to our TA Mr Joseph Samara (jfs22)). 

The payloads that will be stored in the linked list are taken as command line arguments. The second command line argument represents the type of data that will be stored in the linked list (either int or string). 

Note that in case of int, the command line arguments must be converted to integers (possibly using the atoi function). 

Examples of running the application 

./app int 33 45 21 89 100 2 

./app string hi hello cmps241 systems programming 

All memory you allocate must be freed.

Більше питань подібних до цього

Хочете миттєвий доступ до всіх перевірених відповідей на lms.aub.edu.lb?

Отримайте необмежений доступ до відповідей на екзаменаційні питання - встановіть розширення Crowdly зараз!

Browser

Додати до Chrome