Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
cantor_set.c File Reference

Program to generate Cantor ternary set More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for cantor_set.c:

Data Structures

struct  _cantor_set
 structure to define Cantor set More...
 

Typedefs

typedef struct _cantor_set CantorSet
 structure to define Cantor set
 

Functions

void propagate (CantorSet *head)
 Iterative constructor of all sets in the current level. More...
 
void print (CantorSet *head)
 Print sets in the current range to stdout More...
 
void free_memory (CantorSet *head)
 Clear memory allocated by propagate function. More...
 
int main (int argc, char const *argv[])
 Main function.
 

Detailed Description

Program to generate Cantor ternary set

Function Documentation

◆ free_memory()

void free_memory ( CantorSet head)

Clear memory allocated by propagate function.

Parameters
headpointer to first allocated instance.
73 {
74  if (!head)
75  return;
76 
77  if (head->next)
78  free_memory(head->next);
79 
80  free(head);
81 }

◆ print()

void print ( CantorSet head)

Print sets in the current range to stdout

Parameters
headpointer to first set in the current level
56 {
57  CantorSet *temp = head;
58  while (temp != NULL) // print while a valid set is found
59  {
60  printf("\t");
61  printf("[%lf] -- ", temp->start);
62  printf("[%lf]", temp->end);
63  temp = temp->next;
64  }
65 
66  printf("\n");
67 }

◆ propagate()

void propagate ( CantorSet head)

Iterative constructor of all sets in the current level.

This function dynamically allocates memory when creating new sets. These are freed by the function free_memory.

Parameters
headpointer to interval set instance to update
24 {
25  // if input is NULL, ignore the process
26  if (head == NULL)
27  return;
28 
29  CantorSet *temp = head; // local pointer to track propagation
30 
31  // create new node for the new set
32  CantorSet *newNode = (CantorSet *)malloc(sizeof(CantorSet));
33 
34  // get 1/3rd of interval
35  double diff = (((temp->end) - (temp->start)) / 3);
36 
37  // update interval ranges
38  newNode->end = temp->end;
39  temp->end = ((temp->start) + diff);
40  newNode->start = (newNode->end) - diff;
41 
42  // update pointer to next set in this level
43  newNode->next = temp->next;
44 
45  // point to next set
46  temp->next = newNode;
47 
48  // create next set
49  propagate(temp->next->next);
50 }
Here is the call graph for this function:
propagate
void propagate(CantorSet *head)
Iterative constructor of all sets in the current level.
Definition: cantor_set.c:23
free_memory
void free_memory(CantorSet *head)
Clear memory allocated by propagate function.
Definition: cantor_set.c:72
newNode
node * newNode(int data)
The node constructor, which receives the key value input and returns a node pointer.
Definition: binary_search_tree.c:28
_cantor_set
structure to define Cantor set
Definition: cantor_set.c:12