Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. typedef struct linkedlist { // linked list of ints (for use in Node)
  2. int index;
  3. struct linkedlist *next;
  4. } List;
  5.  
  6. typedef struct { // a Node of a Graph
  7. char *name;
  8. List *outlist; // adjacency list
  9. int outdegree; // length of outlist
  10. //double pagerank_score; //not needed for this exercise
  11. } Node;
  12.  
  13.  
  14. typedef struct {
  15. int MaxSize;
  16. Node *table;
  17. } Graph;
  18.  
  19. int initialize_graph (Graph *mygraph, int MaxSize)
  20. {
  21. mygraph->table = malloc(sizeof(Node) * MaxSize);
  22. mygraph->MaxSize = MaxSize;
  23. return 0;
  24. }
  25.  
  26. int insert_graph_node (Graph *mygraph, int n, char *name)
  27. {
  28. strcpy(mygraph->table[n].name ,name);
  29. mygraph->table[n].outdegree = 0;
  30. mygraph->table[n].outlist = NULL;
  31.  
  32. return 0;
  33.  
  34. }
  35.  
  36. int insert_graph_link (Graph *mygraph, int source, int target)
  37. {
  38. List* myList = malloc(sizeof(List));
  39. myList->index = target;
  40. myList->next = NULL;
  41.  
  42. List* anotherList = mygraph->table[source].outlist;
  43.  
  44. if(mygraph->table[source].outdegree == 0)
  45. mygraph->table[source].outlist=myList;
  46. else
  47. {
  48. while(anotherList->next != NULL)
  49. {
  50. anotherList = anotherList->next;
  51. }
  52.  
  53. anotherList->next = myList;
  54. }
  55.  
  56.  
  57. mygraph->table[source].outdegree++;
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement