Posted By

sanovinic on 01/06/11


Tagged


Versions (?)

Datoteka Zaglavlja - „prvo dijete, sljedeći brat“


 / Published in: C++
 

  1. #ifndef OSTABLO_H_
  2. #define OSTABLO_H_
  3.  
  4. typedef int node;
  5.  
  6. struct elem {
  7. char label[MAX_D];
  8. node firstchild, nextsibling;
  9. };
  10. struct tr {
  11. struct elem elements[1000];
  12. node first;
  13. };
  14. typedef struct tr tree;
  15.  
  16. tree *InitT(char *x,tree *T){
  17. T = new tree[M_V_P];
  18. T->first=0;
  19. T->elements[0].firstchild=LAMBD;
  20. T->elements[0].nextsibling=LAMBD;
  21. strcpy(T->elements[0].label,x);
  22. return T;
  23. }
  24. node RootT(tree *T){
  25. return T->first;
  26. }
  27. node NextSiblingT(node n,tree *T){
  28. return T->elements[n].nextsibling;
  29. }
  30. node FirstChildT(node n,tree *T){
  31. return T->elements[n].firstchild;
  32. }
  33. bool CreateT(char *x,node n,tree *T){
  34. static int uk_broj=1;
  35. if(uk_broj>=M_V_P) return 1;
  36. if(T->elements[n].firstchild == LAMBD){
  37. T->elements[n].firstchild = uk_broj;
  38. strcpy(T->elements[uk_broj].label,x);
  39. T->elements[uk_broj].firstchild = LAMBD;
  40. T->elements[uk_broj].nextsibling = LAMBD;
  41. }
  42. else{
  43. node cvor=FirstChildT(n,T);
  44. while(NextSiblingT(cvor,T)!=LAMBD) cvor = NextSiblingT(cvor,T);
  45. T->elements[cvor].nextsibling = uk_broj;
  46. strcpy(T->elements[uk_broj].label,x);
  47. T->elements[uk_broj].firstchild = LAMBD;
  48. T->elements[uk_broj].nextsibling = LAMBD;
  49. }
  50. uk_broj++;
  51. return 0;
  52. }
  53. char *LabelT(node n,tree *T){
  54. return T->elements[n].label;
  55. }
  56. void ChangeLabelT(char *x,node n,tree *T){
  57. strcpy(T->elements[n].label,x);
  58. }
  59. void DeleteT(node n,tree *T){
  60. node cvor = FirstChildT(n,T);
  61. T->elements[n].firstchild=LAMBD;
  62. while(cvor!=LAMBD){
  63. DeleteT(cvor,T);
  64. cvor=NextSiblingT(cvor,T);
  65. }
  66. if(T->first==n){
  67. return;
  68. }
  69. else
  70. for (int i=0;i<MAX_D;i++){
  71. if(T->elements[i].firstchild==n) {
  72. node a = T->elements[i].firstchild;
  73. T->elements[i].firstchild = T->elements[a].nextsibling;
  74. }
  75. else if(T->elements[i].nextsibling==n) {
  76. node a = T->elements[i].nextsibling;
  77. if(a==LAMBD) T->elements[i].nextsibling = LAMBD;
  78. T->elements[i].nextsibling = T->elements[a].nextsibling;
  79. }
  80. }
  81. }
  82.  
  83.  
  84. #endif

Report this snippet  

You need to login to post a comment.