Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////MAIN_CORR.C///////////////////
- #include <stdio.h>
- #include "fgraph.h"
- void print_fgraph(struct fgraph *G);
- int main(int argc, char *argv[]) {
- struct fgraph *G;
- if(argc != 2) {
- fprintf(stderr,"Usage: fgraph <file>\n");
- return 1;
- }
- if((G = read_fgraph(argv[1])) == NULL) {
- fprintf(stderr,"Error reading %s\n",argv[1]);
- return 1;
- }
- print_fgraph(G);
- system("PAUSE");
- return 0;
- }
- /////////////////////////////////////////////////////
- ///////////////////////FGRAPH:C///////////////////////////////
- #include <stdio.h>
- #include <stdlib.h>
- #include "fgraph.h"
- #define N 1000
- struct fgraph *read_fgraph(char *filename) {
- FILE *in = fopen(filename,"r");
- struct fgraph *G = NULL;
- unsigned int i, n, m;
- if(in != NULL && fscanf(in,"%u %u\n",&n,&m) != EOF) {
- unsigned int id, id1, id2;
- char name[N];
- for(i = 0; i < n && fscanf(in,"%u",&id) != EOF && fgets(name,N,in) != NULL; i++) {
- name[strlen(name)-1]='\0';
- insert_individual(&G,strcpy(calloc(strlen(name)+1,sizeof(char)),name),id);
- }
- for(i = 0; i < m && fscanf(in,"%u %u",&id1,&id2) != EOF; i++)
- insert_friendship(G,id1,id2);
- fclose(in);
- }
- return G;
- }
- static struct fgraph * individual_alloc(char *name, unsigned int id)
- {
- struct fgraph *tmp = (struct fgraph *)calloc(1, sizeof(struct fgraph));
- if (tmp != NULL) {
- tmp->friends = NULL;
- tmp->id = id;
- tmp->name = name;
- tmp->next = NULL;
- tmp->prev = NULL;
- }
- return tmp;
- }
- static struct list * friend_alloc(unsigned int id)
- {
- struct list *tmp = (struct list *)calloc(1, sizeof(struct list));
- if (tmp != NULL) {
- tmp->id = id;
- tmp->next = NULL;
- tmp->prev = NULL;
- }
- return tmp;
- }
- static int in_inlist(struct fgraph **G, unsigned int id)
- {
- if ((G == NULL) || (*G == NULL)) {
- return 0;
- }
- else {
- struct fgraph *tmp = *G;
- while (tmp != NULL && tmp->id != id)
- tmp = tmp->next;
- return tmp != NULL;
- }
- }
- static int in_inlist_friend(struct fgraph *G, unsigned int id)
- {
- struct list *tmp = (*G).friends;
- while (tmp != NULL && tmp->id != id)
- tmp = tmp->next;
- return tmp != NULL;
- }
- static struct list * body_search(struct fgraph *G, unsigned int id) {
- while (id != G->id && G != NULL)
- G = G->next;
- return G;
- }
- int insert_individual(struct fgraph **G, char *name, unsigned int id) {
- struct fgraph *tmp = individual_alloc(name, id);
- if ((G == NULL) || (*G == NULL))
- {
- *G = tmp;
- return *G == NULL;
- }
- else
- {
- if (!in_inlist(&(*G), id))
- {
- if (tmp != NULL) {
- tmp->next = *G;
- (*G)->prev = tmp;
- *G = tmp;
- }
- return tmp == NULL;
- }
- else
- return 0;
- }
- }
- int insert_friendship(struct fgraph *G, unsigned int id1, unsigned int id2) {
- if (in_inlist(&G, id1) && in_inlist(&G, id2))
- {
- if (!in_inlist_friend(G, id1) || !in_inlist_friend(G, id2))
- {
- struct fgraph *p1 = body_search(G, id1);
- struct fgraph *p2 = body_search(G, id2);
- struct list *tmp1 = friend_alloc(id1);
- struct list *tmp2 = friend_alloc(id2);
- if (p1->friends == NULL)
- p1->friends = tmp2;
- else
- {
- //p to p per scorrere la lista
- struct list *ciclo1 = (*p1).friends;
- while (ciclo1->next != NULL)
- ciclo1 = ciclo1->next;
- ciclo1->next = tmp2;
- (ciclo1->next)->prev = ciclo1;
- }
- if (p2->friends == NULL)
- p2->friends = tmp1;
- else
- {
- //p to p per scorrere la lista
- struct list *ciclo2 = (*p2).friends;
- while (ciclo2->next != NULL)
- ciclo2 = ciclo2->next;
- ciclo2->next = tmp1;
- (ciclo2->next)->prev = ciclo2;
- }
- return 0;
- }
- else
- return 0;
- }
- return 1;
- }
- //////////////////END//////////////////////
- ////////////////////////FGRAPH:H/////////////////////
- #ifndef FGRAPH_H
- #define FGRAPH_H
- struct list {
- unsigned int id;
- struct list *next;
- struct list *prev;
- };
- struct fgraph {
- char *name;
- unsigned int id;
- struct list *friends;
- struct fgraph *next;
- struct fgraph *prev;
- };
- struct fgraph *read_fgraph(char *filename);
- int insert_individual(struct fgraph **G, char *name, unsigned int id);
- int insert_friendship(struct fgraph *G, unsigned int id1, unsigned int id2);
- #endif
- ////////////////////////END////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement