Advertisement
NB52053

NADUI_LAB8

Dec 10th, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. //imp.cpp
  2. #include "Header.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. void dfs(node* nd)
  7. {
  8.     if(nd != NULL)
  9.     {
  10.         printf("%c ",nd -> name);
  11.         node *temp = nd -> children;
  12.  
  13.         while(temp != NULL){
  14.             dfs(temp);
  15.             temp = temp -> sibling;
  16.         }
  17.     }
  18. }
  19.  
  20. //Header.h
  21. #ifndef Header_h_
  22. #define Header_h_
  23. struct node{
  24.     char name;
  25.     node *children;
  26.     node *sibling;
  27. };
  28.  
  29. void dfs(node *nd);
  30. #endif
  31.  
  32. //source.cpp
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "Header.h"
  36.  
  37. node *a,*b,*c,*d,*e,*f;
  38.  
  39. node *create_node(char n)
  40. {
  41.     node *temp = (node*)malloc(sizeof(node));
  42.     temp -> name = n;
  43.     temp -> children = NULL;
  44.     temp -> sibling = NULL;
  45.     return temp;
  46. }
  47.  
  48. void createGraph()
  49. {
  50.     a = create_node('A');
  51.     b = create_node('B');
  52.     c = create_node('C');
  53.     d = create_node('D');
  54.     e = create_node('E');
  55.     f = create_node('F');
  56.  
  57.     a -> children = c;
  58.     c -> sibling = b;
  59.     b -> children = d;
  60.     d -> sibling = e;
  61.     c -> children = f;
  62.  
  63. }
  64.  
  65. int main()
  66. {
  67.     createGraph();
  68.     dfs(a);
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement