mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 06:49:36 +03:00
Merge remote-tracking branch 'upstream/master'
# Conflicts: # .travis.yml # DIRECTORY.md # README.md
This commit is contained in:
commit
6385b0223e
43
872.c
Normal file
43
872.c
Normal file
@ -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;i<num_of_node1; i++){
|
||||
if (sequence1[i] != sequence2[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int sequence(struct TreeNode* root ,int *list , int num_of_node){
|
||||
|
||||
if (!root)
|
||||
return num_of_node;
|
||||
|
||||
if (!root->left && !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;
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
[![Build Status](https://travis-ci.org/kvedala/C.svg?branch=master)](https://travis-ci.org/kvedala/C)
|
||||
C
|
||||
========
|
||||
|
||||
For a full list of all algorithms, please see: [DIRECTORY.md](https://github.com/TheAlgorithms/C/blob/master/DIRECTORY.md)
|
||||
|
||||
@ -41,6 +40,9 @@ All the code can be executed and tested online: [![using Google Colab Notebook](
|
||||
- recursive_traversals
|
||||
- trie
|
||||
- trie
|
||||
- heap
|
||||
- min heap
|
||||
- max heap
|
||||
|
||||
|
||||
## Searching
|
||||
|
38
conversions/octal_to_decimal.c
Normal file
38
conversions/octal_to_decimal.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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;
|
||||
}
|
@ -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
|
||||
|
102
data_structures/graphs/Graph.c
Normal file
102
data_structures/graphs/Graph.c
Normal file
@ -0,0 +1,102 @@
|
||||
// Graph ADT
|
||||
// Adjacency Matrix Representation
|
||||
#include "Graph.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
37
data_structures/graphs/Graph.h
Normal file
37
data_structures/graphs/Graph.h
Normal file
@ -0,0 +1,37 @@
|
||||
// Graph ADT interface ... COMP2521
|
||||
#include <stdbool.h>
|
||||
|
||||
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
|
56
data_structures/graphs/Makefile
Normal file
56
data_structures/graphs/Makefile
Normal file
@ -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
|
81
data_structures/graphs/bfsQueue.c
Normal file
81
data_structures/graphs/bfsQueue.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#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
|
74
data_structures/graphs/dfsRecursive.c
Normal file
74
data_structures/graphs/dfsRecursive.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#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
|
||||
|
82
data_structures/graphs/euler.c
Normal file
82
data_structures/graphs/euler.c
Normal file
@ -0,0 +1,82 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#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
|
87
data_structures/graphs/hamiltonian.c
Normal file
87
data_structures/graphs/hamiltonian.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#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
|
@ -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;
|
||||
|
||||
|
88
data_structures/graphs/queue.c
Normal file
88
data_structures/graphs/queue.c
Normal file
@ -0,0 +1,88 @@
|
||||
// Queue ADT implementation ... COMP2521
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#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
|
26
data_structures/graphs/queue.h
Normal file
26
data_structures/graphs/queue.h
Normal file
@ -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
|
@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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)
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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)
|
||||
|
48
data_structures/graphs/transitiveClosure.c
Normal file
48
data_structures/graphs/transitiveClosure.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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
|
13
data_structures/hash_set/Makefile
Normal file
13
data_structures/hash_set/Makefile
Normal file
@ -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
|
92
data_structures/hash_set/hash_set.c
Normal file
92
data_structures/hash_set/hash_set.c
Normal file
@ -0,0 +1,92 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
31
data_structures/hash_set/hash_set.h
Normal file
31
data_structures/hash_set/hash_set.h
Normal file
@ -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
|
42
data_structures/hash_set/main.c
Normal file
42
data_structures/hash_set/main.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
155
data_structures/linked_list/CircularLinkedList.C
Normal file
155
data_structures/linked_list/CircularLinkedList.C
Normal file
@ -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 <stdio.h>
|
||||
#include <conio.h>
|
||||
#include <stdlib.h>
|
||||
#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() ;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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();
|
||||
|
79
greedy_approach/djikstra.c
Normal file
79
greedy_approach/djikstra.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
@ -1,20 +1,32 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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));
|
||||
|
||||
#include<stdio.h>
|
||||
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;
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user