diff --git a/Assignment1/diff.c b/Assignment1/diff.c new file mode 100644 index 0000000..950f625 --- /dev/null +++ b/Assignment1/diff.c @@ -0,0 +1,48 @@ +#include + +/*Step 5 : */ + +/*Return 0 if ds1 and ds2 are clones, + * else return -1*/ +int +diff (void *root1, /*pointer to Data structure 1*/ + void *root2, /*pointer to data structure 2*/ + void *(*iterator(void *)), /*Iterator function callback*/ + int comparator(void *, void *), + void *(*get_app_data(void *))){ /*Comparison function callback*/ + + /*Write your code here*/ + + void *data1; + void *data2; + + void *temp1 = root1 , *temp2 = root2; + + if( !root1 && root2 ) + return -1; + else if( root1 && !root2 ) + return -1; + + if( comparator(get_app_data(temp1),get_app_data(temp2))) + return -1; + + while( temp1 != NULL && temp2 != NULL ){ + + data1 =get_app_data(temp1); + data2 = get_app_data(temp2); + + if( comparator(data1,data2) == -1 ){ + return -1; + } + + temp1 = iterator(temp1); + temp2 = iterator(temp2); + } + + if( temp1 == NULL && temp2 != NULL ) + return -1; + else if( temp1 != NULL && temp2 == NULL ) + return -1; + + return 0; +} diff --git a/Assignment1/diff.o b/Assignment1/diff.o new file mode 100644 index 0000000..2131186 Binary files /dev/null and b/Assignment1/diff.o differ diff --git a/Assignment1/dll.c b/Assignment1/dll.c new file mode 100644 index 0000000..287e2a7 --- /dev/null +++ b/Assignment1/dll.c @@ -0,0 +1,40 @@ +#include "dll.h" +#include +#include + +/* Public Function Implementation to create and return + * new empty doubly linked list*/ + +dll_t * +get_new_dll(){ + + dll_t *dll = calloc(1, sizeof(dll_t)); + dll->head = NULL; + return dll; +} + +/* Public Function Implementation to add a new application + * * data to DLL */ +int +add_data_to_dll (dll_t *dll, void *app_data){ + + if(!dll || !app_data) return -1; + + dll_node_t *dll_new_node = calloc(1, sizeof(dll_node_t)); + dll_new_node->left = NULL; + dll_new_node->right = NULL; + dll_new_node->data = app_data; + + /*Now add this to the front of DLL*/ + if(!dll->head){ + dll->head = dll_new_node; + return 0; + } + + dll_node_t *first_node = dll->head; + dll_new_node->right = first_node; + first_node->left = dll_new_node; + dll->head = dll_new_node; + return 0; +} + diff --git a/Assignment1/dll.h b/Assignment1/dll.h new file mode 100644 index 0000000..3737610 --- /dev/null +++ b/Assignment1/dll.h @@ -0,0 +1,23 @@ +/*Header file for Doubly Linked List*/ + +typedef struct dll_node_{ + + void *data; + struct dll_node_ *left; + struct dll_node_ *right; +} dll_node_t; + +typedef struct dll_{ + dll_node_t *head; +} dll_t; + + +/* Public Function declaration to create and return + * a new empty doubly linked list*/ +dll_t * +get_new_dll(); + +/* Public Function declaration to add the appication + * data to DLL*/ +int +add_data_to_dll(dll_t *dll, void *appn_data); diff --git a/Assignment1/dll.o b/Assignment1/dll.o new file mode 100644 index 0000000..3c11763 Binary files /dev/null and b/Assignment1/dll.o differ diff --git a/Assignment1/evaluation.c b/Assignment1/evaluation.c new file mode 100644 index 0000000..d6065a2 --- /dev/null +++ b/Assignment1/evaluation.c @@ -0,0 +1,167 @@ +#include "dll.h" +#include +#include +#include + +extern int +diff (void *root1, /*pointer to first node of data struture 1*/ + void *root2, /*pointer to first node of data struture 2*/ + void *(*iterator)(void *), /*Iterator function callback*/ + int (*comparator)(void *, void *), + void *(*get_app_data)(void *)); /*Comparison function callback*/ + + +/*Application specific data structures*/ +typedef struct student_{ + char name[32]; + unsigned int year_of_birth; + unsigned int height; + unsigned int weight; + unsigned int total_marks; +} student_t; + + +/*Step 2 : Implement list iterator function here*/ +void* list_iterator (void *list_node){ + + /*write your code here*/ + + dll_node_t *_node = (dll_node_t *)list_node; + + if( _node == NULL ) return NULL; + + + return (void *)_node->right; +} + +/*Step 3 : implement student comparator function here*/ + +int student_comparator (void *_studentobj1, void *_studentobj2){ + + /*Write your code here*/ + student_t *_stu1 = (student_t *)_studentobj1; + student_t *_stu2 = (student_t *)_studentobj2; + + if( !_stu1 && !_stu2 ) + return 0; + else if( !_stu1 && _stu2 ) + return -1; + else if( _stu1 && !_stu2 ) + return -1; + + + if( strcmp(_stu1->name,_stu2->name) != 0 ) + return -1; + + if( _stu1->year_of_birth != _stu2->year_of_birth ) + return -1; + + if( _stu1->height != _stu2->height ) + return -1; + + if( _stu1->weight != _stu2->weight ) + return -1; + + if( _stu1->total_marks != _stu2->total_marks ) + return -1; + + + return 0; +} + +/*Step 4 : implement get_app_data_from_list_node function*/ +void* +get_app_data_from_list_node(void *list_node){ + + /*Write your code here*/ + + dll_node_t *_node = (dll_node_t *)list_node; + if( _node == NULL ) + return NULL; + + return (void *)_node->data; + } + +int +main(int argc, char **argv){ + + /*Student database list 1*/ + student_t *student1 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student1->name, "Abhishek", strlen("Abhishek")); + student1->year_of_birth = 2000; + student1->height = 167; + student1->weight = 80; + student1->total_marks = 100; + + student_t *student2 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student2->name, "Sagar", strlen("Sagar")); + student2->year_of_birth = 2001; + student2->height = 170; + student2->weight = 82; + student2->total_marks = 80; + + student_t *student3 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student3->name, "Vineet", strlen("Vineet")); + student3->year_of_birth = 2002; + student3->height = 161; + student3->weight = 62; + student3->total_marks = 72; + + student_t *student4 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student4->name, "Neeraj", strlen("Neeraj")); + student4->year_of_birth = 1998; + student4->height = 167; + student4->weight = 76; + student4->total_marks = 67; + + + dll_t *student_db1 = get_new_dll(); + add_data_to_dll(student_db1, student1); + add_data_to_dll(student_db1, student2); + add_data_to_dll(student_db1, student3); + add_data_to_dll(student_db1, student4); + + + /*Student database list 2*/ + student_t *student5 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student5->name, "Abhishek", strlen("Abhishek")); + student5->year_of_birth = 2000; + student5->height = 167; + student5->weight = 80; + student5->total_marks = 90; + + student_t *student6 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student6->name, "Sagar", strlen("Sagar")); + student6->year_of_birth = 2001; + student6->height = 170; + student6->weight = 82; + student6->total_marks = 80; + + student_t *student7 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student7->name, "Vineet", strlen("Vineet")); + student7->year_of_birth = 2002; + student7->height = 161; + student7->weight = 62; + student7->total_marks = 72; + + student_t *student8 = (student_t *)calloc(1, sizeof(student_t)); + strncpy(student8->name, "Neeraj", strlen("Neeraj")); + student8->year_of_birth = 1998; + student8->height = 167; + student8->weight = 76; + student8->total_marks = 67; + + dll_t *student_db2 = get_new_dll(); + add_data_to_dll(student_db2, student5); + add_data_to_dll(student_db2, student6); + add_data_to_dll(student_db2, student7); + add_data_to_dll(student_db2, student8); + + /*Step 6*/ + + if(diff((void *)student_db1->head, (void *)student_db2->head, list_iterator, student_comparator, get_app_data_from_list_node) == 0) + printf("Data sructures are equal\n"); + else + printf("Data sructures are not equal\n"); + return 0; +} diff --git a/Assignment1/evaluation.o b/Assignment1/evaluation.o new file mode 100644 index 0000000..379eb76 Binary files /dev/null and b/Assignment1/evaluation.o differ diff --git a/Assignment1/exeapp b/Assignment1/exeapp new file mode 100755 index 0000000..4abbafd Binary files /dev/null and b/Assignment1/exeapp differ diff --git a/Assignment1/readme b/Assignment1/readme new file mode 100644 index 0000000..a699d0e --- /dev/null +++ b/Assignment1/readme @@ -0,0 +1,55 @@ +In this assignment, We will write a generic diff function diff() which can tell whether the two similar Data structures are clones of each other or not. Two similar Data structures could be two linked list, two trees or anything.  diff() should be generic and should work for all data structures. + +Instructions : +Create two new Linked lists : list1 and list2. See evaluation.c file, in main(), i have created two lists for you already called student_db1 and student_db2. +list1 and list2 both holds the records of type student_t. student_t is defined as : + +typedef struct student_{ + char name[32]; + unsigned int year_of_birth; + unsigned int height; + unsigned int weight; + unsigned int total_marks; +} student_t; + + +Step 1 (Done): Create two linked list and add some records in both list. I have done it for you in evaluation.c file. I have used library dll.h/.c which you have already created so far. + + +Step 2 : Now write an iterator function called "void* list_iterator (void *list_node)" in evaluation.c file. which takes an input a pointer to node of a linked list, and return the pointer to next node. Signature of function should be generic as below. Handle NULL cases. + Signature : void* list_iterator (void *list_node); + + + +Step 3 : Write a comparator function called "int student_comparator (void *_studentobj1, void *_studentobj2)" in evaluation.c file which takes an input the pointer to objects of type student_t and return 0 if two objects are equal , otherwise -1. The signature of function should be generic as below : + Signature : int student_comparator (void *studentobj1, void *studentobj2); + + + +Step 4 : Write a function called "void * get_app_data_from_list_node(void *list_node)" in file evaluation.c which takes an input the pointer to node of linked list and return the object to data held by the node of the linked list. + Signature : void *get_app_data_from_list_node(void *list_node); + + +Step 5 : Now write a diff function in diff.c file. Signature of the function should be generic as below : + + int  /*return 0 if two Data structures are equal, else -1*/ + diff (void *root1,    /*pointer to starting node of list 1*/ +       void *root2,    /*pointer to starting node of list 2*/ +       void *(*iterator(void *)), /*Iterator function callback*/ +       int comparator(void *, void *), /*Comparison function callback*/ + void *(*get_app_data(void *))); /*get application data function callback*/ + + + +Step 6 : you should call the diff() as follows in evaluation.c file : + +if(diff (first_node1, first_node2, list_iterator, student_comparator, get_app_data_from_list_node) == 0) + printf("Data structures are equal\n"); +else + printf("Data structures are not equal\n"); + + +Step 7 : Compile and execute the final executable. Your output should be "Data structures are equal". +Step 8 : Modify the Student list as per your convinience and verify that you get appropriate output. + +Step 9 : Repeat the above exercise for trees. You should NOT modify diff() that you have written in step 5, it should work for tree data structures as well.