Hide menu

Lab 2 API

We have listed the most important functions and data structures which you will use in Lab 2 below. You will be using two helper libraries - queue (queue.c, queue.h) and doublylinkedlist (doublylinkedlist.c, doublylinkedlist.h).

Queue

This library implements queue data structure. You can use this library to queue this type of data:

Library implements following structures and functions:

  • queue_t - is an alias for struct s_QUEUE_STRUCT. Queue structure holds information about: head node, tail node and number of nodes in the queue.
  • queue_node_t - is an alias for struct s_QUEUE_NODE_STRUCT. Queue node structure holds: data, data type and pointer to next node in the queue.
  • queue_t *queue_init(void) - Initialize FIFO queue. Run this function once to initialize a new queue.

    Example:

    		// Initialize a queue
    		queue_t *test_queue = queue_init();
  • queue_destroy(queue_t *qs) - Deinitialize FIFO queue. Run this once when you want to destroy the queue.

    Example:

    		// Destroy the queue
    		queue_destroy(test_queue);
  • void queue_enqueue(queue_t *qs, void *data, int data_type) - Add data structure to the queue.

    Example:

    		// Put data in a queue
    		queue_enqueue(test_queue, g_robot, s_DATA_STRUCT_TYPE_ROBOT);
  • void queue_dequeue(queue_t *qs, void *data, int *data_type) - Remove data structure from queue

    Example:

    		// Define variables
    		robot_t robot;
    		int data_type;
    		// Get data from structure
    		queue_dequeue(test_queue, &robot, &data_type);
  • void queue_empty(queue_t *qs) - Empty the queue. Remove all data nodes from the queue.

    Example:

    		// Make queue empty
    		queue_empty(test_queue);

Doubly Linked List

This library implements doubly linked list data structure. You can use this library to store the same data listed in the queue description. You can read more about doubly linked list here

Library implements following structures and functions:

  • doublylinkedlist_t - is an alias for struct s_DOUBLYLINKEDLIST_STRUCT. Doubly linked list structure holds information about: first node, last node and number of nodes in the list.
  • doublylinkedlist_node_t - is an alias for struct s_DOUBLYLINKEDLIST_NODE_STRUCT. Doubly linked list node structure holds: data, data type and pointers to the next and previous node in the list.
  • doublylinkedlist_t *doublylinkedlist_init(void) - Initialize doubly linked list. Run this function once to initialize a new list.

    Example:

    		// Initialize a doubly linked list
    		doublylinkedlist_t *test_doublylinkedlist = doublylinkedlist_init();
  • void doublylinkedlist_destroy(doublylinkedlist_t *dlls) - Deinitialize doubly linked list. Run to destroy list and free up the allocated memory.

    Example:

    		// Destroy the doubly linked list
    		doublylinkedlist_destroy(test_doublylinkedlist);
  • void doublylinkedlist_insert_after(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int data_type) - Add a new node after specified node.

    Example:

    		// Insert a node after first node
    		doublylinkedlist_insert_after(	test_doublylinkedlist,
    						test_doublylinkedlist->first,
    						g_robot,
    						s_DATA_STRUCT_TYPE_ROBOT);
  • void doublylinkedlist_insert_before(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int data_type) - Add a new node before specified node.

    Example:

    		// Insert a node before last node
    		doublylinkedlist_insert_before(	test_doublylinkedlist,
    						test_doublylinkedlist->last,
    						g_robot,
    						s_DATA_STRUCT_TYPE_ROBOT);
  • void doublylinkedlist_insert_beginning(doublylinkedlist_t *dlls, void *data, int data_type) - Add a new node in the beginning of the list.

    Example:

    		// Insert a node in the beginning of the list
    		doublylinkedlist_insert_beginning(test_doublylinkedlist,
    						  g_robot,
    						  s_DATA_STRUCT_TYPE_ROBOT);
  • void doublylinkedlist_insert_end(doublylinkedlist_t *dlls, void *data, int data_type) - Add a new node in the end of the list.

    Example:

    		// Insert a node in the end of the list
    		doublylinkedlist_insert_end(test_doublylinkedlist,
    					    g_robot,
    					    s_DATA_STRUCT_TYPE_ROBOT);
  • void doublylinkedlist_remove(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int *data_type) - Remove a node and retrieve the data from the node in the list.

    Example:

    		// Define variables
    		robot_t robot;
    		int data_type;
    		// Remove first node in the list and save the data
    		doublylinkedlist_remove(test_doublylinkedlist,
    					test_doublylinkedlist->first,
    					&robot,
    					&data_type);
  • void doublylinkedlist_delete(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node) - Delete a node from the list.

    Example:

    		// Delete first node in the list
    		doublylinkedlist_remove(test_doublylinkedlist,
    					test_doublylinkedlist->first);
  • void doublylinkedlist_empty(doublylinkedlist_t *dlls) - Empty the list.

    Example:

    		// Empty doubly linked list
    		doublylinkedlist_empty(test_doublylinkedlist);

Page responsible: Simin Nadjm-Tehrani
Last updated: 2023-11-01