mirror of
https://github.com/Hizenberg469/C1-Linux_SYS_Prog-AS-.git
synced 2026-04-19 18:32:24 +03:00
Make a habit to use Iterative Macro for iterating Data Structure
This commit is contained in:
BIN
Iterative_macros/.Makefile.un~
Normal file
BIN
Iterative_macros/.Makefile.un~
Normal file
Binary file not shown.
BIN
Iterative_macros/.tree.c.un~
Normal file
BIN
Iterative_macros/.tree.c.un~
Normal file
Binary file not shown.
BIN
Iterative_macros/.tree.h.un~
Normal file
BIN
Iterative_macros/.tree.h.un~
Normal file
Binary file not shown.
13
Iterative_macros/Makefile
Normal file
13
Iterative_macros/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
TARGET: exe
|
||||
libdll.a: dll.o
|
||||
ar rs libdll.a dll.o
|
||||
dll.o: dll.c
|
||||
gcc -g -c -I . dll.c -o dll.o
|
||||
exe: application.o libdll.a
|
||||
gcc -g application.o -o exe -L . -ldll
|
||||
application.o: application.c
|
||||
gcc -g -c -I . application.c -o application.o
|
||||
clean:
|
||||
rm *.o
|
||||
rm libdll.a
|
||||
rm exe
|
||||
291
Iterative_macros/application.c
Normal file
291
Iterative_macros/application.c
Normal file
@@ -0,0 +1,291 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: application.c
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 05/02/24 08:05:32 PM IST
|
||||
* Revision: none
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: YOUR NAME (),
|
||||
* Organization:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
#include "dll.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
/*Application specific data structures*/
|
||||
typedef struct student_{
|
||||
|
||||
char name[32];
|
||||
int age;
|
||||
int weight;
|
||||
unsigned int rollno;
|
||||
} student_t;
|
||||
|
||||
static void
|
||||
print_student_details(student_t *student){
|
||||
|
||||
printf("Name = %s\n", student->name);
|
||||
printf("Age = %d\n", student->age);
|
||||
printf("weight = %d\n", student->weight);
|
||||
printf("rollno = %u\n", student->rollno);
|
||||
}
|
||||
|
||||
static void
|
||||
print_student_db(dll_t *student_db) {
|
||||
|
||||
#if 0
|
||||
/*Old Code*/
|
||||
if(!student_db || !student_db->head) return;
|
||||
|
||||
dll_node_t *head = student_db->head;
|
||||
student_t *data = NULL;
|
||||
|
||||
while(head){
|
||||
data = head->data;
|
||||
print_student_details(data);
|
||||
head = head->right;
|
||||
}
|
||||
#endif
|
||||
/*Iterate over DLL using Iterative Macro*/
|
||||
|
||||
dll_node_t *node_ptr = NULL;
|
||||
student_t *data = NULL;
|
||||
|
||||
ITERATE_LIST_BEGIN(student_db, node_ptr){
|
||||
|
||||
data = node_ptr->data;
|
||||
print_student_details(data);
|
||||
} ITERATE_LIST_END;
|
||||
}
|
||||
|
||||
/*Application specific data structures*/
|
||||
typedef struct employee_{
|
||||
|
||||
char name[32];
|
||||
char designation[32];
|
||||
unsigned int salary;
|
||||
unsigned int emp_id;
|
||||
} employee_t;
|
||||
|
||||
static void
|
||||
print_employee_details(employee_t *employee){
|
||||
|
||||
printf("Name = %s\n", employee->name);
|
||||
printf("Designation = %s\n", employee->designation);
|
||||
printf("salary = %u\n", employee->salary);
|
||||
printf("emp_id = %u\n", employee->emp_id);
|
||||
}
|
||||
|
||||
static void
|
||||
print_employee_db(dll_t *student_db) {
|
||||
|
||||
if(!student_db || !student_db->head) return;
|
||||
|
||||
dll_node_t *head = student_db->head;
|
||||
student_t *data = NULL;
|
||||
|
||||
while(head){
|
||||
data = head->data;
|
||||
print_student_details(data);
|
||||
head = head->right;
|
||||
}
|
||||
}
|
||||
|
||||
/*Search function*/
|
||||
|
||||
student_t *
|
||||
search_student_by_rollno(dll_t *student_db,
|
||||
unsigned int rollno/*search key*/){
|
||||
|
||||
if(!student_db || !student_db->head) return NULL;
|
||||
|
||||
dll_node_t *head = student_db->head;
|
||||
student_t *data = NULL;
|
||||
|
||||
while(head){
|
||||
data = head->data;
|
||||
if(data->rollno == rollno)
|
||||
return data;
|
||||
head = head->right;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*Search function*/
|
||||
|
||||
employee_t *
|
||||
search_employee_by_emp_id(dll_t *employee_db,
|
||||
unsigned int emp_id/*search key*/){
|
||||
|
||||
if(!employee_db || !employee_db->head) return NULL;
|
||||
|
||||
dll_node_t *head = employee_db->head;
|
||||
employee_t *data = NULL;
|
||||
|
||||
while(head){
|
||||
data = head->data;
|
||||
if(data->emp_id == emp_id)
|
||||
return data;
|
||||
head = head->right;
|
||||
}
|
||||
}
|
||||
|
||||
/*Search callbacks*/
|
||||
static int /*return 0 if matches, return -1 if do not match*/
|
||||
search_student_db_by_key(void *data, void *key){
|
||||
|
||||
student_t *student = (student_t *)data;
|
||||
unsigned int rollno = (unsigned int)key;
|
||||
if(student->rollno == rollno)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*Search callbacks*/
|
||||
static int /*return 0 if matches, return -1 if do not match*/
|
||||
search_employee_db_by_key(void *data, void *key){
|
||||
|
||||
employee_t *emp = (employee_t *)data;
|
||||
unsigned int emp_id = (unsigned int)key;
|
||||
if(emp->emp_id == emp_id)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*Comparion Callback*/
|
||||
/*Return 0 if equal,
|
||||
* -1 if stud1 < stud2
|
||||
* 1 if stud1 > stud2*/
|
||||
|
||||
static int
|
||||
student_comparison_fn(void *stud1, void *stud2){
|
||||
|
||||
/*Let us define the sorting order of students,
|
||||
* Let us compare the students Alphabetically on their names
|
||||
* If two students have same name, younger students should
|
||||
* come before elder student
|
||||
* If age is also same, lighter student comes before heavier
|
||||
* if Weight is also same, students with numerically lesser rollno
|
||||
* comes before in the DLL*/
|
||||
|
||||
student_t *student1 = (student_t *)stud1;
|
||||
student_t *student2 = (student_t *)stud2;
|
||||
|
||||
if(strncmp(student1->name, student2->name, 32) < 0)
|
||||
return -1;
|
||||
else if(strncmp(student1->name, student2->name, 32) > 0)
|
||||
return 1;
|
||||
else if(student1->age < student2->age)
|
||||
return -1;
|
||||
else if(student1->age > student2->age)
|
||||
return 1;
|
||||
else if(student1->weight < student2->weight)
|
||||
return -1;
|
||||
else if(student1->weight > student2->weight)
|
||||
return 1;
|
||||
else if(student1->rollno < student2->rollno)
|
||||
return -1;
|
||||
else if(student1->rollno > student2->rollno)
|
||||
return 1;
|
||||
assert(0); /*intentionally Crash the program !!*/
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv){
|
||||
|
||||
/*Student database*/
|
||||
student_t *student1 = calloc(1, sizeof(student_t));
|
||||
strncpy(student1->name, "Abhishek", strlen("Abhishek"));
|
||||
student1->age = 31;
|
||||
student1->weight = 75;
|
||||
student1->rollno = 800000;
|
||||
|
||||
student_t *student2 = calloc(1, sizeof(student_t));
|
||||
strncpy(student2->name, "Joseph", strlen("Joseph"));
|
||||
student2->age = 41;
|
||||
student2->weight = 70;
|
||||
student2->rollno = 800400;
|
||||
|
||||
student_t *student3 = calloc(1, sizeof(student_t));
|
||||
strncpy(student3->name, "Jack", strlen("Jack"));
|
||||
student3->age = 29;
|
||||
student3->weight = 55;
|
||||
student3->rollno = 800400;
|
||||
|
||||
student_t *student4 = calloc(1, sizeof(student_t));
|
||||
strncpy(student3->name, "Abhishek", strlen("Abhishek"));
|
||||
student3->age = 29;
|
||||
student3->weight = 55;
|
||||
student3->rollno = 800399;
|
||||
|
||||
/*Create a new Linked List*/
|
||||
|
||||
dll_t *student_db = get_new_dll();
|
||||
register_key_match_callback(student_db, search_student_db_by_key);
|
||||
register_comparison_callback(student_db, student_comparison_fn);
|
||||
dll_priority_insert_data(student_db, student1);
|
||||
dll_priority_insert_data(student_db, student2);
|
||||
dll_priority_insert_data(student_db, student3);
|
||||
#if 0
|
||||
//student_t *student = search_student_by_rollno(student_db, 800400);
|
||||
student_t *student = dll_search_by_key(student_db, (void *)800400);
|
||||
if(!student){
|
||||
printf("Student record not found\n");
|
||||
}
|
||||
else{
|
||||
print_student_details(student);
|
||||
}
|
||||
#endif
|
||||
printf("Printing Stduents list\n");
|
||||
print_student_db(student_db);
|
||||
|
||||
#if 0
|
||||
/*Employee database*/
|
||||
employee_t *employee1 = calloc(1, sizeof(employee_t));
|
||||
strncpy(employee1->name, "Harsh", strlen("Harsh"));
|
||||
strncpy(employee1->designation, "Vice President", strlen("Vice President"));
|
||||
employee1->salary = 11131;
|
||||
employee1->emp_id = 10000;
|
||||
|
||||
employee_t *employee2 = calloc(1, sizeof(employee_t));
|
||||
strncpy(employee2->name, "Huma", strlen("Huma"));
|
||||
strncpy(employee2->designation, "CEO", strlen("CEO"));
|
||||
employee1->salary = 127000;
|
||||
employee1->emp_id = 10001;
|
||||
|
||||
employee_t *employee3 = calloc(1, sizeof(employee_t));
|
||||
strncpy(employee3->name, "Neena", strlen("Neena"));
|
||||
strncpy(employee2->designation, "Manager", strlen("Manager"));
|
||||
employee1->salary = 139000;
|
||||
employee1->emp_id = 10002;
|
||||
|
||||
/*Create a new Linked List*/
|
||||
|
||||
dll_t *employee_db = get_new_dll();
|
||||
register_key_match_callback(employee_db, search_employee_db_by_key);
|
||||
register_comparison_callback(employee_db, student_comparison_fn);
|
||||
|
||||
add_data_to_dll(employee_db, employee1);
|
||||
add_data_to_dll(employee_db, employee2);
|
||||
add_data_to_dll(employee_db, employee3);
|
||||
|
||||
employee_t *employee = dll_search_by_key(employee_db, (void *)10002);
|
||||
if(!employee){
|
||||
printf("Student record not found\n");
|
||||
}
|
||||
else{
|
||||
print_employee_details(employee);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
2440
Iterative_macros/application.i
Normal file
2440
Iterative_macros/application.i
Normal file
File diff suppressed because it is too large
Load Diff
155
Iterative_macros/dll.c
Normal file
155
Iterative_macros/dll.c
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: dll.c
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 05/02/24 08:04:39 PM IST
|
||||
* Revision: none
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: YOUR NAME (),
|
||||
* Organization:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
#include "dll.h"
|
||||
#include <memory.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* 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;
|
||||
dll->key_match = NULL;
|
||||
return dll;
|
||||
}
|
||||
|
||||
void
|
||||
register_key_match_callback(dll_t *dll,
|
||||
int (*key_match)(void *, void *)){
|
||||
|
||||
dll->key_match = key_match;
|
||||
}
|
||||
|
||||
void
|
||||
register_comparison_callback(dll_t *dll,
|
||||
int (*comparison_cb)(void *, void *)){
|
||||
|
||||
dll->comparison_fn = comparison_cb;
|
||||
}
|
||||
|
||||
/*Generic Search function*/
|
||||
void *
|
||||
dll_search_by_key (dll_t *dll, void *key){
|
||||
|
||||
if(!dll || !dll->head) return NULL;
|
||||
|
||||
dll_node_t *head = dll->head;
|
||||
|
||||
while(head){
|
||||
if(dll->key_match(head->data, key) == 0)
|
||||
return (void *)head->data;
|
||||
head = head->right;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*Return 0 - on success
|
||||
* -1 on Failure*/
|
||||
int
|
||||
dll_priority_insert_data (dll_t *dll, void *data){
|
||||
|
||||
if(!dll)
|
||||
return -1;
|
||||
|
||||
/*if DLL is empty*/
|
||||
if(!dll->head){
|
||||
add_data_to_dll(dll, data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dll_node_t *data_node = calloc(1, sizeof(dll_node_t));
|
||||
data_node->data = data;
|
||||
data_node->left = NULL;
|
||||
data_node->right = NULL;
|
||||
|
||||
/* if there is only one node in DLL*/
|
||||
if(dll->head && !dll->head->right){
|
||||
if(dll->comparison_fn(dll->head->data, data) == -1){
|
||||
dll->head->right = data_node;
|
||||
data_node->left = dll->head;
|
||||
}
|
||||
else{
|
||||
dll->head->left = data_node;
|
||||
data_node->right = dll->head;
|
||||
dll->head = data_node;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*if More than one node in DLL*/
|
||||
if(dll->comparison_fn(data, dll->head->data) == -1){
|
||||
data_node->right = dll->head;
|
||||
dll->head->left = data_node;
|
||||
dll->head = data_node;
|
||||
return 0;
|
||||
}
|
||||
|
||||
dll_node_t *prev = NULL,
|
||||
*curr = NULL;
|
||||
|
||||
curr = dll->head;
|
||||
|
||||
while(curr){
|
||||
if(dll->comparison_fn(data, curr->data) != -1){
|
||||
prev = curr;
|
||||
curr = curr->right;
|
||||
continue;
|
||||
}
|
||||
|
||||
prev->right = data_node;
|
||||
data_node->left = prev;
|
||||
data_node->right = curr;
|
||||
curr->left = data_node;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*Add in the end*/
|
||||
prev->right = data_node;
|
||||
data_node->left = prev;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
68
Iterative_macros/dll.h
Normal file
68
Iterative_macros/dll.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: dll.h
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 05/02/24 08:03:38 PM IST
|
||||
* Revision: none
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: YOUR NAME (),
|
||||
* Organization:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
/*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;
|
||||
int (*key_match)(void *, void *);
|
||||
int (*comparison_fn)(void *, void *);
|
||||
} dll_t;
|
||||
|
||||
void
|
||||
register_key_match_callback(dll_t *dll, int (*key_match)(void *, void *));
|
||||
|
||||
void
|
||||
register_comparison_callback(dll_t *dll, int (*comparison_cb)(void *, void *));
|
||||
|
||||
/*Generic Search function*/
|
||||
void *
|
||||
dll_search_by_key (dll_t *dll, void *key);
|
||||
|
||||
int
|
||||
dll_priority_insert_data (dll_t *dll, void *data);
|
||||
|
||||
/* 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);
|
||||
|
||||
/*Macro to iterate over a DLL*/
|
||||
|
||||
#define ITERATE_LIST_BEGIN(list_ptr, node_ptr) \
|
||||
{ \
|
||||
dll_node_t *_node_ptr = NULL; \
|
||||
node_ptr = list_ptr->head; \
|
||||
for(; node_ptr!= NULL; node_ptr = _node_ptr){ \
|
||||
if(!node_ptr) break; \
|
||||
_node_ptr = node_ptr->right;
|
||||
|
||||
#define ITERATE_LIST_END }}
|
||||
|
||||
153
Iterative_macros/tree.c
Normal file
153
Iterative_macros/tree.c
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: tree.c
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 05/02/24 09:51:42 PM IST
|
||||
* Revision: none
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: YOUR NAME (),
|
||||
* Organization:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
#include "tree.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
|
||||
tree_t*
|
||||
init_tree(void)
|
||||
{
|
||||
tree_t *tree = calloc(1,sizeof(tree_t));
|
||||
if(!tree)
|
||||
return NULL;
|
||||
tree->root = NULL;
|
||||
return tree;
|
||||
}
|
||||
|
||||
tree_node_t* init_tree_node(int n)
|
||||
{
|
||||
tree_node_t *node = calloc(1, sizeof(tree_node_t));
|
||||
if(!node) return NULL;
|
||||
node->data = n;
|
||||
return node;
|
||||
}
|
||||
|
||||
int
|
||||
add_tree_node_by_value(tree_t *tree, int n)
|
||||
{
|
||||
tree_node_t *root = NULL, *parent = NULL;;
|
||||
if(!tree) return -1;
|
||||
tree_node_t *node = init_tree_node(n);
|
||||
if(!tree->root){
|
||||
tree->root = node;
|
||||
return 0;
|
||||
}
|
||||
|
||||
root = tree->root;
|
||||
|
||||
while(root){
|
||||
parent = root;
|
||||
root = (n <= root->data) ? root->left : root->right;
|
||||
} // while ends
|
||||
|
||||
if(n <= parent->data)
|
||||
parent->left = node;
|
||||
else
|
||||
parent->right = node;
|
||||
|
||||
node->parent = parent;
|
||||
return 0;
|
||||
}
|
||||
|
||||
tree_node_t *
|
||||
get_left_most(tree_node_t *node){
|
||||
|
||||
if(!node->left)
|
||||
return NULL;
|
||||
|
||||
while(node->left){
|
||||
node = node->left;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
tree_node_t *
|
||||
get_next_inorder_succ(tree_node_t *node){
|
||||
|
||||
/* case 1 : Handling root*/
|
||||
if(!node->parent){
|
||||
if(node->right){
|
||||
if(node->right->left)
|
||||
return get_left_most(node->right);
|
||||
else
|
||||
return node->right;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*case 2 : if node is a left child of its parent*/
|
||||
if(node == node->parent->left){
|
||||
if(!node->right)
|
||||
return node->parent;
|
||||
else
|
||||
if(node->right->left)
|
||||
return get_left_most(node->right);
|
||||
else
|
||||
return node->right;
|
||||
}
|
||||
|
||||
/*case 3 : if node is a right child of its parent*/
|
||||
if(node == node->parent->right){
|
||||
if(node->right){
|
||||
if(node->right->left)
|
||||
return get_left_most(node->right);
|
||||
else
|
||||
return node->right;
|
||||
}
|
||||
}
|
||||
|
||||
/* case 4 : Inorder successor of node is a ancestor whose
|
||||
* left children is also an ancestor*/
|
||||
|
||||
tree_node_t *gp = node->parent->parent;
|
||||
tree_node_t *parent = node->parent;
|
||||
|
||||
while(gp && gp->left != parent){
|
||||
|
||||
parent = gp;
|
||||
gp = gp->parent;
|
||||
}
|
||||
|
||||
return gp;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv){
|
||||
|
||||
tree_t *tree = init_tree();
|
||||
|
||||
add_tree_node_by_value(tree, 100);
|
||||
add_tree_node_by_value(tree, 50);
|
||||
add_tree_node_by_value(tree, 10);
|
||||
add_tree_node_by_value(tree, 90);
|
||||
add_tree_node_by_value(tree, 95);
|
||||
add_tree_node_by_value(tree, 99);
|
||||
|
||||
tree_node_t *treenodeptr = NULL;
|
||||
|
||||
ITERATE_BST_BEGIN(tree, treenodeptr){
|
||||
|
||||
printf("%u ", treenodeptr->data);
|
||||
|
||||
} ITERATE_BST_END;
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
58
Iterative_macros/tree.h
Normal file
58
Iterative_macros/tree.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: tree.h
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 05/02/24 09:52:32 PM IST
|
||||
* Revision: none
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: YOUR NAME (),
|
||||
* Organization:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
#ifndef __TREE__
|
||||
#define __TREE__
|
||||
|
||||
typedef struct tree_node {
|
||||
struct tree_node *left;
|
||||
struct tree_node *right;
|
||||
struct tree_node *parent;
|
||||
int data;
|
||||
} tree_node_t;
|
||||
|
||||
|
||||
typedef struct tree {
|
||||
tree_node_t *root;
|
||||
} tree_t;
|
||||
|
||||
int
|
||||
add_tree_node_by_value(tree_t *tree, int n);
|
||||
|
||||
tree_t* init_tree(void);
|
||||
|
||||
tree_node_t* init_tree_node(int n);
|
||||
|
||||
/*Pre-requisites functions to write iterative
|
||||
* macros for a tree.*/
|
||||
|
||||
tree_node_t *
|
||||
get_left_most (tree_node_t *node);
|
||||
|
||||
tree_node_t *
|
||||
get_next_inorder_succ (tree_node_t *node);
|
||||
|
||||
#define ITERATE_BST_BEGIN(treeptr, currentnodeptr) \
|
||||
{ \
|
||||
tree_node_t *_next = NULL; \
|
||||
for(currentnodeptr = get_left_most(treeptr->root); currentnodeptr != NULL ; currentnodeptr = _next){ \
|
||||
_next = get_next_inorder_succ(currentnodeptr);
|
||||
|
||||
#define ITERATE_BST_END }}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user