Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct nod {
  5. int id;
  6. char tag;
  7. struct nod *next, *prev;
  8. };
  9.  
  10.  
  11. struct nod* insert(struct nod X, struct nod* T){
  12. if(T==NULL){
  13. T = malloc(sizeof(struct nod));
  14. if(T==NULL){
  15. printf("\n Eroare !");
  16. exit(1);
  17. }
  18. else
  19. {
  20. T->tag = X.tag;
  21. T->id = X.id;
  22. T->prev = T->next = NULL;
  23. }
  24. }
  25. else
  26. if(X.id < T->id)
  27. T->prev = insert(X,T->prev);
  28. else
  29. if(X.id >T->id)
  30. T->next=insert(X,T->next);
  31. return T;
  32. }
  33.  
  34.  
  35. void inorder(struct nod* head)
  36. {
  37. if(head !=NULL)
  38. {
  39. inorder(head->prev);
  40. printf("%c",head->tag);
  41. inorder(head->next);
  42. }
  43. }
  44. void preorder (struct nod* head )
  45. {
  46. if(head !=NULL)
  47. {
  48. printf("%c",head->tag);
  49. preorder(head->prev);
  50. preorder(head->next);
  51. }
  52. }
  53. void postorder (struct nod* head)
  54. {
  55. if(head !=NULL)
  56. {
  57. postorder(head->prev);
  58. postorder(head->next);
  59. printf("%c",head->tag);
  60. }
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64. char nume[10];
  65. printf("introduceti numele");
  66. printf("\n");
  67. scanf("%s",&nume);
  68. //nod data[]={{4,'o'},{2,'r'},{6,'e'},{1,'a'},{3,'b'},{5,'r'},{7,'\n'}};
  69. struct nod data[]={{4,110},{2,105},{6,97},{1,98},{3,97},{5,99}};
  70. struct nod* head=NULL;
  71. struct nod* temp;
  72. struct nod* tree=NULL;
  73. int i;
  74. for(i=0;i<6;i++){
  75. if(!head) {
  76. temp = malloc(sizeof(struct nod));
  77. head = temp;
  78. }
  79. else{
  80. temp = head;
  81. while(temp->next) temp = temp->next;
  82. temp->next = malloc(sizeof(struct nod));
  83. temp = temp->next;
  84. }
  85. *temp = data[i];
  86. }
  87.  
  88. // inchide lista
  89. temp->next = head;
  90.  
  91. // sare la primul nod
  92. temp = temp->next; // sau temp=head;
  93. do{
  94. tree=insert(*temp,tree);
  95. temp = temp->next;
  96. }while(temp != head);
  97.  
  98. inorder(tree);
  99. printf("\n");
  100. preorder(tree);
  101. printf("\n");
  102. postorder(tree);
  103. printf("\n");
  104. system("PAUSE");
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement