DS Assignments
DS Assignments
Assignment – 1
1. insert_at_first(head, data)
2. insert_at_last(head, data)
3. delete_first(head)
4. delete_last(head)
5. delete_list(head)
6. find_node(head, data)
1. insert_at_first :
Read data from user and insert the given data in first position.
40 → 10 → 20 → 30 → NULL
2. Insert_at_last :
3. delete_first :
1. List empty → Return LIST_EMPTY (in empty list node can’t be deleted)
2. List not empty → Update the head with next node address, delete the first node.
Example :
head → 10 → 20 → 30 → 40 → 50 → NULL
head 10 → 20 → 30 → 40 → 50 → NULL
head → 20 → 30 → 40 → 50 → NULL
4. delete_last
1. List empty → Return LIST_EMPTY (in empty list node can’t be deleted)
2. List not empty → Traverse to the last node, update the previous node address and delete
the last node.
5. delete_list
1. List empty → Return LIST_EMPTY (in empty list node can’t be deleted)
2. List not empty → Delete all nodes one by one.
NOTE : Should not update head directly with NULL without freeing the nodes.
6. find_node
Output Images :
Assignment – 2
1. insert_after :
2. insert_before :
3. delete_element :
Description Write a function to delete the given data in the single linked list.
1. List Empty
2. List Non-Empty
Cases
1. Given data present
2. Given data not present
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty Case-2 List Non-Empty
Sub-case:1 Given data present Sub-case:2 Given data not present
Example If given_data = 40 Example If given_data = 60
Input 10 → 20 → 30 → 40 → 50 Input 10 → 20 → 30 → 40 → 50
Output 10 → 20 → 30 → 50 Output Return DATA_NOT_FOUND
int sl_delete_element(Slist **head, data_t g_data);
4. insert_Nth :
Descriptio Write a function to insert the given data exactly at the ‘n’ position in
n the single linked list.
1. List Empty
2. List Non-Empty
Cases
1. Given ‘n’th position present
2. Given ‘n’th position not present
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2
List Non-Empty Case-2 List Non-Empty
Sub-
Given data present Sub-case:2 Given data not present
case:1
Example If n = 3, n_data = 23 Example If n = 10, n_data = 23
Input 10 → 20 → 30 → 40 → 50 Input 10 → 20 → 30 → 40 → 50
Output 10 → 20 → 23 → 30 → 50 Output Return POSITION_NOT_FOUND
int sl_insert_Nth(Slist **head, data_t ndata, int n);
Output Images :
Assignment – 3
1. dll_insert_at_first :
Read data from user and insert the given data in first position.
head = NULL, tail = NULL , After inserting data into list, list contains
head → 10 ← tail
head and tail should be updated with new node address
head → 40 ↔ 10 ↔ 20 ↔ 30 ← tail
2. dll_Insert_at_last :
1. List empty – Update the head and tail with new node address.
2. List not empty – Establish the link between last node and new node using tail pointer.
3. dll_delete_first :
1. List empty → Return LIST_EMPTY (in empty list node can’t be deleted)
2. List not empty → Update the head with next node address, delete the first node.
Example :
head → 10 → 20 → 30 → 40 → 50 → NULL
head 10 → 20 → 30 → 40 → 50 → NULL
head → 20 → 30 → 40 → 50 → NULL
4. dll_delete_last
1. List empty → Return LIST_EMPTY (in empty list node can’t be deleted)
2. List not empty → Update the previous node address in tail pointer, and update link of
previous node and delete the last node.
5. delete_list
1. List empty → Return LIST_EMPTY (in empty list node can’t be deleted)
2. List not empty → Delete all nodes one by one from head or tail.
NOTE : Should not update head and tail directly with NULL without freeing the
nodes.
Output Images :
Assignment – 4
1. dll_insert_after :
Descriptio
Write a function to insert the new data after the given data.
n
1. List Empty
2. List Non-Empty
Cases
1. Given data present
2. Given data not present
Case-1 List Empty
Input head = NULL, tail = NULL
Output return LIST_EMPTY
Case-2
Case-2 List Non-Empty List Non-Empty
Sub-
Sub-case:1 Given data present Given data not present
case:2
If given_data = 40, new_data = If given_data = 60, new_data =
Example Example
45 45
Input 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50 Input 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50
Output 10 ↔ 20 ↔ 30 ↔ 40 ↔ 45 ↔ 50 Output Return DATA_NOT_FOUND
int dl_insert_after(Dlist *head, Dlist *tail, data_t gdata, data_t ndata);
2. dll_insert_before
Descriptio
Write a function to insert the new data before the given data.
n
Cases 1. List Empty
2. List Non-Empty
1. Given data present
2. Given data not present
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2
Case-2 List Non-Empty List Non-Empty
Sub-
Sub-case:1 Given data present Given data not present
case:2
If given_data = 40, new_data = If given_data = 60, new_data =
Example Example
45 45
Input 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50 Input 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50
Output 10 ↔ 20 ↔ 30 ↔ 45 ↔ 40 ↔ 50 Output Return DATA_NOT_FOUND
Case-3 Data found in the first
Example If given_data = 10, new_data = 45
Input 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50
Output 45 ↔ 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50
int dl_insert_before(Dlist **head, data_t gdata, data_t ndata);
3. dll_delete_element
Description Write a function to delete the given data in the double linked list.
1. List Empty
2. List Non-Empty
Cases
1. Given data present
2. Given data not present
Case-1 List Empty
Input head = NULL, tail = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty Case-2 List Non-Empty
Sub-case:1 Given data present Sub-case:2 Given data not present
Example If given_data = 40 Example If given_data = 60
Input 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50 Input 10 ↔ 20 ↔ 30 ↔ 40 ↔ 50
Output 10 ↔ 20 ↔ 30 ↔ 50 Output Return DATA_NOT_FOUND
int dl_delete_element(Dlist **head, Dlist **tail, data_t g_data);
Assignment – 5
1. sl_find_mid(head, mid)
2. sl_get_Nth_last(head, n, data)
1. sl_find_mid
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty Case-2 List Non-Empty
Sub-case:1 List contains Odd nodes Sub-case:2 List contains Even nodes
Input 10 -> 20 -> 30 -> 40 -> 50 Input 10 -> 20 -> 40 -> 50
Output 30, return SUCCESS Output 20 (or) 40, return SUCCESS
int sl_find_mid(Slist *head, int * mid);
2. sl_get_Nth_last
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty
Input 10 -> 20 -> 30 -> 40 -> 50, n = 2
Output 40 (From the last, second node conatins the data 40)
int sl_get_nth_from_last(Slist *head, int n, int *data);
Output Images :
Assignment – 6
1. insert_sorted(head, ndata)
2. find_loop(head)
1. insert_sorted
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL, ndata = 10
Output 10
Case-2 List Non-Empty
Example If ndata = 45
Input 10 → 20 → 30 → 40 → 50
Output 10 → 20 → 30 → 40 → 45 → 50
int sl_insert_sorted(Slist **head, data_t n_data);
2. sl_find_loop
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2
Sub- List Non-Empty
Case:1
Input
Prototype
head : Pointer to the first node
return value : status (LIST_EMPTY, LOOP_DETECTED, LOOP_NOT_DETECTED)
Note Traverse the linked list only once
Output Images :
Assignment – 7
1.sl_sort(head)
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty
Input 50 → 40 → 30 → 20 → 10
Output 10 → 20 → 30 → 40 → 50
int sl_sort(Slist **head);
Prototype
head : Pointer to the first node
return value : status (LIST_EMPTY, SUCCESS)
Note Don’t swap the data present in the nodes, swap the nodes itself.
Output Images :
Assignment – 8
1. sl_reverse_iterative(head)
2. sl_reverse_recursive(head)
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty
Input 50 → 40 → 30 → 20 → 10
Output 10 → 20 → 30 → 40 → 50
int sl_reverse_iterative/recursive(Slist **head);
Prototype
head : Pointer to the first node
return value : status (LIST_EMPTY, SUCCESS)
Output Images :
Assignment – 9
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty
Input 5→3→4→5→2→1→4→5→3
Output 5→3→4→2→1
int sl_remove_duplicates(Slist **head);
Prototype head : Pointer to the first node
return value : status (LIST_EMPTY, SUCCESS)
Sample Output :
SL List → 1 → 2 → 4 → 2 → 5 → 4 → 3 → 1 → 2
Output → 1 → 2 → 4 → 5 → 3
Assignment – 10
1. List Empty
Cases
2. List Non-Empty
Case-1 List Empty
Input Head = NULL
Output return LIST_EMPTY
Case-2 List Non-Empty
List – 1
30 → 10 → 50
Input
List – 2
20 → 5 → 35
Output 5 → 10 → 20 → 30 → 35 → 50
int sl_sorted_merge(Slist **head1, Slist **head2);
Prototype 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)
1. If second list is EMPTY, no need to append it.
2. If first list is EMPTY, update head1 to the head2
Note
3. If both list are EMPTY, return LISTS_EMPTY
4. Finally sort the list
Output Images :
Assignment – 11 & 12
Implement the stack using array and linked list:
1. push(stack, data)
2. pop(stack)
3. peek(stack, data)
4. peep(stack)
1. Push (insert)
Inputs : stack → Pointer that contains address of structure variable (array)
stack → Pointer to the first node (LL)
data → Data to be inserted into stack
Cases :
1. Stack Full → Return STACK_FULL
2. Stack not full → Push the given data into stack
2. Pop (delete)
Inputs : stack → Pointer that contains address of structure variable (array)
stack → Pointer to the first node (LL)
Cases :
1. Stack empty → Return STACK_EMPTY
2. Stack not empty → Pop (delete) the top of the data from the stack.
3. Peek int peek(stack_t *stk, int *data);
Inputs : stack → pointer that contains address of the structure variable (array)
stack → Pointer to the first node (LL).
Data → integer pointer
Cases :
1. Stack empty → Return STACK_EMPTY
2. Stack not empty → Store the top most value in pointer variable and return
SUCCESS.
Peep :
Output Images :
Assignment – 13
2. Postfix evaluation
Assignment – 14
2. Prefix Evaluation
Output Images :
Assignment – 15 & 16 (Circular queue)
1. Enqueue :
2. Dequeue :
3. Front_data :
Output Images :
Assignment – 17
Objective :
To understand the working of Binary Search
Requirements :
1.Prompt the user to Enter the size of the Array and read it
2.Declare an integer array and read the elements of array
3.Prompt the user to Enter the key element to search and read it
4.Call the binary search function to search the element
Sample Output :
./a.out
./a.out
WAF to sort given array using bubble sort, insertion sort and selection
sort
Objective :
To understand the working of Sorting Techniques
Requirements :
1.Prompt the user to Enter the size of the Array and read it
2.Declare an integer array and read the elements of array
3.Call the different sort function to sort the element of the array
4.Display the sorted elements of array
Bubble Sort :
Title Bubble Sort
Filename bubble.c
Description Write a function to sort given array using bubble sort
Input 54321
Output 12345
void bubble_sort(int *ptr)
Sample
Prototype ptr Base address of the integer array
return void
Insertion Sort :
Title Insertion Sort
Filename insertion.c
Description Write a function to sort given array using Insertion sort
Input 54321
Output 12345
void insertion_sort(int *ptr)
Sample
Prototype ptr Base address of the integer array
return void
Selection Sort :
Title Selection Sort
Filename Selection.c
Description Write a function to sort given array using Selection sort
Input 54321
Output 12345
void selection_sort(int *ptr)
Sample
Prototype ptr Base address of the integer array
return void
Sample Output :
./a.out
Objective:
To understand the working of Quick sort
Requirement:
Quick Sort :
Title Quick Sort
Filename quick_sort.c
Description Write a function to sort given array using quick sort
Input 54321
Output 12345
int quick_sort(int *ptr ,int size)
Sample Output :
./a.out
Objective:
To understand the working of Merge sort
Requirement:
Merge Sort :
Title Merge Sort
Filename merge_sort.c
Description Write a functions to sort given array using merge sort
Input 54321
Output 12345
void merge_sort(int *ptr ,int size)
Sample Output :
./a.out
Enter the size of array : 5
Elements of array :
1 22 3 14 25
The sorted elements :
1 3 14 22 25
Assignment – 21
WAF to implement the function of BST
Objective :
Understand the working of Binary search Tree(BST)
Requirement :
1.Add the insert() to insert elements in the bst
2.Add the inorder() to print the elements of bst
Input
Sample Output :
./a.out
Select the Option:
1.Create BST
2.Print BST
3.Search an element in BST
4.Min and Max element of BST
Choice: 2
BST is Empty
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Search an element in BST
4.Min and Max element of BST
Choice: 3
Enter the Key element to search : 25
BST is Empty
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Search an element in BST
4.Min and Max element of BST
Choice: 1
Enter the 5 element : 10 30 15 5 4
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Search an element in BST
4.Min and Max element of BST
Choice: 3
Enter the Key element to search : 15
15 is present in the BST
Do you want to continue ,if yes then type[y/Y]:N
Sample Output :
./a.out
Select the Option:
1.Create BST
2.Print BST
3.Search an element in BST
4.Min and Max element of BST
Choice: 2
BST is Empty
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Search an element in BST
4.Min and Max element of BST
Choice: 1
Enter the 5 element : 10 30 15 5 4
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Search an element in BST
4.Min and Max element of BST
Choice: 4
Minimum element is 4
Maximum element is 30
Do you want to continue ,if yes then type[y/Y]:N
Assignment – 22
WAF to delete the given data node from the BST
Objective :
Understand the working of Binary search Tree(BST)
Requirement :
1.Add the insert() to insert elements in the bst
2.Add the inorder() to print the elements of bst
Input
Output
Input
After deleting 30
Output
After deleting 50
Output
Sample Output :
./a.out
Select the Option:
1.Create BST
2.Print BST
3.Delete an element of BST
Choice: 2
BST is Empty
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Delete an element of BST
Choice: 3
Enter the Key element to search : 25
BST is Empty ,hence we cannot delete the element 25
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Delete an element of BST
Choice: 1
Enter the 5 element : 10 30 15 5 4
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Delete an element of BST
Choice: 3
Enter the element to delete : 15
15 is deleted in the BST
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create BST
2.Print BST
3.Delete an element of BST
Choice: 2
4 5 10 30
Do you want to continue ,if yes then type[y/Y]:N
Assignment – 23
WAF to find height and total number of nodes in the BST
Objective :
Understand the working of Binary search Tree(BST)
Requirement :
Input
Output Height = 2
Input
Objective:
To understand the working of Heap sort
Requirement:
1.Prompt the user to Enter the size of the Array
2.Declare an integer array and read the elements of array
3.Call Heap sort function
4.Display the sorted elements of array
5.To implement this function we should add dependency functions(ie. buildmaxheap and
maxheapify)
Sample Output :
./a.out
Enter the size of array : 5
Elements of array :
1 22 3 14 25
The sorted elements :
1 3 14 22 25
Assignment – 25
WAF to create hash table, to search data , to insert and delete element
in hash table. Also to delete entire hash table.
Objective:
To understand the working of Hashing
Requirements:
In this assignments create 5 functions
Sample Output
./a.out
Enter the size of Hash table : 5
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 1
Hash table created
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 6
Hash table
[0] → -1
[1] → -1
[2] → -1
[3] → -1
[4] → -1
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 2
Enter the 5 element to add in Hash table : 10 20 33 22 55
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 6
Hash table
[0] → 10 →20 → 55
[1] → -1
[2] → 22
[3] → 33
[4] → -1
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 4
Enter the element to delete : 10
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 6
Hash table
[0] → 20 → 55
[1] → -1
[2] → 22
[3] → 33
[4] → -1
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 5
Hash table Deleted
Do you want to continue ,if yes then type[y/Y]:Y
Select the Option:
1.Create Hash table
2.Insert element in Hash table
3.Search an element in Hash table
4.Delete an element from Hash table
5.Delete Entire Hash table
6.Print the elements of Hash table
Choice: 6
Hash table
[0] → -1
[1] → -1
[2] → -1
[3] → -1
[4] → -1
Do you want to continue ,if yes then type[y/Y]:N