Data Structures | Assignments

Home Embedded Systems Data Structures Data Structures | Assignments

Data Structures - Assignments

In Emertxe each one of our Data Structures Algorithm Examples will ensure you apply the concept you have leant in your classroom. By solving these Data Structures Exercises, you will go through a systematic problem-solving approach which include requirement understanding, algorithm design, pseudocode creation, dry run and final execution. As you move from simple to more complex data Structures Exercises of each module it will slowly start building your self-confidence. 

Chapter 1 : Single Linked List

TitleInsert After
Filenamesl_insert_after.c
DescriptionWrite a function to insert the new data after the given data using Data Structure Algorithm Examples
Cases
  1. List Empty
  2. List Non-Empty
    1. Given data present
    2. Given data not present
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
Given data present
Case-2
Sub-case:2
List Non-Empty
Given data not present
ExampleIf given_data = 40, new_data = 45ExampleIf given_data = 60, new_data = 45
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 30 -> 40 -> 45 -> 50OutputReturn DATA_NOT_FOUND
Prototype

int sl_insert_after(Slist *head, data_t g_data, data_t n_data);

head: Pointer to the first node
g_data: Given data
n_data: New data to be inserted into the list
return value: status (LIST_EMPTY, SUCCESS, DATA_NOT_FOUND)
TitleInsert Before
Filenamesl_insert_before.c
DescriptionWrite a function to insert the new data before the given data.
Cases
  1. List Empty
  2. List Non-Empty
    1. Given data present
    2. Given data not present
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
Given data present
Case-2
Sub-case:2
List Non-Empty
Given data not present
ExampleIf given_data = 40, new_data = 45ExampleIf given_data = 60, new_data = 45
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 30 -> 45 -> 40 -> 50OutputReturn DATA_NOT_FOUND
Case-3Data found in the first
ExampleIf given_data = 10, new_data = 45
Input10 -> 20 -> 30 -> 40 -> 50
Output45 -> 10 -> 20 -> 30 -> 40 -> 50
Prototype

int sl_insert_after(Slist **head, data_t g_data, data_t n_data);

 

head: Pointer to the first node
g_data: Given data
n_data: New data to be inserted into the list
return value: status (LIST_EMPTY, SUCCESS, DATA_NOT_FOUND)

 

TitleDelete element
Filenamesl_delete_element.c
DescriptionWrite a function to delete the given data in the single linked list.
Cases
  1. List Empty
  2. List Non-Empty
    1. Given data present
    2. Given data not present
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
Given data present
Case-2
Sub-case:2
List Non-Empty
Given data not present
ExampleIf given_data = 40ExampleIf given_data = 60
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 30 -> 50OutputReturn DATA_NOT_FOUND
Prototype

int sl_insert_after(Slist **head, data_t g_data);

 

head: Pointer to the first node
g_data: Given data
return value: status (LIST_EMPTY, SUCCESS, DATA_NOT_FOUND)

 

TitleInsert at โ€˜nโ€™ position
Filenamesl_insert_nth.c
DescriptionWrite a function to insert the given data exactly at the โ€˜nโ€™ position in the single linked list. incorporating essential data structure algorithm examples for better understanding.
Cases
  1. List Empty
  2. List Non-Empty
    1. Given โ€˜nโ€™th position present
    2. Given โ€˜nโ€™th position not present
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
Given data present
Case-2
Sub-case:2
List Non-Empty
Given data not present
ExampleIf n = 3, n_data = 23ExampleIf n = 10, n_data = 23
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 23 -> 30 -> 50OutputReturn POSITION_NOT_FOUND
Prototype

int sl_insert_after(Slist **head, int n, data_t n_data);

head: Pointer to the first node
n: Position number
n_data: New data, to be inserted into the list
return value: status (LIST_EMPTY, SUCCESS, POSITION_NOT_FOUND)
TitleFind Mid
Filenamesl_find_mid.c
DescriptionWrite a function to find the mid node in the given linked list. Accompained by data structure algorithm example.
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
List contains Odd nodes
Case-2
Sub-case:2
List Non-Empty
List contains Even nodes
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 40 -> 50
Output30Output20
Prototype

int sl_find_mid(Slist *head, Slist **mid);

head: Pointer to the first node
mid: Pointer to the mid node
return value: status (LIST_EMPTY, SUCCESS)
NoteTraverse the linked list only once
TitleGet nth node from last
Filenamesl_get_nth_from_last.c
DescriptionWrite a function to get the nth node from the end of the linked list. a common problem in data structure algorithms examples.
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2List Non-Empty
Input10 -> 20 -> 30 -> 40 -> 50, n = 2
Output40 (From the last, second node conatins the data 40)
Prototype

int sl_get_nth_from_last(Slist *head, int n);

head: Pointer to the first node
g_data: Given data
n: Position from the last node
return value: status (LIST_EMPTY, SUCCESS)
TitleInsert Sorted
Filenamesl_insert_sorted.c
DescriptionWrite a function to insert the new data in the already sorted linked list
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2List Non-Empty
ExampleIf n_data = 45
Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 30 -> 40 -> 45 -> 50
Prototype

int sl_insert_after(Slist **head, data_t n_data);

 

head: Pointer to the first node
n_data: New data to be inserted into the sorted list
return value: status (LIST_EMPTY, SUCCESS)
TitleFind loop
Filenamesl_find_loop.c
DescriptionWrite a function to detect whether the given linked list is ending with loop or not incorporating data structure algorithm examples to illustrate practical implementation
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-Case:1
List Non-Empty
Input
OutputLoop detected.
Case-2
Sub-Case:2
List Non-Empty
Input
OutputLoop not detected.
Prototype

int sl_insert_after(Slist *head);

head: Pointer to the first node
return value: status (LIST_EMPTY, LOOP_DETECTED, LOOP_NOT_DETECTED)
NoteTraverse the linked list only once
TitleSort
Filenamesl_sort.c
DescriptionWrite a function to sort the given single linked list.
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2List Non-Empty
Input50 -> 40 -> 30 -> 20 -> 10
Output10 -> 20 -> 30 -> 40 -> 50
Prototype

int sl_sort(Slist **head);

 

head: Pointer to the first node
return value: status (LIST_EMPTY, SUCCESS)
NoteDonโ€™t swap the data present in the nodes, swap the nodes itself.

 

TitleReverse
Filenamesl_reverse_iterative.c
sl_reverse_recursive.c
DescriptionWrite a function to reverse the single linked list in both iterative and recursive methods
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2List Non-Empty
Input50 -> 40 -> 30 -> 20 -> 10
Output10 -> 20 -> 30 -> 40 -> 50
Prototype

int sl_sort(Slist **head);

 

head: Pointer to the first node
return value: status (LIST_EMPTY, SUCCESS)
TitleReverse
Filenamesl_reverse_iterative.c
sl_reverse_recursive.c
DescriptionWrite a function to reverse the single linked list in both iterative and recursive methods, incorporating data structure algorithm examples to illustrate the implementation process.
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2List Non-Empty
Input50 -> 40 -> 30 -> 20 -> 10
Output10 -> 20 -> 30 -> 40 -> 50
Prototype

int sl_sort(Slist **head);

head: Pointer to the first node
return value: status (LIST_EMPTY, SUCCESS)
TitleRemove Duplicates
Filenamesl_remove_duplicates.c
DescriptionWrite a function to remove the duplicates data present in the single linked list, implementing a data structure algorithm example for efficient removal of duplicates
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2List Non-Empty
Input5 -> 3 -> 4 -> 5 -> 2 -> 1 -> 4 -> 5 -> 3
Output5 -> 3 -> 4 -> 2 -> 1
Prototype

int sl_sort(Slist **head);

head: Pointer to the first node
return value: status (LIST_EMPTY, SUCCESS)
TitleSorted Merge
Filenamesl_sorted_merge.c
DescriptionWrite a function to merge two linked list into one
Cases
  1. List Empty
  2. List Non-Empty
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2List Non-Empty
InputList โ€“ 1
30 -> 10 -> 50
List โ€“ 2
20 -> 5 -> 35
Output5 -> 10 -> 20 -> 30 -> 35 -> 50
Prototype

int sl_sort(Slist **head1, Slist **head2);

 

head1: Pointer to the first node of the first linked list
head2: Pointer to the first node of the second linked list
return value: status (LISTS_EMPTY, SUCCESS)
Note

 

  1. If lists are not sorted, sort it
  2. If second list is EMPTY, no need to append it.
  3. If first list is EMPTY, update head1 to the head2
  4. If both list are EMPTY, return LISTS_EMPTY

Chapter 2 : Double Linked List

TitleInsert After
Filenamedl_insert_after.c
DescriptionWrite a function to insert the new data after the given data.
Cases
  1. List Empty
  2. List Non-Empty
    1. Given data present
    2. Given data not present
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
Given data present
Case-2
Sub-case:2
List Non-Empty
Given data not present
ExampleIf given_data = 40, new_data = 45ExampleIf given_data = 60, new_data = 45
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 30 -> 40 -> 45 -> 50OutputReturn DATA_NOT_FOUND
Prototype

int dl_insert_after(Dlist *head, data_t g_data, data_t n_data);

 

head: Pointer to the first node
g_data: Given data
n_data: New data to be inserted into the list
return value: status (LIST_EMPTY, SUCCESS, DATA_NOT_FOUND)
TitleInsert Before
Filenamedl_insert_before.c
DescriptionWrite a function to insert the new data before the given data.
Cases
  1. List Empty
  2. List Non-Empty
    1. Given data present
    2. Given data not present
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
Given data present
Case-2
Sub-case:2
List Non-Empty
Given data not present
ExampleIf given_data = 40, new_data = 45ExampleIf given_data = 60, new_data = 45
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 30 -> 45 -> 40 -> 50OutputReturn DATA_NOT_FOUND
Case-3Data found in the first
ExampleIf given_data = 10, new_data = 45
Input10 -> 20 -> 30 -> 40 -> 50
Output45 -> 10 -> 20 -> 30 -> 40 -> 50
Prototype

int sl_insert_after(Dlist **head, data_t g_data, data_t n_data);

 

head: Pointer to the first node
g_data: Given data
n_data: New data to be inserted into the list
return value: status (LIST_EMPTY, SUCCESS, DATA_NOT_FOUND)
TitleDelete element
Filenamedl_delete_element.c
DescriptionWrite a function to delete the given data in the double linked list.
Cases
  1. List Empty
  2. List Non-Empty
    1. Given data present
    2. Given data not present
Case-1List Empty
InputHead = NULL
Outputreturn LIST_EMPTY
Case-2
Sub-case:1
List Non-Empty
Given data present
Case-2
Sub-case:2
List Non-Empty
Given data not present
ExampleIf given_data = 40ExampleIf given_data = 60
Input10 -> 20 -> 30 -> 40 -> 50Input10 -> 20 -> 30 -> 40 -> 50
Output10 -> 20 -> 30 -> 50OutputReturn DATA_NOT_FOUND
Prototype

int sl_insert_after(Dlist **head, data_t g_data);

 

head: Pointer to the first node
g_data: Given data
return value: status (LIST_EMPTY, SUCCESS, DATA_NOT_FOUND)

Chapter 3 : Stacks

TitleInfix to Postfix
Filenameinfix_postfix.c
DescriptionWrite a function to convert the given infix expression into the postfix form
Cases
  1. Single digit numbers
  2. Multiple digit numbers
Case-1Single digit numbers
Input2 * 3 โ€“ 3 + 8 / 4 / (1 + 1)
Output2 3 * 3 โ€“ 8 4 / 1 1 + / +
Case-2Multiple digit numbers
Input2 * 30 โ€“ 3 + 8 / 4 / (10 + 1)
Output2, 30, *, 3, โ€“, 8, 4, /, 10, 1, +, / ,+
Sample Prototype

Char * infix_postfix(char *infix);

 

infix: Pointer the base address of the infix array
return: Pointer the base address of the postfix array
TitlePostfix Evaluation
Filenamepostfix_eval.c
DescriptionWrite a function to evaluate the postfix expression.
Cases
  1. Single digit numbers
  2. Multiple digit numbers
Case-1Single digit numbers
Input2 3 * 3 โ€“ 8 4 / 1 1 + / +
Output4
Case-2Multiple digit numbers
Input2, 30, *, 3, โ€“, 8, 4, /, 10, 1, +, / ,+
Output57.181
Sample Prototype

double infix_postfix(char *postfix);

 

Postfix: Pointer the base address of the postfix array
return: Result of the expression in double.
TitleInfix to Prefix
Filenameinfix_prefix.c
DescriptionWrite a function to convert the given infix expression into the prefix form
Cases
  1. Single digit numbers
  2. Multiple digit numbers
Case-1Single digit numbers
Input2 * 3 โ€“ 3 + 8 / 4 / (1 + 1)
Output+ โ€“ * 2 3 3 / / 8 4 + 1 1
Case-2Multiple digit numbers
Input2 * 30 โ€“ 3 + 8 / 4 / (10 + 1)
Output+, -, *, 2, 30, 3, /, /, 8, 4, +, 10, 1
Sample Prototype

Char * infix_prefix(char *infix);

 

infix: Pointer the base address of the infix array
return: Pointer the base address of the prefix array
TitlePrefix Evaluation
Filenameprefix_eval.c
DescriptionWrite a function to evaluate the prefix expression.
Cases
  1. Single digit numbers
  2. Multiple digit numbers
Case-1Single digit numbers
Input+ โ€“ * 2 3 3 / / 8 4 + 1 1
Output4
Case-2Multiple digit numbers
Input+, -, *, 2, 30, 3, /, /, 8, 4, +, 10, 1
Output57.181
Sample Prototype

double infix_postfix(char *prefix);

 

Prefix: Pointer the base address of the prefix array
return: Result of the expression in double.

Chapter 4 : Sorting

TitleBubble Sort
Filenamebubble.c
DescriptionWrite a function to sort given array using bubble sort
Input5 4 3 2 1
Output1 2 3 4 5
Sample Prototype

void bubble_sort(int *ptr)

 

ptr: Base address of the integer array
return: void
TitleInsertion Sort
Filenameinsertion.c
DescriptionWrite a function to sort given array using insertion sort
Input5 4 3 2 1
Output1 2 3 4 5
Sample Prototype

void insertion_sort(int *ptr)

 

ptr: Base address of the integer array
return: void
TitleSelection Sort
Filenameselection.c
DescriptionWrite a function to sort given array using selection sort
Input5 4 3 2 1
Output1 2 3 4 5
Sample Prototype

void selection_sort(int *ptr)

 

ptr: Base address of the integer array
return: void

Chapter 5 : Trees

TitleDelete nodes
Filenamebst_delete_node.c
DescriptionWrite a function to delete the given data from the bst
Cases

Node to be deleted may be,

  1. Leaf node
  2. Node with single child
  3. Node with two children
Case-1Leaf node
Input  
Output
Case-2Node with single child
Input
OutputAfter deleting 30
Case-3Node with two children
Input
OutputAfter deleting 50
TitleFind height
Filenamebst_find_height.c
DescriptionWrite a function to find the height of the given tree
Cases
  1. Tree Empty
  2. Tree Non-Empty
Case-1Tree Non-Empty
Input
OutputHeight = 2

Chapter 6 : Hashing

TitleCreate hash table
Filenamecreate_hash_table.c
DescriptionWrite a function to create the hash table
InputSize
OutputHash table with the given size
TitleHash table search
Filenamehash_search.c
DescriptionWrite a function to search the data in the hash table
InputHash Table
OutputAddress of the node where data is found in the hash table
TitleHash table insert
Filenamehash_insert.c
DescriptionWrite a function to insert the data in the hash table
InputHash Table
OutputStatus [SUCCESS / FAILURE]
TitleHash delete element
Filenamehash_delete_element.c
DescriptionWrite a function to delete the given item from the hash table
InputHash Table
OutputStatus [SUCCESS / FAILURE]
TitleHash Table delete
Filenamehash_table_delete.c
DescriptionWrite a function to delete the entire hash table
InputHash Table
OutputStatus [SUCCESS / FAILURE]