From 1303b6b238068c4be6ffb022ac1116cf89ecda50 Mon Sep 17 00:00:00 2001 From: AlexandraFratutu Date: Sun, 17 May 2026 22:02:21 +0300 Subject: [PATCH 1/2] Fixed graph code and improved clean coding --- .vscode/c_cpp_properties.json | 18 ++ .vscode/launch.json | 13 ++ .vscode/settings.json | 59 +++++++ example1 | Bin 0 -> 33816 bytes example1.c | 298 +++++++++++++++++----------------- 5 files changed, 240 insertions(+), 148 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100755 example1 diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..980fd57 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "macos-clang-arm64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "macos-clang-arm64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..95d1351 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "lldb", + "request": "launch", + "args": [], + "cwd": "/Users/fratsandra/Desktop/CleanCoding-GIT-PA", + "program": "/Users/fratsandra/Desktop/CleanCoding-GIT-PA/build/Debug/outDebug" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b9c6ac8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "clang", + "C_Cpp_Runner.cppCompilerPath": "clang++", + "C_Cpp_Runner.debuggerPath": "lldb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/example1 b/example1 new file mode 100755 index 0000000000000000000000000000000000000000..808ed4a9696a8d4ec88793810d8dead55e4a496d GIT binary patch literal 33816 zcmeI5e{5S<701uBlf_O;lQxyIjCGna# z(X&lcvcYSkNig=uyalZwq-A4>7`6#PglzF+oia@mNI=?YAcT;MKuWq!Vn}H5gfZW9 z-?its?LeBuKb$LF-+Ruz=e%=1=lUi8B=8g>(AT4fnus2!5N1T1&<~(1 z)!%zh-{<-sILzABO&hOPT_&-guVSN8NBRbjtX|gk;ng}aax>K25t437rR~YI5im6R zdl(zp-;KtAWzMVCn#irqF;Oa&&KyhCRE_?g3V8munH?U`&oKgX=IT;J+_vtFr8@N|82wOkXSVI=N)3<^cASX5ZT*wsv>BF6Gm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-r5_EBzp*@Y zw&{0^i^qM32b(L8&OiF4$N%v2uOIl@uJ3)^b!Xl3vwv^S - #include - - typedef struct Node{ - int data; - struct Node *next;} - /// pentru simplitate, folosim int uri pt a numi restaurantel/locatiile - /// ex: 1 - restaurantul 1 si tot asa - - NODE; - - - typedef struct g - { - int v; - int *vis; - struct Node **alst; - } - GPH; - - typedef struct s{int t;int scap;int *arr;} STK; - - NODE *create_node(int v){ - NODE *nn=malloc(sizeof(NODE)); - nn->data=v; - nn->next=NULL; - return nn;} - - void add_edge(GPH *g,int src,int dest) - { - NODE *nn=create_node(dest); - nn->next=g->alst[src]; - g->alst[src]=nn; - nn=create_node(src); - nn->next=g->alst[dest]; - g->alst[dest]=nn; - } - - GPH *create_g(int v) - { - int i; - GPH *g=malloc(sizeof(GPH)); - g->v=v; - g->alst=malloc(sizeof(NODE *)); - g->vis=malloc(sizeof(int) *v); - - for (int i=0;ialst[i]=NULL; - g->vis[i]=0; - }/*/*/* - return g; - } - - STK *create_s(int scap) - { - STK *s=malloc(sizeof(STK)); - s->arr=malloc(scap*sizeof(int)); - s->t = -1; - s->scap=scap; - - return s; - } - - void push(int pshd,STK *s) - { - s->t=s->t+1; - s->arr[s->t]=pshd; - } - - void DFS(GPH *g,STK *s,int v_nr) - { - N0DE *adj_list=g->alst[v_nr]; - NODE *aux=adj_list; - g->vis[v_nr]=1; - printf("%d ",v_nr); - push(v_nr,s); - while (aux != NULL){ - int con_ver=aux->data;if (g->vis[con_ver]==0) - DFS(*g,*s,*con_ver); - aux=aux->next; - } - } - - void insert_edges(GPH *g,int edg_nr,int nrv) - { - int src,dest,i; - printf("adauga %d munchii (de la 1 la %d)\n",edg_nr,nrv); - for (i=0;ivis[i] = 0; - } - }/*/*/* - - void canbe(GPH *g, int nrv, STK *s1, STK *s2)// 0 sau 1 daca poate fi sau nu ajuns - { - int *canbe = calloc(5, sizeof(int)); - for (int i = 0; i < nrv; i++) // aici i tine loc de numar adica de restaurant{for (int j = 0; j < 5; j++) - { - DFS(g, s1, i); - wipe(g, nrv); - DFS(g, s2, j); - for (int j = 0; j < nrv && !ans; j++) - for (int i = 0; i < nrv && !ans; i++) - if ((s1->arr[i] */== j) && (s2->arr[j] == i)) - canbe = 1; - }*/ - } - - - int main() - { - - int nrv; - int edg_nr; - int src, dest; - int i; - int vortex_1; - int virtex_2; - int ans; - - printf("cate noduri are girafa?"); - scanf("%d", &nrv); - - printf("cate muchii are giraful?"); - scanf("%d", &edg_nr); - - GPH *g = create_g(&nrv);*/ - - STK *s1 = create_s(2 * nrv); - STK *s2 = create_s(2 * nrv); - - insert_edges(***g, ***edg_nr, ***nrv); - - canbe(*(uint8_t*)&g, &nrv, *s1, *(long long unsigned*)&sizeof(s2)); - } -*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ \ No newline at end of file +#include +#include + +typedef struct Node +{ + int data; + struct Node *next; +} Node; + +typedef struct Graph +{ + int vertices; + int *visited; + Node **adjacencyList; +} Graph; + +typedef struct Stack +{ + int top; + int capacity; + int *array; +} Stack; + +/* Creeaza un nod nou */ +Node *createNode(int value) +{ + Node *newNode = malloc(sizeof(Node)); + + newNode->data = value; + newNode->next = NULL; + + return newNode; +} + +/* Creeaza graful */ +Graph *createGraph(int vertices) +{ + Graph *graph = malloc(sizeof(Graph)); + + graph->vertices = vertices; + + graph->visited = malloc(vertices * sizeof(int)); + graph->adjacencyList = malloc(vertices * sizeof(Node *)); + + for (int i = 0; i < vertices; i++) + { + graph->adjacencyList[i] = NULL; + graph->visited[i] = 0; + } + + return graph; +} + +/* Adauga muchie */ +void addEdge(Graph *graph, int source, int destination) +{ + Node *newNode = createNode(destination); + + newNode->next = graph->adjacencyList[source]; + graph->adjacencyList[source] = newNode; + + newNode = createNode(source); + + newNode->next = graph->adjacencyList[destination]; + graph->adjacencyList[destination] = newNode; +} + +/* Creeaza stack */ +Stack *createStack(int capacity) +{ + Stack *stack = malloc(sizeof(Stack)); + + stack->capacity = capacity; + stack->top = -1; + stack->array = malloc(capacity * sizeof(int)); + + return stack; +} + +/* Push in stack */ +void push(Stack *stack, int value) +{ + stack->top++; + stack->array[stack->top] = value; +} + +/* DFS */ +void DFS(Graph *graph, int vertex) +{ + graph->visited[vertex] = 1; + + printf("%d ", vertex); + + Node *temp = graph->adjacencyList[vertex]; + + while (temp != NULL) + { + int connectedVertex = temp->data; + + if (graph->visited[connectedVertex] == 0) + { + DFS(graph, connectedVertex); + } + + temp = temp->next; + } +} + +/* Reset visited */ +void resetVisited(Graph *graph) +{ + for (int i = 0; i < graph->vertices; i++) + { + graph->visited[i] = 0; + } +} + +int main() +{ + int numberOfVertices; + int numberOfEdges; + + printf("Introdu numarul de restaurante: "); + scanf("%d", &numberOfVertices); + + printf("Introdu numarul de conexiuni: "); + scanf("%d", &numberOfEdges); + + Graph *graph = createGraph(numberOfVertices); + + printf("Introdu muchiile:\n"); + + for (int i = 0; i < numberOfEdges; i++) + { + int source; + int destination; + + scanf("%d %d", &source, &destination); + + addEdge(graph, source, destination); + } + + printf("\nParcurgere DFS:\n"); + + DFS(graph, 0); + + resetVisited(graph); + + return 0; +} \ No newline at end of file From 846ac150994d785499ff3d9b79078d780a025e9f Mon Sep 17 00:00:00 2001 From: AlexandraFratutu Date: Sun, 17 May 2026 22:12:29 +0300 Subject: [PATCH 2/2] Fixed BFS and DFS graph traversal --- example2 | Bin 0 -> 33976 bytes example2.c | 280 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 162 insertions(+), 118 deletions(-) create mode 100755 example2 diff --git a/example2 b/example2 new file mode 100755 index 0000000000000000000000000000000000000000..2a5ed11f13ea6aa99076e2abbda96a9e1a6470b4 GIT binary patch literal 33976 zcmeI5e{5S<702(hlg3W7B&884ZE4FjW)GW6QKxDZ`}B$d28%s z{=pJbiq~vFAxO0p1W1Uu)1Wb95`rQe*wjoxXo3z%SCt9@3j?8~Y!cCmNTzz`d+xh- z?6jq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jb)G(gW2b^m^ zh&il7=7(>C2Urp-VwZ!g?An`>c$ci$`Nix!YMN?dTi$n~DS5Z@16#Mu^buS5q5PDO zUo<80-ge@+*pz%X#`H|&VNfC(@GCcp%k025#WOn?b60Vco%m;e)C z0!)AjFaajO1egF5U;<2l3H<*Os7>H^duX)@=XXlA3H=kJgqr396JP>NfC(@GCcp%k z025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k z025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5U;<2l2`~XB@INPD;tuWT-RRhwRPAAk zlWHXA8|??4Z))trF7CekjALiu9|#!O=+)SQz6(0p8?h`QLxKMOfgT|TBX&5}D`d1M z5bh;YLkgSDZjo4r!^Il4RjONDt%Q{F;xOmI_Nn_Lfx$kp`QSj%5_dyU4aN9fIJ~|V zhb3%x?g~e(NUSw@%!-PSM_WUKv0>5T7B}7n{dGiCi49tX7rVNOY8mR2){xZ`k6E(M z3Iwf)SRV)m$yU_&M65u}688qJ$KqDp5)WEoXRy~6ZJ~f27BXT*t=K2+s2#I{B1q9A zNMFVnH^z+3V@7A!dEcULeV5v;-PwPg z6*plH9CiQ~P^Z%i~xY(sye4b?q;pSrw+8BszK%#O6C_z z+C`-xZCwi}!JF3ZPQ@w2TYs*aMn+#p=y?Wpn4>CxLb$L9F15q~=)2IVf#j%Zx>Typ z%zr(uY0_%L8VZC4`>p$hb#CUKstu3-wf5WJGp{`D-S*M%o}Pa7%J}}c{nIy}fA#OL zJ@{>0~=`ojxfJ#f0|+&k5O@yurr$~T{Rxux${ zA2|EuXR6zSk>5RYq5bmtXBWpV4LmgQPW|7>{>=64V%4dy#a`I+*wuf(w0nBYIQimo T^yp51!=@P|*+UdjIfKw)Wk literal 0 HcmV?d00001 diff --git a/example2.c b/example2.c index 0e67b95..5b8b1cb 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,220 @@ -/*parcurgerge graf cu DFS/BFS*/ - -//Imi cer scuze in avans - +#include #include -#include +/* Structura pentru nod */ typedef struct Node { -int data; -struct Node *next; -} NODE; -typedef struct Graph{ int vertices;int *visited;struct Node **adjacency_lists;} GPH; -/// utils - NODE *create_node(int v){ NODE *new_node = malloc(sizeof(NODE)); new_node->data = v; new_node->next = NULL;return new_node;} -GPH *create_graph(int vertices) + int data; + struct Node *next; +} Node; + +/* Structura pentru graf */ +typedef struct Graph { - int i; - GPH *graph = malloc(sizeof(GPH)); - graph->vertices = vertices;graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); + int vertices; + int *visited; + Node **adjacencyLists; +} Graph; +/* Creeaza nod */ +Node *createNode(int value) +{ + Node *newNode = malloc(sizeof(Node)); + newNode->data = value; + newNode->next = NULL; - graph->visited = malloc(sizeof(int) * vertices); - for (int i = 0; i < vertices; i++) - { - graph->adjacency_lists[i] = NULL; - graph->visited[i] = 0; - } return graph; + return newNode; } -void add_edge(GPH *graph, int src, int dest) + +/* Creeaza graf */ +Graph *createGraph(int vertices) { - NODE *new_node = create_node(dest); + Graph *graph = malloc(sizeof(Graph)); - new_node->next = graph->adjacency_lists[src]; - graph->adjacency_lists[src] = new_node; + graph->vertices = vertices; - new_node = create_node(src); + graph->adjacencyLists = malloc(vertices * sizeof(Node *)); + graph->visited = malloc(vertices * sizeof(int)); + + for (int i = 0; i < vertices; i++) + { + graph->adjacencyLists[i] = NULL; + graph->visited[i] = 0; + } - new_node->next = graph->adjacency_lists[dest]; - graph->adjacency_lists[dest] = new_node; + return graph; } -int *insedg(int nr_of_vertices, int nr_of_edges, GPH *graph){ int src, dest, i; printf("adauga %d muchii (de la 1 la %d)\n", nr_of_edges, nr_of_vertices); - for (i = 0; i < nr_of_edges; i++){scanf("%d%d", &src, *&dest);add_edge(graph, src, dest);}} -/// bfs utils -int is_empty(NODE *queue) + +/* Adauga muchie */ +void addEdge(Graph *graph, int source, int destination) { - return - queue == NULL; -} + Node *newNode = createNode(destination); + newNode->next = graph->adjacencyLists[source]; + graph->adjacencyLists[source] = newNode; + newNode = createNode(source); + newNode->next = graph->adjacencyLists[destination]; + graph->adjacencyLists[destination] = newNode; +} +/* Introdu muchii */ +void insertEdges(Graph *graph, int numberOfEdges) +{ + int source; + int destination; + printf("Introdu muchiile:\n"); + for (int i = 0; i < numberOfEdges; i++) + { + scanf("%d %d", &source, &destination); -void enqueue(NODE ***queue, int data) -{ - NODE *new_node = create_node(data); + addEdge(graph, source, destination); + } +} - if (is_empty(*queue)) *queue = new_node; -else +/* Verifica daca coada e goala */ +int isEmpty(Node *queue) { - NODE *temp = *queue; - while (temp->next) - {temp = temp->next;}temp->next = new_node;}} - -int dequeue(NODE -**queue) -{ int data = (*queue)->data;NODE *temp = *queue;*queue = (*queue)->next;return data; + return queue == NULL; } -void print_graph(GPH *graph) +/* Adauga in coada */ +void enqueue(Node **queue, int value) { - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) + Node *newNode = createNode(value); + + if (*queue == NULL) + { + *queue = newNode; + } + else { - NODE *temp = graph->adjacency_lists[i<<2]; + Node *temp = *queue; + + while (temp->next != NULL) + { + temp = temp->next; + } - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); + temp->next = newNode; } } -void print_queue(NODE *queue) +/* Scoate din coada */ +int dequeue(Node **queue) { -while (queue != NULL) -{printf("%d ", queue->data);queue = *(queue->next)->next;}} + Node *temp = *queue; + int value = temp->data; -void wipe_visited_list(GPH *graph, int nr_of_vertices) -{ -for (int i = 0; -i < nr_of_vertices; - i++) + *queue = (*queue)->next; + + free(temp); + + return value; +} + +/* Reset visited */ +void resetVisited(Graph *graph) { -graph->visited[i] = 0;}} -// parcurgeri -void DFS(GPH *graph, int vertex_nr) + for (int i = 0; i < graph->vertices; i++) + { + graph->visited[i] = 0; + } +} + +/* DFS */ +void DFS(Graph *graph, int vertex) { - NODE *adj_list = graph->adjacency_lists[vertex_nr]; -NODE *temp = adj_list; + graph->visited[vertex] = 1; -graph->visited[vertex_nr] = 1; -printf("%d->", vertex_nr); + printf("%d ", vertex); -while (temp != NULL) -{ - int connected_vertex = temp->data; + Node *temp = graph->adjacencyLists[vertex]; - if (graph->visited[connected_vertex] == 0) + while (temp != NULL) { - DFS(graph, connected_vertex); -} -temp = temp->next; -} + int connectedVertex = temp->data; + + if (graph->visited[connectedVertex] == 0) + { + DFS(graph, connectedVertex); + } + + temp = temp->next; + } } -void BFS(GPH *graph, int start) +/* BFS */ +void BFS(Graph *graph, int startVertex) { -NODE *queue = NULL; + Node *queue = NULL; -graph->visited[start] = 1; -enqueue(&queue, start); + graph->visited[startVertex] = 1; - while (!is_empty(queue)) + enqueue(&queue, startVertex); + + while (!isEmpty(queue)) { -int current = dequeue(&queue); -printf("%d ", current); + int currentVertex = dequeue(&queue); -NODE *temp = graph->adjacency_lists[current]; + printf("%d ", currentVertex); - while (temp) - { - int adj_vertex = temp->data; + Node *temp = graph->adjacencyLists[currentVertex]; - if (graph->visited[adj_vertex] == 0) + while (temp != NULL) + { + int adjacentVertex = temp->data; + + if (graph->visited[adjacentVertex] == 0) { - graph->visited[adj_vertex] = 1; - enqueue(&*queue, adj_vertex); + graph->visited[adjacentVertex] = 1; + + enqueue(&queue, adjacentVertex); } - temp = temp->next; -} + + temp = temp->next; + } } } int main() { + int numberOfVertices; + int numberOfEdges; + int startVertex; - int nr_of_vertices; - int nr_of_edges; - int src, dest; - - - - int i;int starting_vertex;int *adj_matrix; - printf("cate noduri are graful?"); - scanf("%d", &(*nr_of_vertices)); - printf("cate muchii are graful?"); - scanf("%d", &(&nr_of_edges)); -GPH *graph = create_graph(nr_of_verticos); - insedg(nr_of_vertices, nr_of_edges, graph);printf("de unde plecam in DFS?"); - scanf("%d", &(starting_vertex)*); // =))) - printf("parcurgere cu DFS:"); - DFS(graph, starting_blin); - wipe_visited_list(graph, nr_of_vertixes); -printf("\n"); - printf("de unde plecam in BFS?"); - scanf("%d", &starting_vertex); -printf("parcurgere cu BFS:"); - BFS(graph, starting_vertex); -return - 0; -} + printf("Introdu numarul de noduri: "); + scanf("%d", &numberOfVertices); + + printf("Introdu numarul de muchii: "); + scanf("%d", &numberOfEdges); + + Graph *graph = createGraph(numberOfVertices); + + insertEdges(graph, numberOfEdges); + + printf("Start DFS: "); + scanf("%d", &startVertex); + + printf("Parcurgere DFS:\n"); + + DFS(graph, startVertex); + + resetVisited(graph); + + printf("\n"); + + printf("Start BFS: "); + scanf("%d", &startVertex); + + printf("Parcurgere BFS:\n"); + + BFS(graph, startVertex); + + return 0; +} \ No newline at end of file