From 89a54ae41a03a8f6b3d0ae8e5d45105cbcb15c3b Mon Sep 17 00:00:00 2001 From: orperes1 <53706045+orperes1@users.noreply.github.com> Date: Sun, 29 Dec 2019 14:45:24 +0200 Subject: [PATCH 01/19] Create 872.c --- 872.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 872.c diff --git a/872.c b/872.c new file mode 100644 index 00000000..577a3c9f --- /dev/null +++ b/872.c @@ -0,0 +1,43 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * struct TreeNode *left; + * struct TreeNode *right; + * }; + */ + + +bool leafSimilar(struct TreeNode* root1, struct TreeNode* root2){ + int sequence1[100] ={0}, sequence2[100]={0}, num_of_node1 =0,num_of_node2 =0; + + num_of_node1 = sequence(root1,sequence1,num_of_node1); + num_of_node2 = sequence(root2,sequence2,num_of_node2); + + if (num_of_node1 != num_of_node2) + return false; + + for (int i = 0;ileft && !root->right){ + list[num_of_node] = root->val; + num_of_node ++; + return num_of_node; + } + + num_of_node = sequence(root->left ,list , num_of_node); + num_of_node = sequence(root->right ,list , num_of_node); + + return num_of_node; +} From fd52c9f2c653ce4ac6c35815d7554a45efe52ac3 Mon Sep 17 00:00:00 2001 From: ttuanho Date: Fri, 17 Jan 2020 20:10:31 +1100 Subject: [PATCH 02/19] added more grah algos & Makefile, fixed som return non-void func errors --- data_structures/graphs/Graph.c | 102 ++++++++++++++++++ data_structures/graphs/Graph.h | 37 +++++++ data_structures/graphs/Makefile | 56 ++++++++++ data_structures/graphs/bfsQueue.c | 81 ++++++++++++++ data_structures/graphs/dfsRecursive.c | 74 +++++++++++++ data_structures/graphs/euler.c | 82 ++++++++++++++ data_structures/graphs/hamiltonian.c | 87 +++++++++++++++ data_structures/graphs/kruskal.c | 2 +- data_structures/graphs/queue.c | 88 +++++++++++++++ data_structures/graphs/queue.h | 26 +++++ .../graphs/strongly_connected_components.c | 2 + data_structures/graphs/topologicalSort.c | 2 + data_structures/graphs/transitiveClosure.c | 48 +++++++++ 13 files changed, 686 insertions(+), 1 deletion(-) create mode 100644 data_structures/graphs/Graph.c create mode 100644 data_structures/graphs/Graph.h create mode 100644 data_structures/graphs/Makefile create mode 100644 data_structures/graphs/bfsQueue.c create mode 100644 data_structures/graphs/dfsRecursive.c create mode 100644 data_structures/graphs/euler.c create mode 100644 data_structures/graphs/hamiltonian.c create mode 100644 data_structures/graphs/queue.c create mode 100644 data_structures/graphs/queue.h create mode 100644 data_structures/graphs/transitiveClosure.c diff --git a/data_structures/graphs/Graph.c b/data_structures/graphs/Graph.c new file mode 100644 index 00000000..52fc3a27 --- /dev/null +++ b/data_structures/graphs/Graph.c @@ -0,0 +1,102 @@ +// Graph ADT +// Adjacency Matrix Representation +#include "Graph.h" +#include +#include +#include + +typedef struct GraphRep { + int **edges; // adjacency matrix + int nV; // #vertices + int nE; // #edges +} GraphRep; + +Graph newGraph(int V) { + assert(V >= 0); + int i; + + Graph g = malloc(sizeof(GraphRep)); + assert(g != NULL); + g->nV = V; + g->nE = 0; + + // allocate memory for each row + g->edges = malloc(V * sizeof(int *)); + assert(g->edges != NULL); + // allocate memory for each column and initialise with 0 + for (i = 0; i < V; i++) { + g->edges[i] = calloc(V, sizeof(int)); + assert(g->edges[i] != NULL); + } + + return g; +} + +// check if vertex is valid in a graph +bool validV(Graph g, Vertex v) { + return (g != NULL && v >= 0 && v < g->nV); +} + +void insertEdge(Graph g, Edge e) { + assert(g != NULL && validV(g,e.v) && validV(g,e.w)); + + if (!g->edges[e.v][e.w]) { // edge e not in graph + g->edges[e.v][e.w] = 1; + g->edges[e.w][e.v] = 1; + g->nE++; + } +} + +void removeEdge(Graph g, Edge e) { + assert(g != NULL && validV(g,e.v) && validV(g,e.w)); + + if (g->edges[e.v][e.w]) { // edge e in graph + g->edges[e.v][e.w] = 0; + g->edges[e.w][e.v] = 0; + g->nE--; + } +} + +bool adjacent(Graph g, Vertex v, Vertex w) { + assert(g != NULL && validV(g,v) && validV(g,w)); + + return (g->edges[v][w] != 0); +} + +void showGraph(Graph g) { + assert(g != NULL); + int i, j; + + printf("Number of vertices: %d\n", g->nV); + printf("Number of edges: %d\n", g->nE); + for (i = 0; i < g->nV; i++) + for (j = i+1; j < g->nV; j++) + if (g->edges[i][j]) + printf("Edge %d - %d\n", i, j); +} + +void freeGraph(Graph g) { + assert(g != NULL); + + int i; + for (i = 0; i < g->nV; i++) + free(g->edges[i]); + free(g->edges); + free(g); +} + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com diff --git a/data_structures/graphs/Graph.h b/data_structures/graphs/Graph.h new file mode 100644 index 00000000..95a35e2a --- /dev/null +++ b/data_structures/graphs/Graph.h @@ -0,0 +1,37 @@ +// Graph ADT interface ... COMP2521 +#include + +typedef struct GraphRep *Graph; + +// vertices are ints +typedef int Vertex; + +// edges are pairs of vertices (end-points) +typedef struct Edge { + Vertex v; + Vertex w; +} Edge; + +Graph newGraph(int); +void insertEdge(Graph, Edge); +void removeEdge(Graph, Edge); +bool adjacent(Graph, Vertex, Vertex); +void showGraph(Graph); +void freeGraph(Graph); + + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com diff --git a/data_structures/graphs/Makefile b/data_structures/graphs/Makefile new file mode 100644 index 00000000..781352e6 --- /dev/null +++ b/data_structures/graphs/Makefile @@ -0,0 +1,56 @@ +CC=gcc +CFLAGS=-Wall -Werror -std=c99 +all: BFS Bellman-Ford DFS Dijkstra Floyd-Warshall bfsQueue dfsRecursive euler hamiltonian strongly_connected_components topologicalSort transitiveClosure + + +BFS: BFS.c + $(CC) -o BFS BFS.c +Bellman-Ford: Bellman-Ford.c + $(CC) -o Bellman-Ford Bellman-Ford.c +DFS: DFS.c + $(CC) -o DFS DFS.c +Dijkstra: Dijkstra.c + $(CC) -o Dijkstra Dijkstra.c +Floyd-Warshall: Floyd-Warshall.c + $(CC) -o Floyd-Warshall Floyd-Warshall.c +Graph.o: Graph.c Graph.h + $(CC) $(CFLAGS) -c Graph.c +bfsQueue: Graph.o queue.o bfsQueue.o + $(CC) Graph.o queue.o bfsQueue.o -o bfsQueue +bfsQueue.o: bfsQueue.c + $(CC) $(CFLAGS) -c bfsQueue.c +dfsRecursive: Graph.o queue.o dfsRecursive.o + $(CC) Graph.o queue.o dfsRecursive.o -o dfsRecursive +dfsRecursive.o: dfsRecursive.c + $(CC) -c dfsRecursive.c +euler: Graph.o euler.o + $(CC) Graph.o euler.o -o euler +euler.o: euler.c + $(CC) $(CFLAGS) -c euler.c +hamiltonian: Graph.o hamiltonian.o + $(CC) Graph.o hamiltonian.o -o hamiltonian +hamiltonian.o: hamiltonian.c + $(CC) $(CFLAGS) -c hamiltonian.c +queue.o: queue.c queue.h + $(CC) $(CFLAGS) -c queue.c +strongly_connected_components: strongly_connected_components.c + $(CC) -o strongly_connected_components strongly_connected_components.c +topologicalSort: topologicalSort.c + $(CC) -o topologicalSort topologicalSort.c +transitiveClosure: transitiveClosure.c + $(CC) -o transitiveClosure transitiveClosure.c +# By +# .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +# | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +# | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +# | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +# | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +# | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +# | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +# | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +# | | | || | | || | | || | | | | | | || | | | +# | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +# '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +# Email : z5261243@unsw.edu.au +# hhoanhtuann@gmail.com \ No newline at end of file diff --git a/data_structures/graphs/bfsQueue.c b/data_structures/graphs/bfsQueue.c new file mode 100644 index 00000000..8eb7b975 --- /dev/null +++ b/data_structures/graphs/bfsQueue.c @@ -0,0 +1,81 @@ +#include +#include +#include "queue.h" +#include "Graph.h" + +#define MAX_NODES 1000 + +int visited[MAX_NODES]; // array to store visiting order + // indexed by vertex 0..nV-1 + +bool findPathBFS(Graph g, int nV, Vertex src, Vertex dest) { + Vertex v; + for (v = 0; v < nV; v++) + visited[v] = -1; + + visited[src] = src; + queue Q = newQueue(); + QueueEnqueue(Q, src); + while (!QueueIsEmpty(Q)) { + v = QueueDequeue(Q); + Vertex w; + for (w = 0; w < nV; w++) + if (adjacent(g, v, w) && visited[w] == -1) { + visited[w] = v; + if (w == dest) + return true; + else + QueueEnqueue(Q, w); + } + } + return false; +} + +int main(void) { + int V = 10; + Graph g = newGraph(V); + + Edge e; + e.v = 0; e.w = 1; insertEdge(g, e); + e.v = 0; e.w = 2; insertEdge(g, e); + e.v = 0; e.w = 5; insertEdge(g, e); + e.v = 1; e.w = 5; insertEdge(g, e); + e.v = 2; e.w = 3; insertEdge(g, e); + e.v = 3; e.w = 4; insertEdge(g, e); + e.v = 3; e.w = 5; insertEdge(g, e); + e.v = 3; e.w = 8; insertEdge(g, e); + e.v = 4; e.w = 5; insertEdge(g, e); + e.v = 4; e.w = 7; insertEdge(g, e); + e.v = 4; e.w = 8; insertEdge(g, e); + e.v = 5; e.w = 6; insertEdge(g, e); + e.v = 7; e.w = 8; insertEdge(g, e); + e.v = 7; e.w = 9; insertEdge(g, e); + e.v = 8; e.w = 9; insertEdge(g, e); + + int src = 0, dest = 6; + if (findPathBFS(g, V, src, dest)) { + Vertex v = dest; + while (v != src) { + printf("%d - ", v); + v = visited[v]; + } + printf("%d\n", src); + } + return 0; +} + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com diff --git a/data_structures/graphs/dfsRecursive.c b/data_structures/graphs/dfsRecursive.c new file mode 100644 index 00000000..e9e960f8 --- /dev/null +++ b/data_structures/graphs/dfsRecursive.c @@ -0,0 +1,74 @@ +#include +#include +#include "Graph.h" + +#define MAX_NODES 1000 + +int visited[MAX_NODES]; // array to store visiting order + // indexed by vertex 0..nV-1 + +bool dfsPathCheck(Graph g, int nV, Vertex v, Vertex dest) { + Vertex w; + for (w = 0; w < nV; w++) + if (adjacent(g, v, w) && visited[w] == -1) { + visited[w] = v; + if (w == dest) + return true; + else if (dfsPathCheck(g, nV, w, dest)) + return true; + } + return false; +} + +bool findPathDFS(Graph g, int nV, Vertex src, Vertex dest) { + Vertex v; + for (v = 0; v < nV; v++) + visited[v] = -1; + visited[src] = src; + return dfsPathCheck(g, nV, src, dest); +} + +int main(void) { + int V = 6; + Graph g = newGraph(V); + + Edge e; + e.v = 0; e.w = 1; insertEdge(g, e); + e.v = 0; e.w = 4; insertEdge(g, e); + e.v = 0; e.w = 5; insertEdge(g, e); + e.v = 5; e.w = 4; insertEdge(g, e); + e.v = 4; e.w = 2; insertEdge(g, e); + e.v = 4; e.w = 3; insertEdge(g, e); + e.v = 5; e.w = 3; insertEdge(g, e); + e.v = 1; e.w = 2; insertEdge(g, e); + e.v = 3; e.w = 2; insertEdge(g, e); + + int src = 0, dest = 5; + if (findPathDFS(g, V, src, dest)) { + Vertex v = dest; + while (v != src) { + printf("%d - ", v); + v = visited[v]; + } + printf("%d\n", src); + } + return 0; +} + + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com + diff --git a/data_structures/graphs/euler.c b/data_structures/graphs/euler.c new file mode 100644 index 00000000..512abb0d --- /dev/null +++ b/data_structures/graphs/euler.c @@ -0,0 +1,82 @@ +#include +#include +#include "Graph.h" + +// Return the number of vertices that v is +// connected to +int degree(Graph g, int nV, Vertex v) { + int deg = 0; + Vertex w; + for (w = 0; w < nV; w++) + if (adjacent(g, v, w)) + deg++; + return deg; +} + +// If start from vertex v, decide if the +// graph has euler path +bool hasEulerPath(Graph g, int nV, Vertex v, Vertex w) { + if (v != w) { + if (degree(g, nV, v) % 2 == 0 || degree(g, nV, w) % 2 == 0) + return false; + } else if (degree(g, nV, v) % 2 != 0) { + return false; + } + Vertex x; + for (x = 0; x < nV; x++) + if (x != v && x != w && degree(g, nV, x) % 2 != 0) + return false; + return true; +} + +int main(void) { + Edge e; + int n; + + printf("Enter the number of vertices: "); + scanf("%d", &n); + Graph g = newGraph(n); + + Vertex src, dest; + printf("Enter source node: "); + scanf("%d", &src); + printf("Enter destination node: "); + scanf("%d", &dest); + + printf("Enter an edge (from): "); + while (scanf("%d", &e.v) == 1) { + printf("Enter an edge (to): "); + scanf("%d", &e.w); + insertEdge(g, e); + printf("Enter an edge (from): "); + } + printf("Finished.\n"); + + printf("The graph has "); + if (hasEulerPath(g, n, src, dest)) + printf("an"); + else + printf("no"); + printf(" Euler path from %d to %d.\n", src, dest); + + freeGraph(g); + return 0; +} + + + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com diff --git a/data_structures/graphs/hamiltonian.c b/data_structures/graphs/hamiltonian.c new file mode 100644 index 00000000..ffe20700 --- /dev/null +++ b/data_structures/graphs/hamiltonian.c @@ -0,0 +1,87 @@ +#include +#include +#include "Graph.h" + +#define MAX_NODES 1000 + +bool visited[MAX_NODES]; + +bool hamiltonR(Graph g, int nV, Vertex v, Vertex dest, int d) { +// v = current vertex considered +// dest = destination vertex +// d = distance "remaining" until path found + + Vertex w; + if (v == dest) { + return (d == 0); + } else { + visited[v] = true; + for (w = 0; w < nV; w++) { + if (adjacent(g, v, w) && !visited[w]) { + if (hamiltonR(g, nV, w, dest, d-1)) { + return true; + } + } + } + } + visited[v] = false; + return false; +} + +bool hasHamiltonianPath(Graph g, int nV, Vertex src, Vertex dest) { + Vertex v; + for (v = 0; v < nV; v++) + visited[v] = false; + return hamiltonR(g, nV, src, dest, nV-1); +} + +int main(void) { + Edge e; + int n; + + printf("Enter the number of vertices: "); + scanf("%d", &n); + Graph g = newGraph(n); + + Vertex src, dest; + printf("Enter source node: "); + scanf("%d", &src); + printf("Enter destination node: "); + scanf("%d", &dest); + + printf("Enter an edge (from): "); + while (scanf("%d", &e.v) == 1) { + printf("Enter an edge (to): "); + scanf("%d", &e.w); + insertEdge(g, e); + printf("Enter an edge (from): "); + } + printf("Finished.\n"); + + printf("The graph has "); + if (hasHamiltonianPath(g, n, src, dest)) + printf("a"); + else + printf("no"); + printf(" Hamiltonian path from %d to %d.\n", src, dest); + + freeGraph(g); + return 0; +} + + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com diff --git a/data_structures/graphs/kruskal.c b/data_structures/graphs/kruskal.c index c1342273..4fd158a7 100644 --- a/data_structures/graphs/kruskal.c +++ b/data_structures/graphs/kruskal.c @@ -27,7 +27,7 @@ struct Graph // Creates a graph with V vertices and E edges struct Graph* createGraph(int V, int E) { - struct Graph* graph = new Graph; + struct Graph* graph = new Graph(); graph->V = V; graph->E = E; diff --git a/data_structures/graphs/queue.c b/data_structures/graphs/queue.c new file mode 100644 index 00000000..8986d5d1 --- /dev/null +++ b/data_structures/graphs/queue.c @@ -0,0 +1,88 @@ +// Queue ADT implementation ... COMP2521 + +#include +#include +#include "queue.h" + +typedef struct node { + int data; + struct node *next; +} NodeT; + +typedef struct QueueRep { + int length; + NodeT *head; + NodeT *tail; +} QueueRep; + +// set up empty queue +queue newQueue() { + queue Q = malloc(sizeof(QueueRep)); + Q->length = 0; + Q->head = NULL; + Q->tail = NULL; + return Q; +} + +// remove unwanted queue +void dropQueue(queue Q) { + NodeT *curr = Q->head; + while (curr != NULL) { + NodeT *temp = curr->next; + free(curr); + curr = temp; + } + free(Q); +} + +// check whether queue is empty +int QueueIsEmpty(queue Q) { + return (Q->length == 0); +} + +// insert an int at end of queue +void QueueEnqueue(queue Q, int v) { + NodeT *new = malloc(sizeof(NodeT)); + assert(new != NULL); + new->data = v; + new->next = NULL; + if (Q->tail != NULL) { + Q->tail->next = new; + Q->tail = new; + } else { + Q->head = new; + Q->tail = new; + } + Q->length++; +} + +// remove int from front of queue +int QueueDequeue(queue Q) { + assert(Q->length > 0); + NodeT *p = Q->head; + Q->head = Q->head->next; + if (Q->head == NULL) { + Q->tail = NULL; + } + Q->length--; + int d = p->data; + free(p); + return d; +} + + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com diff --git a/data_structures/graphs/queue.h b/data_structures/graphs/queue.h new file mode 100644 index 00000000..57692c2f --- /dev/null +++ b/data_structures/graphs/queue.h @@ -0,0 +1,26 @@ +// Queue ADT header file ... COMP2521 + +typedef struct QueueRep *queue; + +queue newQueue(); // set up empty queue +void dropQueue(queue); // remove unwanted queue +int QueueIsEmpty(queue); // check whether queue is empty +void QueueEnqueue(queue, int); // insert an int at end of queue +int QueueDequeue(queue); // remove int from front of queue + + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com diff --git a/data_structures/graphs/strongly_connected_components.c b/data_structures/graphs/strongly_connected_components.c index 1bdde2e7..1982540e 100644 --- a/data_structures/graphs/strongly_connected_components.c +++ b/data_structures/graphs/strongly_connected_components.c @@ -1,6 +1,7 @@ #include #include #define MAX_SIZE 40//Assume 40 nodes at max in graph +#define INT_MIN 0 //A vertex of the graph struct node { @@ -195,6 +196,7 @@ struct Stack* createStack() { struct Stack* stack=malloc(sizeof(struct Stack)); stack->top=-1; + return stack; } //Pushes element into stack void push(struct Stack* stack,int element) diff --git a/data_structures/graphs/topologicalSort.c b/data_structures/graphs/topologicalSort.c index eddd183e..4f3213da 100644 --- a/data_structures/graphs/topologicalSort.c +++ b/data_structures/graphs/topologicalSort.c @@ -1,6 +1,7 @@ #include #include #define MAX_SIZE 40//Assume 40 nodes at max in graph +#define INT_MIN 0 //A vertex of the graph struct node { @@ -147,6 +148,7 @@ struct Stack* createStack() { struct Stack* stack=malloc(sizeof(struct Stack)); stack->top=-1; + return stack; } //Pushes element into stack void push(struct Stack* stack,int element) diff --git a/data_structures/graphs/transitiveClosure.c b/data_structures/graphs/transitiveClosure.c new file mode 100644 index 00000000..d5936fe4 --- /dev/null +++ b/data_structures/graphs/transitiveClosure.c @@ -0,0 +1,48 @@ +#include +#include + +#define NODES 4 + +int digraph[NODES][NODES]={ {0,1,1,1}, {1,0,1,0}, {0,1,0,0}, {0,0,0,0} }; +int tc[NODES][NODES]; + +void warshall() { + int i, s, t; + for (s = 0; s < NODES; s++) + for (t = 0; t < NODES; t++) + tc[s][t] = digraph[s][t]; + + for (i = 0; i < NODES; i++) + for (s = 0; s < NODES; s++) + for (t = 0; t < NODES; t++) + if (tc[s][i] && tc[i][t]) + tc[s][t] = 1; +} + +int main(void) { + warshall(); + int i, j; + for (i = 0; i < NODES; i++) { + for (j = 0; j < NODES; j++) { + printf("%d ", tc[i][j]); + } + putchar('\n'); + } + return 0; +} + +// By +// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------. +// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. | +// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | | +// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | +// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | | +// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | | +// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | | +// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | | +// | | | || | | || | | || | | | | | | || | | | +// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' | +// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' + +// Email : z5261243@unsw.edu.au +// hhoanhtuann@gmail.com From d830970d59b25995035fa65033759a4bc6e2ac82 Mon Sep 17 00:00:00 2001 From: sagnik-chatterjee Date: Sun, 2 Feb 2020 21:30:50 +0530 Subject: [PATCH 03/19] added file for djikstars's in /greedy_approach --- greedy_approach/djikstra.c | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 greedy_approach/djikstra.c diff --git a/greedy_approach/djikstra.c b/greedy_approach/djikstra.c new file mode 100644 index 00000000..39f44258 --- /dev/null +++ b/greedy_approach/djikstra.c @@ -0,0 +1,79 @@ +#include +#include + +#define MAX 20 +#define INF 999 + +int mat[MAX][MAX]; +int V; + +int dist[MAX]; + +int q[MAX]; +int qp = 0; + +void enqueue (int v) { + q[qp++] = v; +} + +int cf (void *a, void *b) { + int *x = (int *)a; + int *y = (int *)b; + return *y - *x; +} + +int dequeue () { + qsort(q, qp, sizeof(int), cf); + return q[--qp]; +} + +int queue_has_something () { + return (qp > 0); +} + +int visited[MAX]; +int vp = 0; + +void dijkstra (int s) { + dist[s] = 0; + int i; + for (i = 0; i < V; ++i) { + if (i != s) { + dist[i] = INF; + } + enqueue(i); + } + while (queue_has_something()) { + int u = dequeue(); + visited[vp++] = u; + for (i = 0; i < V; ++i) { + if (mat[u][i]) { + if (dist[i] > dist[u] + mat[u][i]) { + dist[i] = dist[u] + mat[u][i]; + } + } + } + } +} + +int main(int argc, char const *argv[]) { + + printf("Enter the number of vertices: "); + scanf(" %d", &V); + printf("Enter the adj matrix: "); + int i, j; + for (i = 0; i < V; ++i) { + for (j = 0; j < V; ++j) { + scanf(" %d", &mat[i][j]); + } + } + + dijkstra(0); + + printf("\nNode\tDist\n"); + for (i = 0; i < V; ++i) { + printf("%d\t%d\n", i, dist[i]); + } + + return 0; +} From 6f20588fdb7d9e7d19fbc41e86430a18987f58d1 Mon Sep 17 00:00:00 2001 From: LethargicLeprechaun Date: Sat, 2 May 2020 17:52:26 +0100 Subject: [PATCH 04/19] Added octal to decimal --- conversions/octal_to_decimal.c | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 conversions/octal_to_decimal.c diff --git a/conversions/octal_to_decimal.c b/conversions/octal_to_decimal.c new file mode 100644 index 00000000..f26f7141 --- /dev/null +++ b/conversions/octal_to_decimal.c @@ -0,0 +1,38 @@ +#include +#include + +// Converts octal number to decimal +int convertValue(int num, int i) { + return num * pow(8, i); +} + +long long toDecimal(int octal_value) { + + int decimal_value = 0, i = 0; + + while (octal_value) { + + // Extracts right-most digit and then multiplies by 8^i + decimal_value += convertValue(octal_value % 10, i++); + + // Shift right in base 10 + octal_value /= 10; + } + + return decimal_value; +} + +int main() { + + printf("Enter octal value: "); + + int octal_value; + + scanf("%d", &octal_value); + + long long result = toDecimal(octal_value); + + printf("%d in decimal is %lld\n", octal_value, result); + + return 0; +} From 496a3061353c98916cf0a5612106f02799c1b35f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 3 May 2020 00:50:12 +0000 Subject: [PATCH 05/19] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 78880457..82594ba6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -23,6 +23,7 @@ * [Decimal To Octal](https://github.com/TheAlgorithms/C/blob/master/conversions/decimal_to_octal.c) * [Decimal To Octal Recursion](https://github.com/TheAlgorithms/C/blob/master/conversions/decimal_to_octal_recursion.c) * [Hexadecimal To Octal](https://github.com/TheAlgorithms/C/blob/master/conversions/hexadecimal_to_octal.c) + * [Octal To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_decimal.c) * [To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/to_decimal.c) ## Data Structures From ac13ada884f56bbe2d2cae079a0f9734a7e2dd2a Mon Sep 17 00:00:00 2001 From: Stepfen Shawn Date: Tue, 5 May 2020 09:56:03 +0800 Subject: [PATCH 06/19] Create .travis.yml --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..9f9de830 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: c +compiler: +- clang +- gcc +script: true From 0db64d5db58afb7aecf5d47a4c9228a0f5d70c07 Mon Sep 17 00:00:00 2001 From: Wesllhey Holanda Date: Sat, 9 May 2020 13:34:25 -0300 Subject: [PATCH 07/19] hash set data structure --- data_structures/hash_set/Makefile | 13 ++++ data_structures/hash_set/hash_set.c | 92 +++++++++++++++++++++++++++++ data_structures/hash_set/hash_set.h | 31 ++++++++++ data_structures/hash_set/main.c | 42 +++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 data_structures/hash_set/Makefile create mode 100644 data_structures/hash_set/hash_set.c create mode 100644 data_structures/hash_set/hash_set.h create mode 100644 data_structures/hash_set/main.c diff --git a/data_structures/hash_set/Makefile b/data_structures/hash_set/Makefile new file mode 100644 index 00000000..275be8bd --- /dev/null +++ b/data_structures/hash_set/Makefile @@ -0,0 +1,13 @@ +CC = gcc +CFLAGS = -g -Wall + +all: main + +main: main.o hash_set.o + $(CC) $(CFLAGS) $^ -o $@ + +hash_set.o: hash_set.c + $(CC) $(CFLAGS) -c $^ + +clean: + rm *.o main \ No newline at end of file diff --git a/data_structures/hash_set/hash_set.c b/data_structures/hash_set/hash_set.c new file mode 100644 index 00000000..1d340e68 --- /dev/null +++ b/data_structures/hash_set/hash_set.c @@ -0,0 +1,92 @@ +#include +#include + +#include "hash_set.h" + +extern hash_set_t *init_hash_set() +{ + hash_set_t *set = (hash_set_t *)malloc(sizeof(hash_set_t)); + set->keys = calloc(DEFAULT_HASH_SET_CAPACITY, sizeof(void **)); + set->values = calloc(DEFAULT_HASH_SET_CAPACITY, sizeof(void **)); + set->length = 0; + set->capacity = DEFAULT_HASH_SET_CAPACITY; + + return set; +} + +unsigned add(hash_set_t *set, void *value) +{ + return put(set, hash(value), value); +} + +unsigned put(hash_set_t *set, long long hash, void *value) +{ + if (contains_hash(set, hash)) { + if (set->keys[retrieve_index_from_hash(hash, set->capacity)] == value) { + return 0; + } + + // collision + resize(set); + + return put(set, hash, value); + } + + set->keys[retrieve_index_from_hash(hash, set->capacity)] = value; + set->values[set->length++] = value; + + return 1; +} + +int contains(hash_set_t *set, void *value) +{ + return set->keys[retrieve_index_from_hash(hash(value), set->capacity)] == value ? 1 : 0; +} + +int contains_hash(hash_set_t *set, long long hash) +{ + return set->keys[retrieve_index_from_hash(hash, set->capacity)] ? 1 : 0; +} + +void delete(hash_set_t *set, void *value) { + set->keys[retrieve_index_from_hash(hash(value), set->capacity)] = NULL; +} + + +// adler_32 hash +long long hash(void *value) +{ + char *str = value; + + int a = 1; + int b = 0; + const int MODADLER = 65521; + + for (int i = 0; str[i] != '\0'; i++) { + a = (a + str[i]) % MODADLER; + b = (b + a) % MODADLER; + } + + return (b << 16) | a; +} + +unsigned retrieve_index_from_hash(const long long hash, const unsigned capacity) +{ + return (capacity - 1) & (hash ^ (hash >> 12)); +} + +void resize(hash_set_t *set) +{ + void **keys_resized = calloc((set->capacity <<= 1), sizeof(void **)); + + for (int i = 0; i < set->length; i++) { + keys_resized[retrieve_index_from_hash(hash(set->values[i]), set->capacity)] = set->values[i]; + } + + free(set->keys); + + set->keys = keys_resized; + + void **new_values = (void **)realloc(set->values, set->capacity * sizeof(void **)); + set->values = new_values; +} diff --git a/data_structures/hash_set/hash_set.h b/data_structures/hash_set/hash_set.h new file mode 100644 index 00000000..cb79d24b --- /dev/null +++ b/data_structures/hash_set/hash_set.h @@ -0,0 +1,31 @@ +#ifndef __HASH_SET__ +#define __HASH_SET__ + +#define DEFAULT_HASH_SET_CAPACITY 1 << 10 + +typedef struct { + unsigned capacity; + unsigned length; + void **values; + void **keys; +} hash_set_t; + +extern hash_set_t *init_hash_set(); + +extern unsigned add(hash_set_t *set, void *value); + +unsigned put(hash_set_t *set, long long hash, void *value); + +extern int contains(hash_set_t *set, void *value); + +int contains_hash(hash_set_t *set, long long hash); + +extern void delete(hash_set_t *set, void *value); + +extern long long hash(void *value); + +extern unsigned retrieve_index_from_hash(const long long hash, const unsigned capacity); + +extern void resize(hash_set_t *set); + +#endif \ No newline at end of file diff --git a/data_structures/hash_set/main.c b/data_structures/hash_set/main.c new file mode 100644 index 00000000..6517aea7 --- /dev/null +++ b/data_structures/hash_set/main.c @@ -0,0 +1,42 @@ +#include + +#include "hash_set.h" + +int main() +{ + hash_set_t *set = init_hash_set(); + + int v1 = 10, v2 = 20, v3 = 30, v4 = 40, v5 = 50, v6 = 60, v7 = 70; + + printf("Value %d was add ? %d\n", v1, add(set, &v1)); + printf("Value %d was add ? %d\n", v1, add(set, &v1)); + printf("contains %d ? %d\n", v1, contains(set, &v1)); + + printf("Value %d was add ? %d\n", v2, add(set, &v2)); + printf("Value %d was add ? %d\n", v2, add(set, &v2)); + printf("contains %d ? %d\n", v2, contains(set, &v2)); + + printf("Value %d was add ? %d\n", v3, add(set, &v3)); + printf("Value %d is add ? %d\n", v3, add(set, &v3)); + printf("contains %d ? %d\n", v3, contains(set, &v3)); + + printf("Value %d was add ? %d\n", v4, add(set, &v4)); + printf("Value %d was add ? %d\n", v4, add(set, &v4)); + printf("contains %d ? %d\n", v4, contains(set, &v4)); + + printf("Value %d was add ? %d\n", v5, add(set, &v5)); + printf("Value %d was add ? %d\n", v5, add(set, &v5)); + printf("contains %d ? %d\n", v5, contains(set, &v5)); + + printf("Value %d is add ? %d\n", v6, add(set, &v6)); + printf("Value %d is add ? %d\n", v6, add(set, &v6)); + printf("contains %d ? %d\n", v6, contains(set, &v6)); + + printf("contains %d ? %d\n", v7, contains(set, &v7)); + + delete(set, &v6); + + printf("contains %d ? %d\n", v6, contains(set, &v6)); + + return 0; +} \ No newline at end of file From 93607e86f29ded35f23c825e6684b3cc94a53c3c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 9 May 2020 16:39:10 +0000 Subject: [PATCH 08/19] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 82594ba6..52b97280 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -51,6 +51,9 @@ * [Kruskal](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/kruskal.c) * [Strongly Connected Components](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/strongly_connected_components.c) * [Topologicalsort](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/topologicalSort.c) + * Hash Set + * [Hash Set](https://github.com/TheAlgorithms/C/blob/master/data_structures/hash_set/hash_set.c) + * [Main](https://github.com/TheAlgorithms/C/blob/master/data_structures/hash_set/main.c) * Heap * [Max Heap](https://github.com/TheAlgorithms/C/blob/master/data_structures/heap/max_heap.c) * [Min Heap](https://github.com/TheAlgorithms/C/blob/master/data_structures/heap/min_heap.c) From cf37f742489c976493f635568ab71791c72f6f7c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 11 May 2020 15:15:54 +0000 Subject: [PATCH 09/19] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 52b97280..a03ce62f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1,4 +1,6 @@ +## [872](https://github.com/TheAlgorithms/C/blob/master//872.c) + ## Client Server * [Client](https://github.com/TheAlgorithms/C/blob/master/client_server/client.c) * [Server](https://github.com/TheAlgorithms/C/blob/master/client_server/server.c) From 153b904429323f5b433a4320eb3be1f72a847860 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 14 May 2020 04:43:56 +0000 Subject: [PATCH 10/19] updating DIRECTORY.md --- DIRECTORY.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index a03ce62f..60d4a37e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -47,12 +47,19 @@ * Graphs * [Bellman-Ford](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Bellman-Ford.c) * [Bfs](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/BFS.c) + * [Bfsqueue](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/bfsQueue.c) * [Dfs](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/DFS.c) + * [Dfsrecursive](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/dfsRecursive.c) * [Dijkstra](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Dijkstra.c) + * [Euler](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/euler.c) * [Floyd-Warshall](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Floyd-Warshall.c) + * [Graph](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Graph.c) + * [Hamiltonian](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/hamiltonian.c) * [Kruskal](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/kruskal.c) + * [Queue](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/queue.c) * [Strongly Connected Components](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/strongly_connected_components.c) * [Topologicalsort](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/topologicalSort.c) + * [Transitiveclosure](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/transitiveClosure.c) * Hash Set * [Hash Set](https://github.com/TheAlgorithms/C/blob/master/data_structures/hash_set/hash_set.c) * [Main](https://github.com/TheAlgorithms/C/blob/master/data_structures/hash_set/main.c) From 03d30ca97f9e76dbf0f01a4fd5a337d9a1a683d4 Mon Sep 17 00:00:00 2001 From: Prasad Sawant <58025997+prasadsawant7@users.noreply.github.com> Date: Fri, 15 May 2020 12:29:47 +0530 Subject: [PATCH 11/19] Added definition or meaning of "Sort Function". --- data_structures/Array/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/Array/README.md b/data_structures/Array/README.md index bb037413..5534aae9 100644 --- a/data_structures/Array/README.md +++ b/data_structures/Array/README.md @@ -2,6 +2,10 @@ Simple array of integers. With I/O functions, Sort Functions and Search Functions. +#Sort Function + +The Sort function sorts the elements in the range in a particular order. The different types of sorting methods are Bubble Sort, Selection Sort, Merge Sort and Quick Sort. Bubble Sort repeatedly sorts the adjacent elements if they are in wrong order. + ## Structure ```C From 15c2ced3bac5b32d2922a9bec31989593ac8e7fe Mon Sep 17 00:00:00 2001 From: Suraj Patil Date: Sun, 17 May 2020 20:37:48 +0530 Subject: [PATCH 12/19] Fix spelling mistake --- data_structures/linked_list/singly_link_list_deletion.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/singly_link_list_deletion.c b/data_structures/linked_list/singly_link_list_deletion.c index a02b75c0..4a725233 100644 --- a/data_structures/linked_list/singly_link_list_deletion.c +++ b/data_structures/linked_list/singly_link_list_deletion.c @@ -35,7 +35,7 @@ void insert()//function to insert at first location } } /////////////////////////////////////////////////////////// -void deleteion()//function to delete from first position +void deletion()//function to delete from first position { struct node *t; if(start==NULL) @@ -85,7 +85,7 @@ int main() insert(); break; case 2: - deleteion(); + deletion(); break; case 3: viewlist(); From 104bf2cafc50b005250466f47ced56f85cede35a Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Mon, 18 May 2020 15:37:18 +0530 Subject: [PATCH 13/19] Create Circular Linked List Program to create Circular linked list with options to create, delete and traverse the list --- data_structures/linked_list/CLL.C | 155 ++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 data_structures/linked_list/CLL.C diff --git a/data_structures/linked_list/CLL.C b/data_structures/linked_list/CLL.C new file mode 100644 index 00000000..e16ecf93 --- /dev/null +++ b/data_structures/linked_list/CLL.C @@ -0,0 +1,155 @@ +/* Circularly Linked List (Basic Operations) - Program to create a Circularly linked list abstract data type and perform various operations on it (Variable first and last declared globally) */ + +#include +#include +#include +#define NULL 0 + +/* Assume that the data portion of each node consists of ONLY an integer.*/ +struct node +{ + int data ; + struct node *next ; +} ; + +struct node *first=NULL ; +struct node *last=NULL ; +/* first and last are global variables and need not be passed to any function. Any changes made to variables first and last by any of the functions in the program will be reflected in the entire program */ + +/* This function is responsible for creating the Circularly Linked List right from the BEGINING. */ +void create() +{ + int i , n ; + struct node *pnode , *p ; + + printf("Enter the number of nodes required:\n") ; + scanf("%d",&n) ; + + printf("Enter the data value of each node:\n") ; + for(i=1 ; i<=n ; i++) + { + pnode=(struct node*)malloc(sizeof(struct node)) ; + if(pnode==NULL) + { + printf("Memory overflow. Unable to create.\n") ; + return ; + } + + scanf("%d",&pnode->data) ; + + if(first==NULL) + first=last=pnode ; + else + { + last->next=pnode ; + last=pnode ; /* last keeps track of last node */ + } + + last->next=first ; + } +} + +/* This function will delete a node with value k from the Linked List if such a node exists */ +void deletenode(int k) +{ + struct node *p , *follow ; + + /* searching the required node */ + p=first ; + follow=NULL ; + while(follow!=last) + { + if(p->data==k) + break ; + follow=p ; + p=p->next ; + } + + if(follow==last) + printf("Required node not found.\n") ; + else + { + if(p==first&&p==last) /* deleting the one and the only node */ + first=last=NULL ; + else if(p==first) /* deleting the first node */ + { + first=first->next ; + last->next=first ; + } + else if(p==last) /* deleting the last node */ + { + last=follow ; + last->next=first ; + } + else /* deleting any other node */ + follow->next=p->next ; + + free(p) ; + } +} + +/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */ +void traverse() +{ + struct node *p , *follow ; + if(first==NULL) + printf("Circularly Linked List Empty") ; + else + { + printf("Circularly Linked List is as shown: \n") ; + + p=first ; + follow = NULL ; + while(follow!=last) + { + printf("%d " , p->data) ; + follow=p ; + p=p->next ; + } + + printf("\n") ; + } +} + +void main() +{ + int x , k , ch ; + clrscr() ; + do + { + printf("\n Menu: \n") ; + printf("1:Create Linked List \n") ; + printf("2:Delete Node \n") ; + printf("3:Traverse \n") ; + printf("4:Exit \n") ; + + printf("\nEnter your choice: ") ; + scanf("%d",&ch) ; + + switch(ch) + { + case 1: + create() ; + break ; + + case 2: + printf("Enter the data value of the node to be deleted: ") ; + scanf("%d",&k) ; + deletenode(k) ; + break ; + + case 3: + traverse() ; + break ; + + case 4: + break ; + } + } + while(ch!=4) ; + + getch() ; +} + + + From a3e1817738145a5d0989b9b18ded09aa8ac21948 Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Mon, 18 May 2020 15:38:30 +0530 Subject: [PATCH 14/19] Rename CLL.C to CircularLinkedList.C --- .../{CLL.C => CircularLinkedList.C} | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) rename data_structures/linked_list/{CLL.C => CircularLinkedList.C} (95%) diff --git a/data_structures/linked_list/CLL.C b/data_structures/linked_list/CircularLinkedList.C similarity index 95% rename from data_structures/linked_list/CLL.C rename to data_structures/linked_list/CircularLinkedList.C index e16ecf93..7297217a 100644 --- a/data_structures/linked_list/CLL.C +++ b/data_structures/linked_list/CircularLinkedList.C @@ -1,20 +1,20 @@ -/* Circularly Linked List (Basic Operations) - Program to create a Circularly linked list abstract data type and perform various operations on it (Variable first and last declared globally) */ +/* Circularly Linked List (Basic Operations) - Program to create a Circularly linked list abstract data type and perform various operations on it (Variable first and last declared globally) */ -#include -#include +#include +#include #include -#define NULL 0 +#define NULL 0 -/* Assume that the data portion of each node consists of ONLY an integer.*/ +/* Assume that the data portion of each node consists of ONLY an integer.*/ struct node { int data ; struct node *next ; } ; - -struct node *first=NULL ; -struct node *last=NULL ; -/* first and last are global variables and need not be passed to any function. Any changes made to variables first and last by any of the functions in the program will be reflected in the entire program */ + +struct node *first=NULL ; +struct node *last=NULL ; +/* first and last are global variables and need not be passed to any function. Any changes made to variables first and last by any of the functions in the program will be reflected in the entire program */ /* This function is responsible for creating the Circularly Linked List right from the BEGINING. */ void create() @@ -54,15 +54,15 @@ void deletenode(int k) { struct node *p , *follow ; - /* searching the required node */ - p=first ; - follow=NULL ; - while(follow!=last) - { - if(p->data==k) - break ; - follow=p ; - p=p->next ; + /* searching the required node */ + p=first ; + follow=NULL ; + while(follow!=last) + { + if(p->data==k) + break ; + follow=p ; + p=p->next ; } if(follow==last) @@ -82,7 +82,7 @@ void deletenode(int k) last->next=first ; } else /* deleting any other node */ - follow->next=p->next ; + follow->next=p->next ; free(p) ; } @@ -96,7 +96,7 @@ void traverse() printf("Circularly Linked List Empty") ; else { - printf("Circularly Linked List is as shown: \n") ; + printf("Circularly Linked List is as shown: \n") ; p=first ; follow = NULL ; @@ -105,7 +105,7 @@ void traverse() printf("%d " , p->data) ; follow=p ; p=p->next ; - } + } printf("\n") ; } @@ -146,10 +146,10 @@ void main() break ; } } - while(ch!=4) ; + while(ch!=4) ; getch() ; } - + From bc3f26c1412b7c798e11d86da60eeb106cf39cbd Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 18 May 2020 10:57:31 +0000 Subject: [PATCH 15/19] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 60d4a37e..e8b5759e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -98,6 +98,9 @@ * Word Count * [Word Count](https://github.com/TheAlgorithms/C/blob/master/exercism/word_count/word_count.c) +## Greedy Approach + * [Djikstra](https://github.com/TheAlgorithms/C/blob/master/greedy_approach/djikstra.c) + ## Hash * [Hash](https://github.com/TheAlgorithms/C/blob/master/hash/hash.c) * [Test Program](https://github.com/TheAlgorithms/C/blob/master/hash/test_program.c) From 8e8e8df40698ef927d23a2d2b3d7970b4d97a4de Mon Sep 17 00:00:00 2001 From: Sagnik Chatterjee Date: Mon, 18 May 2020 19:13:28 +0530 Subject: [PATCH 16/19] added heap to readme; missing in the master README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 912c5e2f..0aabeabb 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,9 @@ For a full list of all algorithms, please see: [DIRECTORY.md](https://github.com - recursive_traversals - trie - trie + - heap + - min heap + - max heap ## Searching From cf696ddc6d946442e42a8d936602df9c081267f1 Mon Sep 17 00:00:00 2001 From: Sai Sriram Kalavagunta Date: Wed, 20 May 2020 15:40:14 +0530 Subject: [PATCH 17/19] Modified code of misc/Factorial.c to accomodate larger numbers. --- misc/Factorial.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/misc/Factorial.c b/misc/Factorial.c index e8885284..f85077e8 100644 --- a/misc/Factorial.c +++ b/misc/Factorial.c @@ -1,20 +1,32 @@ -#include +#include +int main() +{ + int a[200],n,counter,temp,i; + a[0]=1; + counter=0; + printf("Enter a whole number to Find its Factorial: "); + scanf("%d",&n); + if(n<0) + printf("Cannot Calculate factorials for negative numbers."); + else{ + for(; n>=2; n--) + { + temp=0; + for(i=0; i<=counter; i++) + { + temp=(a[i]*n)+temp; + a[i]=temp%10; + temp=temp/10; + } + while(temp>0) + { + a[++counter]=temp%10; + temp=temp/10; + } + } + for(i=counter; i>=0; i--) + printf("%d",a[i]); + } + return 0; -int fat(int number){ - if (number == 1 || number == 0) - return 1; - else - return number*fat(number-1); -} - -int main(){ - int number; - - //Asks the factorial of the number n - printf("Number: "); - scanf("%d", &number); - - printf("%d\n", fat(number)); - - return 0; } From 0de20fc55581305cf3ca55155ee819d9ab258973 Mon Sep 17 00:00:00 2001 From: Stepfen Shawn Date: Thu, 21 May 2020 22:54:01 +0800 Subject: [PATCH 18/19] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0aabeabb..21e09760 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Build Status](https://travis-ci.com/TheAlgorithms/C.svg?branch=master)](https://travis-ci.com/TheAlgorithms/C) C ======== From d1f6643355830d9afe4e59e200abe85cb2388616 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 22 May 2020 15:08:33 +0000 Subject: [PATCH 19/19] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index e8b5759e..5066685c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -67,6 +67,7 @@ * [Max Heap](https://github.com/TheAlgorithms/C/blob/master/data_structures/heap/max_heap.c) * [Min Heap](https://github.com/TheAlgorithms/C/blob/master/data_structures/heap/min_heap.c) * Linked List + * [Circularlinkedlist](https://github.com/TheAlgorithms/C/blob/master/data_structures/linked_list/CircularLinkedList.C) * [Merge Linked Lists](https://github.com/TheAlgorithms/C/blob/master/data_structures/linked_list/merge_linked_lists.c) * [Middle Element In List](https://github.com/TheAlgorithms/C/blob/master/data_structures/linked_list/middle_element_in_list.c) * [Singly Link List Deletion](https://github.com/TheAlgorithms/C/blob/master/data_structures/linked_list/singly_link_list_deletion.c)