8000 Merge pull request #1185 from VAR-solutions/master · glitches-coder/Algorithms@ecf563d · GitHub
[go: up one dir, main page]

Skip to content

Commit ecf563d

Browse files
authored
Merge pull request VAR-solutions#1185 from VAR-solutions/master
Updated Devel
2 parents a4c3c78 + d072efe commit ecf563d

File tree

8 files changed

+694
-0
lines changed

8 files changed

+694
-0
lines changed

Blockchain/blockchain_algorithm.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from hashlib import sha256
2+
import time
3+
4+
class block:
5+
def __init__ (self, timestamp, data, previousHash = ' '):
6+
self.timestamp = timestamp
7+
self.data = data
8+
self.previousHash = previousHash
9+
self.hash = self.calculateHash()
10+
11+
def calculateHash(self):
12+
return sha256((str(self.timestamp) + str(self.data) + str(self.previousHash)).encode()).hexdigest()
13+
14+
15+
class blockchain:
16+
def __init__(self):
17+
self.chain = [self.createGenesis()]
18+
19+
def createGenesis(self):
20+
return block(time.ctime(), "genesisBlock", "00000")
21+
22+
def mineBlock(self, data):
23+
node = block(time.ctime(), data, self.chain[-1].hash)
24+
# mining a new block to the blockchain
25+
self.chain.append(node)
26+
27+
def printBlockchain(self):
28+
for i in range(len(self.chain)):
29+
print("\n-----Block ", i ,"---------\n timestamp = "\
30+
, self.chain[i].timestamp,"\n data = ", \
31+
self.chain[i].data, "\n previousHash = ",\
32+
self.chain[i].previousHash,"\n hash = ", \
33+
self.chain[i].hash)
34+
35+
36+
37+
CEVcoin = blockchain()
38+
39+
data = input()
40+
41+
# sending data to get mined
42+
print("\n\n ----> Mining New Block -->")
43+
CEVcoin.mineBlock(data)
44+
45+
print("\n\n ----> New Block mined successfully --> ")
46+
47+
CEVcoin.printBlockchain()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define MAX_DEGREE 5
5+
#define MAX_NUM_VERTICES 20
6+
7+
struct vertices_s {
8+
int visited;
9+
int deg;
10+
int adj[MAX_DEGREE]; /* < 0 if incoming edge */
11+
} vertices[] = {
12+
{0, 3, {2, -3, 4}},
13+
{0, 2, {-1, 3}},
14+
{0, 3, {1, -2, 7}},
15+
{0, 3, {-1, -5, 6}},
16+
{0, 2, {4, -7}},
17+
{0, 3, {-4, 7, -8}},
18+
{0, 4, {-3, 5, -6, -12}},
19+
{0, 3, {6, -9, 11}},
20+
{0, 2, {8, -10}},
21+
{0, 3, {9, -11, -12}},
22+
{0, 3, {-8, 10, 12}},
23+
{0, 3, {7, 10, -11}}
24+
};
25+
int num_vertices = sizeof(vertices) / sizeof(vertices[0]);
26+
27+
struct stack_s {
28+
int top;
29+
int items[MAX_NUM_VERTICES];
30+
} stack = {-1, {}};
31+
32+
void stack_push(int v) {
33+
stack.top++;
34+
if (stack.top < MAX_NUM_VERTICES)
35+
stack.items[stack.top] = v;
36+
else {
37+
printf("Stack is full!\n");
38+
exit(1);
39+
}
40+
}
41+
42+
int stack_pop() {
43+
return stack.top < 0 ? -1 : stack.items[stack.top--];
44+
}
45+
46+
void dfs(int v, int transpose) {
47+
int i, c, n;
48+
vertices[v].visited = 1;
49+
for (i = 0, c = vertices[v].deg; i < c; ++i) {
50+
n = vertices[v].adj[i] * transpose;
51+
if (n > 0)
52+
/* n - 1 because vertex indexing begins at 0 */
53+
if (!vertices[n - 1].visited)
54+
dfs(n - 1, transpose);
55+
}
56+
if (transpose < 0)
57+
stack_push(v);
58+
else
59+
printf("%d ", v + 1);
60+
}
61+
62+
void reset_visited() {
63+
int i;
64+
for (i = 0; i < num_vertices; ++i)
65+
vertices[i].visited = 0;
66+
}
67+
68+
void order_pass() {
69+
int i;
70+
for (i = 0; i < num_vertices; ++i)
71+
if (!vertices[i].visited)
72+
dfs(i, -1);
73+
}
74+
75+
void scc_pass() {
76+
int i = 0, v;
77+
while((v = stack_pop()) != -1) {
78+
if (!vertices[v].visited) {
79+
printf("scc %d: ", ++i);
80+
dfs(v, 1);
81+
printf("\n");
82+
}
83+
}
84+
}
85+
86+
int main(void) {
87+
order_pass();
88+
reset_visited();
89+
scc_pass();
90+
return 0;
91+
}

LinkedList/C/linkedList.c

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
// Define the node and declare head, temp variables.
5+
6+
struct node{
7+
int data;
8+
struct node* link;
9+
}*head, *temp;
10+
11+
//Function to insert at beginning
12+
void begins(int x){
13+
struct node* new = (struct node*)malloc(sizeof(struct node));
14+
new -> data = x;
15+
if (head == NULL){
16+
head = new;
17+
head -> link = NULL;
18+
return;
19+
}
20+
new -> link = head;
21+
head = new;
22+
}
23+
24+
//Function to insert at end
25+
void endins(int x){
26+
struct node *new, *ptr;
27+
new = (struct node*)malloc(sizeof(struct node));
28+
new -> data = x;
29+
if (head == NULL){
30+
head = new;
31+
head -> link = NULL;
32+
return;
33+
}
34+
ptr = head;
35+
while(ptr -> link != NULL){
36+
ptr = ptr -> link;
37+
}
38+
ptr -> link = new;
39+
new -> link = NULL;
40+
}
41+
42+
//Function to insert at a position
43+
void posins(int x, int pos){
44+
struct node *new, *ptr;
45+
new = (struct node*)malloc(sizeof(struct node));
46+
new -> data = x;
47+
if (head == NULL){
48+
head = new;
49+
head -> link = NULL;
50+
return;
51+
}
52+
ptr = head;
53+
int count = 0;
54+
while(count != pos && ptr -> link != NULL){
55+
ptr = ptr -> link;
56+
count++;
57+
}
58+
new -> link = ptr -> link;
59+
ptr -> link = new;
60+
}
61+
62+
//Function to insert before a specific data node
63+
void datains(int x, int data){
64+
struct node *new, *ptr, *ptr1;
65+
new = (struct node*)malloc(sizeof(struct node));
66+
new -> data = x;
67+
if (head == NULL){
68+
head = new;
69+
head -> link = NULL;
70+
return;
71+
}
72+
ptr = head;
73+
while(ptr -> data != data && ptr -> link != NULL){
74+
ptr1 = ptr;
75+
ptr = ptr -> link;
76+
}
77+
new -> link = ptr;
78+
ptr1 -> link = new;
79+
}
80+
81+
//Function to delete at beginning
82+
void begdel(){
83+
if (head == NULL){
84+
printf("LIST ALREADY EMPTY\n");
85+
return ;
86+
}
87+
temp = head;
88+
head = head -> link;
89+
free(temp);
90+
}
91+
92+
//Function to delete at end
93+
void enddel(){
94+
if (head == NULL){
95+
printf("LIST ALREADY EMPTY\n");
96+
return ;
97+
}
98+
struct node* ptr = head;
99+
while(ptr -> link -> link != NULL){
100+
ptr = ptr -> link;
101+
}
102+
temp = ptr -> link;
103+
ptr -> link = NULL;
104+
free(temp);
105+
}
106+
107+
//Function to delete at a position
108+
void posdel(int pos){
109+
if (head == NULL){
110+
printf("LIST ALREADY EMPTY\n");
111+
return ;
112+
}
113+
int count = 0;
114+
struct node* ptr = head, *ptr1;
115+
while(count != pos && ptr -> link != NULL){
116+
ptr1 = ptr;
117+
ptr = ptr -> link;
118+
count++;
119+
}
120+
temp = ptr;
121+
ptr1 -> link = ptr -> link;
122+
free(temp);
123+
}
124+
125+
//Function to delete before a specific data node
126+
void datadel(int data){
127+
if (head == NULL){
128+
printf("LIST ALREADY EMPTY\n");
129+
return ;
130+
}
131+
struct node* ptr = head, *ptr1;
132+
while(ptr -> link -> data != data && ptr -> link != NULL){
133+
ptr1 = ptr;
134+
ptr = ptr -> link;
135+
}
136+
temp = ptr;
137+
ptr1 -> link = ptr -> link;
138+
free(temp);
139+
}
140+
141+
//Function to neatly display the list
142+
void disp(){
143+
printf("\n======================================================\n");
144+
struct node* ptr = head;
145+
while (ptr != NULL){
146+
printf("%d ", ptr -> data);
147+
ptr = ptr -> link;
148+
}
149+
printf("\n======================================================\n");
150+
}
151+
152+
int main(){
153+
int c, x, pos;
154+
while(1){
155+
printf("1. Beg Ins\n2. End Ins\n3. Beg Del\n4. End Del\n5. Pos Ins\n6. Data Ins\n7. Pos Del\n8. Data Del\nEnter your choice: ");
156+
scanf("%d", &c);
157+
switch(c){
158+
case 1: printf("Enter the data to be inserted: ");
159+
scanf("%d", &x);
160+
begins(x);
161+
break;
162+
case 2: printf("Enter the data to be inserted: ");
163+
scanf("%d", &x);
164+
endins(x);
165+
break;
166+
case 3: begdel();
167+
break;
168+
case 4: enddel();
169+
break;
170+
case 5: printf("Enter the Position and Data: ");
171+
scanf("%d%d", &pos, &x);
172+
if(pos == 0){
173+
begins(x);
174+
break;
175+
}
176+
posins(x, pos-1);
177+
break;
178+
case 6: printf("Enter the insert-before-data data and data: ");
179+
scanf("%d%d", &pos, &x);
180+
datains(x, pos);
181+
break;
182+
case 8: printf("Enter the delete-before-data data: ");
183+
scanf("%d", &pos);
184+
datadel(pos);
185+
break;
186+
case 7: printf("Enter the Position: ");
187+
scanf("%d", &pos);
188+
if(pos == 0){
189+
begdel();
190+
break;
191+
}
192+
posdel(pos-1);
193+
break;
194+
default:printf("INVALID CHOICE\n");
195+
}
196+
disp();
197+
}
198+
return 0;
199+
}

0 commit comments

Comments
 (0)
0