spacepaste

  1.  
  2. $ cat new.c
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define pp(x) printf("%s = %p\n", #x, x);
  8. #define pi(x) printf("%s = %d\n", #x, x);
  9. #define pc(x) printf("%s = %c\n", #x, x);
  10. // A C program to demonstrate linked list based implementation of queue
  11. // A linked list (LL) node to store a queue entry
  12. int index_start = -1;
  13. int ref_start = 0;
  14. int global_indent = 0;
  15. #define node_init(t, i, r, nc, n) t->index = i; t->ref = r; t->next_count = nc; t->next = n; t->level = global_indent;
  16. #define STORE_NORMAL 1
  17. #define STORE_EMPTY 2
  18. #define NODE_TYPE_NORMAL 1
  19. #define NODE_TYPE_BRANCH_START 2
  20. #define NODE_TYPE_BRANCH_END 3
  21. #define NODE_TYPE_BRANCH_CHOICE_START 4
  22. #define NODE_TYPE_BRANCH_CHOICE_END 5
  23. struct QNode
  24. {
  25. int level;
  26. int index;
  27. int ref;
  28. int next_count;
  29. int type;
  30. struct QNode **next;
  31. };
  32. // The queue, front stores the front node of LL and rear stores ths
  33. // last node of LL
  34. struct Queue
  35. {
  36. struct QNode *front, *rear;
  37. };
  38. // A utility function to create a new linked list node.
  39. struct QNode* newNode(int type)
  40. {
  41. struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
  42. if (type == STORE_NORMAL) {
  43. node_init(temp, index_start++, ref_start++, 0, NULL);
  44. } else if (type == STORE_EMPTY) {
  45. node_init(temp, 0, 0, 0, NULL);
  46. }
  47. return temp;
  48. }
  49. // A utility function to create an empty queue
  50. struct Queue *createQueue()
  51. {
  52. struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
  53. q->front = q->rear = NULL;
  54. return q;
  55. }
  56. void store_asm(struct Queue *q, int type)
  57. {
  58. // Create a new LL node
  59. int t = 0;
  60. if (type == NODE_TYPE_NORMAL) t = STORE_NORMAL;
  61. else if (
  62. type == NODE_TYPE_BRANCH_START
  63. ||
  64. type == NODE_TYPE_BRANCH_END
  65. ||
  66. type == NODE_TYPE_BRANCH_CHOICE_START
  67. ||
  68. type == NODE_TYPE_BRANCH_CHOICE_END
  69. ) t = STORE_EMPTY;
  70. struct QNode *temp = newNode(t);
  71. temp->type = type;
  72. // If queue is empty, then new node is front and rear both
  73. if (q->rear == NULL)
  74. {
  75. q->front = q->rear = temp;
  76. if (!q->rear->next) {
  77. q->rear->next_count = 1;
  78. q->rear->next = malloc(sizeof(struct QNode)*q->rear->next_count);
  79. } else {
  80. q->rear->next_count++;
  81. q->rear->next = realloc(q->rear->next, sizeof(struct QNode)*q->rear->next_count);
  82. }
  83. return;
  84. }
  85. // Add the new node at the end of queue and change rear
  86. q->rear->next[q->rear->next_count-1] = temp;
  87. q->rear = temp;
  88. if (!q->rear->next) {
  89. q->rear->next_count = 1;
  90. q->rear->next = malloc(sizeof(struct QNode)*q->rear->next_count);
  91. } else {
  92. q->rear->next_count++;
  93. q->rear->next = realloc(q->rear->next, sizeof(struct QNode)*q->rear->next_count);
  94. }
  95. }
  96. void add(struct Queue **q) {
  97. if (!(*q)) (*q) = createQueue();
  98. store_asm((*q), NODE_TYPE_NORMAL);
  99. }
  100. #define add_indent(indent) { int i = 0; for (; i < indent; i++) printf(" "); }
  101. struct Queue * list = NULL;
  102. void print(struct QNode * p, int indent) {
  103. int indentation = 0;
  104. while(p) {
  105. indentation = p->level;
  106. add_indent(indentation);
  107. if (p->type == NODE_TYPE_BRANCH_START) {
  108. add_indent(indentation);
  109. printf("branch start\n");
  110. }
  111. else if (p->type == NODE_TYPE_BRANCH_END) {
  112. add_indent(indentation);
  113. printf("branch end\n");
  114. }
  115. else if (p->type == NODE_TYPE_BRANCH_CHOICE_START) {
  116. add_indent(indentation);
  117. printf("branch choise start\n");
  118. }
  119. else if (p->type == NODE_TYPE_BRANCH_CHOICE_END) {
  120. add_indent(indentation);
  121. printf("branch choise end\n");
  122. }
  123. else if (p->type == NODE_TYPE_NORMAL) {
  124. add_indent(indentation);
  125. printf("%d -> %d\n", p->index, p->ref);
  126. if (p->next_count > 1) print(p->next[1], indent+1);
  127. }
  128. if (p->next) p = p->next[0];
  129. else p = NULL;
  130. }
  131. add_indent(indentation);
  132. puts("NULL");
  133. }
  134. void add_branch(struct Queue **q) {
  135. if (!(*q)) (*q) = createQueue();
  136. store_asm((*q), NODE_TYPE_BRANCH_START);
  137. global_indent++;
  138. }
  139. void end_branch(struct Queue **q) {
  140. global_indent--;
  141. if (!(*q)) (*q) = createQueue();
  142. store_asm((*q), NODE_TYPE_BRANCH_END);
  143. }
  144. void add_branch_choice(struct Queue **q) {
  145. if (!(*q)) (*q) = createQueue();
  146. store_asm((*q), NODE_TYPE_BRANCH_CHOICE_START);
  147. global_indent++;
  148. }
  149. void end_branch_choice(struct Queue **q) {
  150. global_indent--;
  151. if (!(*q)) (*q) = createQueue();
  152. store_asm((*q), NODE_TYPE_BRANCH_CHOICE_END);
  153. }
  154. int main(void) {
  155. index_start = 0;
  156. ref_start = 1;
  157. add(&list);
  158. add_branch(&list);
  159. add_branch_choice(&list);
  160. add(&list);
  161. add(&list);
  162. end_branch_choice(&list);
  163. add_branch_choice(&list);
  164. add(&list);
  165. add(&list);
  166. end_branch_choice(&list);
  167. end_branch(&list);
  168. add(&list);
  169. print(list->front, 0);
  170. return 0;
  171. }
  172. $ gcc new.c -o new
  173. $ ./new
  174. 0 -> 1
  175. branch start
  176. branch choise start
  177. 1 -> 2
  178. 2 -> 3
  179. branch choise end
  180. branch choise start
  181. 3 -> 4
  182. 4 -> 5
  183. branch choise end
  184. branch end
  185. 5 -> 6
  186. NULL
  187.