spacepaste

  1.  
  2. $ cat ./undo_history.c
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <inttypes.h>
  6. // A C program to demonstrate linked list based implementation of queue
  7. // A linked list (LL) node to store a queue entry
  8. struct QNode
  9. {
  10. int16_t type;
  11. struct QNode *next;
  12. };
  13. // The queue, front stores the front node of LL and rear stores ths
  14. // last node of LL
  15. struct Queue
  16. {
  17. struct QNode *front, *rear;
  18. };
  19. // A utility function to create a new linked list node.
  20. struct QNode* newNode(int16_t type)
  21. {
  22. struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
  23. temp->type = type;
  24. temp->next = NULL;
  25. return temp;
  26. }
  27. // A utility function to create an empty queue
  28. struct Queue *createQueue()
  29. {
  30. struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
  31. q->front = q->rear = NULL;
  32. return q;
  33. }
  34. void store_asm(struct Queue **qq, int16_t type)
  35. {
  36. if (*qq == NULL)
  37. *qq = createQueue();
  38. // Create a new LL node
  39. struct QNode *temp = newNode(type);
  40. // If queue is empty, then new node is front and rear both
  41. if ((*qq)->rear == NULL)
  42. {
  43. (*qq)->front = (*qq)->rear = temp;
  44. return;
  45. }
  46. // Add the new node at the end of queue and change rear
  47. (*qq)->rear->next = temp;
  48. (*qq)->rear = temp;
  49. }
  50. struct QNode * load_asm(struct Queue **qq)
  51. {
  52. if ((*qq) == NULL)
  53. return NULL;
  54. // If queue is empty, return NULL.
  55. if ((*qq)->rear == NULL)
  56. return NULL;
  57. // Store previous front and move front one node ahead
  58. struct QNode *temp = (*qq)->rear;
  59. (*qq)->rear = (*qq)->rear->next;
  60. // If front becomes NULL, then change rear also as NULL
  61. if ((*qq)->rear == NULL)
  62. (*qq)->front = NULL;
  63. return temp;
  64. }
  65. int main(void) {
  66. struct Queue * history;
  67. history = NULL;
  68. store_asm(&history, 1);
  69. store_asm(&history, 2);
  70. store_asm(&history, 3);
  71. puts("attempting to list node history");
  72. // obtain the first added instruction
  73. struct QNode * node = malloc(1); // this gets freed anyway
  74. int nodes = 0;
  75. while (node != NULL) {
  76. // drain the list until empty
  77. free(node);
  78. node = load_asm(&history);
  79. printf("node %d = %p\n", nodes, node);
  80. if (node == NULL) break;
  81. if (node) {
  82. printf("node->type = 0x%02x\n", node->type);
  83. printf("node->type = %d\n", node->type);
  84. }
  85. nodes++;
  86. }
  87. if (nodes == 0) puts("i have no history");
  88. else puts("i have no nodes left in history");
  89. // should result in 3, 2, 1
  90. return 0;
  91. }
  92. $ gcc undo_history.c -o undo_history
  93. $ ./undo_history
  94. attempting to list node history
  95. node 0 = 0x5583a820e2c0
  96. node->type = 0x03
  97. node->type = 3
  98. node 1 = (nil)
  99. i have no nodes left in history
  100.