First Upload

This commit is contained in:
2022-02-25 15:33:16 +02:00
commit 0c74d10f0d
295 changed files with 74784 additions and 0 deletions

2830
29 - Graphs/General.c Normal file

File diff suppressed because it is too large Load Diff

298
29 - Graphs/General.h Normal file
View File

@@ -0,0 +1,298 @@
#include <stdio.h>
#include <malloc.h>
#define DAYS_IN_MONTH 30
#define MONTHS_IN_YEAR 12
#define ONE_HUNDRED 100
#define ONE_THOSEND 10000
#define BOOLEAN unsigned short
#define TRUE 1
#define FALSE 0
#define SEVEN 7
#define EIGHT 8
#define TEN 10
#define TWELVE 12
#define THIRTY 30
#define ZERO 0
#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 4
#define FIVE 5
#define TAKE_SIGNED_MATH(x) (((2 * (x)) + 1) % 2)
#define TAKE_SIGNED(x) ((x) < 0) ? -1 : 1
#define ABS(x) (x) * (((2 * (x)) + 1) % 2)
#define MAX(x, y) (x > y) ? x : y
#define MIN(x, y) (x < y) ? x : y
#define BACKSLASH_ZERO '\0'
#define MAX_SIZE_STRING 256
#define MASK_FIRST_BIT 0x1
typedef char string[MAX_SIZE_STRING];
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int dblword;
typedef union
{
int int_;
char char_;
double double_;
void * pointer;
} Data_Type;
typedef struct
{
Data_Type * values;
unsigned int items;
} stack;
typedef struct
{
Data_Type * values;
unsigned int items;
} queue;
struct LLL
{
Data_Type value;
struct LLL * next;
};
struct DLLL
{
Data_Type info;
struct DLLL * next;
struct DLLL * prev;
};
struct CLLL
{
Data_Type info;
struct CLLL * next;
};
struct CDLLL
{
Data_Type info;
struct CDLLL * next;
struct CDLLL * prev;
};
struct SM
{
Data_Type info;
int row;
int col;
struct SM * col_next;
struct SM * row_next;
};
struct BinaryTree
{
Data_Type info;
struct BinaryTree * right;
struct BinaryTree * left;
};
struct ThirdTree
{
Data_Type info;
struct ThirdTree * right;
struct ThirdTree * left;
struct ThirdTree * middle;
};
struct GeneralTree
{
Data_Type info;
struct LLL * manager_general_trees;
};
struct Graph
{
Data_Type info;
struct LLL * list;
};
struct Arc
{
int weight;
struct Graph * point;
};
typedef struct SM SM;
typedef struct LLL LLL;
typedef struct DLLL DLLL;
typedef struct CLLL CLLL;
typedef struct CDLLL CDLLL;
typedef struct BinaryTree BinaryTree;
typedef struct ThirdTree ThirdTree;
typedef struct GeneralTree GeneralTree;
typedef struct Graph Graph;
typedef struct Arc Arc;
void ChangeYearsToMonths(int *years, short *month);
void SwapNumbers(int *ptr_number1, int *ptr_number2);
void SwapChars(char *ptr_char1, char *ptr_char2);
void ChangeMonthsToDays(short *months, int *days);
int Random(int ** seed, int min, int max);
void ChangeDateToDays(int *date, int *days);
void ChangeDaysToMonths(int *days, int *months);
void ChangeMonthsToYears(int *months, int *years);
void ChangeDaysToDate(int *days, int *date);
int DifferentDates(int *date1, int *date2, int *different);
BOOLEAN EvenNumber(int num);
int OppositeNumber(int num);
int OddDigitsInNumber(int num);
int EvenDigitsInNumber(int num);
int SumOfNumberWithPositiveDigitsAndNumberWithNegativeDigits(int num);
double Power(float num, short pow);
int GetRest(double num, unsigned short count_digits);
unsigned short CountDigits(int num);
float ConvertBaseToDeciaml(float num, float base, unsigned short rest_size);
double Sum(double number1, double number2);
int SumOfDigitsNumber(double number);
int SumEvenDigits(double number);
int SumOddDigits(double number);
BOOLEAN CommonDigitsInTwoNumbers(double number1, double number2);
double Sqrt(double number, short root);
BOOLEAN PrimeNumber(int number);
double Module(double number, double div); // TODO : FIX IT
BOOLEAN DigitInNumber(double number, unsigned short digit);
int ConcatenationNumbers(short number1, short number2);
int Multiplication(short number1, short multiply); // TODO : FIX IT
int GetDenominator(double number);
short GetCounter(double number);
double Divide(double number, double div); // TODO : FIX IT
BOOLEAN SumInArray(int array[], unsigned int size, int sum);
BOOLEAN ChangeBool(BOOLEAN bool1);
BOOLEAN Equality(int number1, int number2);
BOOLEAN NumBBiggerThenNumA(int a, int b);
unsigned int SumOfTimesCharsArray1InCharsArray2(char array1[],
unsigned int array1_size,
char array2[],
unsigned int array2_size);
unsigned int CreateMask(unsigned short size, unsigned short digit_of_mask);
unsigned short CountADigitInNumber(int number, unsigned short digit);
short GetDigit(int number, unsigned short location);
unsigned short CountOfIdenticalDigitsInSomeIndex(int number1, int number2);
unsigned short CountOfIdenticalDigits(int number1, int number2);
BOOLEAN ForeignDigits(int number);
void PrintMatrix(void * ptr_mat, unsigned short col, unsigned short row, void print(void *));
void CopyChar(char * ch, char * copy);
void ResetMatrix(void * ptr_mat, unsigned short col, unsigned short row, void * type, void insert(void *, void *));
BOOLEAN CharInString(char text[], char c);
char * LastCharOfString(char * start_string);
unsigned int StringLength(char * str);
BOOLEAN StringCompare(char *start_stringA_index, char *start_stringB_index);
char * IndexCharInPointers(char *start_pointer, char *end_pointer , char c);
unsigned short CountCharInString(char *start_string_index, char *c);
void CutString(char *start_textA_index, char *end_textA_index, char *start_textB_index);
void CopyString(char *start_string_index, char *start_copy_index);
void LinkingString(char *start_stringA_index, char *start_stringB_index);
void ReverseString(char textA[]);
BOOLEAN StringBInStringA(char *start_string_a, char *start_string_b);
char * IndexStringBInStringA(char *start_string_a, char *start_string_b);
unsigned int CountStringBInStringA(char *start_string_a, char *start_string_b);
void RemoveStringBFromStringA(char *start_string_a, char *start_string_b);
void RemoveAllStringBFromStringA(char *start_stringA_index, char *start_stringB_index);
BOOLEAN ValidParenthesesTemplate(char text[]);
unsigned short CharToNumber(char *c);
unsigned int MaxCountCharInString(char *start_string_index);
void CopyPointers(char *start_string_index, char *start_copy_index, char *end_copy_index);
BOOLEAN EvenBits(byte b);
unsigned short CountBits(byte b);
void Multiply(float *ptr_number1, float *ptr_number2, double *ptr_result);
// -------------------------------------- Stack -----------------------------------------
void InitStack(stack * sk);
BOOLEAN IsEmptyStack(stack * sk);
void PushStack(stack * sk , Data_Type item);
Data_Type PopStack(stack * sk);
void EmptyStack(stack * sk);
void CopyStack(stack * sk, stack * copy);
void OppositeStack(stack * sk);
BOOLEAN IsEqualsStack(stack * sk1, stack * sk2);
unsigned int ItemsStack(stack * sk);
int SumStack(stack * sk);
BOOLEAN FindNumberInStack(stack * sk, Data_Type item);
void UnionStack(stack * sk1, stack * sk2);
// -------------------------------------- Queue -----------------------------------------
void InitQueue(queue * q);
void InsertQueue(queue * q, Data_Type item);
Data_Type RemoveQueue(queue * q);
BOOLEAN IsEmptyQueue(queue * q);
// -------------------------------------- LLL -------------------------------------------
void InsertAfterLLL(LLL * manager);
void PushLLL(LLL ** manager);
void PopLLL(LLL ** manager);
void DeleteLLL(LLL * manager);
LLL * GetLoactionLLL(LLL ** manager, unsigned short location);
// -------------------------------------- DLLL ------------------------------------------
void InitDLLL(DLLL ** manager);
void PushDLLL(DLLL ** manager);
void InsertAfterDLLL(DLLL * ptr_before);
void InsertBeforeDLLL(DLLL * ptr_after);
void PopDLLL(DLLL ** manager);
void DeleteDLLL(DLLL * ptr_cur);
void PrintDLLL(DLLL * manager, void Print(Data_Type dt));
// --------------------------------- CLLL -----------------------------------------------
void InsertLastCLLL(CLLL ** manager);
void InsertAfterCLLL(CLLL * ptr_before);
void InsertEndCLLL(CLLL ** manager);
void DeleteLastCLLL(CLLL ** manager);
void DeleteAfterCLLL(CLLL * ptr_before);
void DeleteEndCLLL(CLLL ** manager);
// --------------------------------- CDLLL ----------------------------------------------
void InsertLastCDLLL(CDLLL ** manager);
void InsertAfterCDLLL(CDLLL * ptr_before);
void InsertBeforeCDLLL(CDLLL * ptr_after);
void InsertEndCDLLL(CDLLL ** manager);
void DeleteCDLLL(CDLLL * ptr_cur);
void DeleteLastCDLLL(CDLLL ** manager);
void PrintCDLLL(CDLLL * manager, void Print(Data_Type dt));
// ---------------------------- Sparce Matrix -------------------------------------------
void InitSM(SM ** manager);
void AddRowSM(SM * manager);
void AddColSM(SM * manager);
SM * GetItemSM(SM * manager, int row, int col);
SM * FindAbove(SM * ptr_sm);
SM * FindBefore(SM * ptr_sm);
unsigned int RowsCountSM(SM * manager);
unsigned short ColsCountSM(SM * manager);
void AddItemSM(SM * manager, int row, int col);
void RemoveItemSM(SM * manager, int row, int col);
void RemoveColSM(SM * manager);
void RemoveRowSM(SM * manager);
void PrintIntDataType(Data_Type dt);
void PrintSM(SM * manager, void print(Data_Type));
// -------------------------------------- Binary Tree -----------------------------------
void MakeBinaryTree(BinaryTree ** tree);
void AddLeftAfterBinaryTree(BinaryTree * tree);
void AddRightAfterBinaryTree(BinaryTree * tree);
BOOLEAN IsLeafBinaryTree(BinaryTree * tree);
// -------------------------------------- Third Tree -----------------------------------
void MakeThirdTree(ThirdTree ** tree);
void AddLeftAfterThirdTree(ThirdTree * tree);
void AddRightAfterThirdTree(ThirdTree * tree);
void AddMiddleAfterThirdTree(ThirdTree * tree);
BOOLEAN IsLeafThirdTree(ThirdTree * tree);
// -------------------------------------- General Tree -----------------------------------
void MakeGeneralTree(GeneralTree ** tree);
void AddAfterGeneralTree(GeneralTree * tree);
BOOLEAN IsLeafGeneralTree(GeneralTree * tree);
// -------------------------------------- Funcations Tree -----------------------------------
void PreodererPrintBinaryTree(BinaryTree * root, void print(Data_Type));
void PosorderPrintBinaryTree(BinaryTree * root, void print(Data_Type));
void InorderPrintBinaryTree(BinaryTree * root, void print(Data_Type));
void LevelPrintBinaryTree(BinaryTree * root, void print(Data_Type));
// -------------------------------------- Graph -----------------------------------------
void JoinGraphDirectedWt(Graph * a, Graph * b, int weight);
void JoinGraphNonDirectedWt(Graph * a, Graph * b, int weight);
void JoinGraphDirected(Graph * a, Graph * b);
void JoinGraphNonDirected(Graph * a, Graph * b);
void RemoveGraphDirectedWt(Graph * a, Graph * b, int weight);
void RemoveGraphDirected(Graph * a, Graph * b);
void RemoveGraphNonDirected(Graph * a, Graph * b);
void RemoveGraphNonDirectedWt(Graph * a, Graph * b, int weight);
BOOLEAN AdjacentGraph(Graph * a, Graph * b);

BIN
29 - Graphs/a.out Normal file

Binary file not shown.

255
29 - Graphs/ex1.c Normal file
View File

@@ -0,0 +1,255 @@
#include "General.h"
struct Graph
{
Data_Type info;
struct LLL * list;
};
struct Arc
{
int weight;
struct Graph * point;
};
typedef struct Graph Graph;
typedef struct Arc Arc;
//---------------------------------------------------------------------------------------
// JoinGraphDirectedWt
// -------------------
//
// General : the function adds an arc between two items with direction
// and weight to the arc
//
// Parameters : Graph * a - the first item
// Graph * b -the second item
// int weight - the weight of the arc to be added
//
// Return Value : Void
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void JoinGraphDirectedWt(Graph * a, Graph * b, int weight)
{
Arc * arc = (Arc *)malloc(sizeof(Arc));
arc->point = b;
arc->weight = weight;
PushLLL(&a->list);
a->list->value = (Data_Type)(void *)arc;
}
//---------------------------------------------------------------------------------------
// JoinGraphNonDirectedWt
// ----------------------
//
// General : adds an arc between two items for both direction with
// weight to it
//
// Parameters : Graph * a - the first item
// Graph * b -the second item
// int weight - the weight of the arc to be added
//
// Return Value : Void
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void JoinGraphNonDirectedWt(Graph * a, Graph * b, int weight)
{
JoinGraphDirectedWt(a, b, weight);
JoinGraphDirectedWt(b, a, weight);
}
//---------------------------------------------------------------------------------------
// JoinGraphDirected
// -----------------
//
// General : adds an arc between two items without weight(single
// direction)
//
// Parameters : Graph * a - the first item
// Graph * b -the second item
//
// Return Value : Void
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void JoinGraphDirected(Graph * a, Graph * b)
{
JoinGraphDirectedWt(a, b, ZERO);
}
//---------------------------------------------------------------------------------------
// JoinGraphNonDirected
// --------------------
//
// General : adds an arc between two items without weight.
//
// Parameters : Graph * a - the first item.
// Graph * b -the second item.
//
// Return Value : Void.
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void JoinGraphNonDirected(Graph * a, Graph * b)
{
JoinGraphDirected(a, b);
JoinGraphDirected(b, a);
}
//---------------------------------------------------------------------------------------
// RemoveGraphDirectedWt
// ---------------------
//
// General : removes an arc between two items in a graph with the
// weight of it(searches for a certain direction).
//
// Parameters : Graph * a - the first item.
// Graph * b -the second item.
// int weight - the weight of the arc to be removed.
//
// Return Value : Void.
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void RemoveGraphDirectedWt(Graph * a, Graph * b, int weight)
{
LLL * temp = a->list;
if (temp == NULL)
{
}
else if (((Arc *)temp->value.pointer)->point == b &&
((Arc *)temp->value.pointer)->weight == weight)
{
PopLLL(&a->list);
}
else
{
while (temp->next && ((Arc *)temp->next->value.pointer)->point != b ||
((Arc *)temp->next->value.pointer)->weight != weight)
{
temp = temp->next;
}
if (temp->next)
{
DeleteAfterLLL(temp);
}
}
}
//---------------------------------------------------------------------------------------
// RemoveGraphDirected
// -------------------
//
// General : function removes arc between two items in a graph with a
// direction between them.
//
// Parameters : Graph * a - the first item.
// Graph * b -the second item.
//
// Return Value : Void.
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void RemoveGraphDirected(Graph * a, Graph * b)
{
RemoveGraphDirectedWt(a, b, ZERO);
}
//---------------------------------------------------------------------------------------
// RemoveGraphNonDirected
// ----------------------
//
// General : function removes arc between two items in a graph,
// when the arc doesnt have weight to it.
//
// Parameters : Graph * a - the first item.
// Graph * b -the second item.
//
// Return Value : Void
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void RemoveGraphNonDirected(Graph * a, Graph * b)
{
RemoveGraphDirected(a ,b);
RemoveGraphDirected(b, a);
}
//---------------------------------------------------------------------------------------
// RemoveGraphNonDirectedWt
// ------------------------
//
// General : the function removes a specific arc with a specific weight
// to it
//
// Parameters : Graph * a - the first item
// Graph * b -the second item
// int weight - the weight of the arc to be removed
//
// Return Value : Void
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
void RemoveGraphNonDirectedWt(Graph * a, Graph * b, int weight)
{
RemoveGraphDirectedWt(a ,b, weight);
RemoveGraphDirectedWt(b, a, weight);
}
//---------------------------------------------------------------------------------------
// AdjacentGraph
// -------------
//
// General : Checks if a has direct access to b.
//
// Parameters : Graph * a - item to check if it is next to b
// Graph * b - item to check if it is next to a.
//
// Return Value : if Adjacent graph or not (true/false).
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
BOOLEAN AdjacentGraph(Graph * a, Graph * b)
{
LLL * temp = a->list;
while (temp && ((Arc *)temp->next->value.pointer)->point != b)
{
temp = temp->next;
}
return (((Arc *)temp->next->value.pointer)->point == b);
}
void main(void)
{
}

41
29 - Graphs/ex2.c Normal file
View File

@@ -0,0 +1,41 @@
#include "General.h"
//---------------------------------------------------------------------------------------
// DoesExistPath
// -------------
//
// General : Check if have path from point a to point b in graph with number of arc
//
// Parameters : Graph * a - the first item.
// Graph * b - the second item.
// unsigned int number_road - the number of arc.
//
// Return Value : Void
//
//---------------------------------------------------------------------------------------
// Programer : Cohen Idan
// Student No. : 211675038
// Date : 04.01.2020
//---------------------------------------------------------------------------------------
unsigned int DoesExistPath(Graph * start_point, Graph * end_point, unsigned int number_road)
{
LLL * temp;
if (!number_road)
{
return (start_point == end_point);
}
else
{
temp = start_point->list;
while (temp && !DoesExistPath(((Arc *)temp->value.pointer)->point, end_point, number_road - ONE))
{
temp = temp->next;
}
return (!temp);
}
}
void main(void)
{
}