fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int stringLength(char *s) {
  5. int i=0;
  6. while ( (i < 1000) && (s[i]!='\0') ) i++;
  7. if (s[i]!='\0') {printf("ERROR: Missing string end character.\n\n"); exit(1);}
  8. return (i);
  9. }
  10.  
  11. char* stringCopy(char *s) {
  12. int i=0;
  13. char *t = malloc(stringLength(s)+2+1);
  14. do {t[i]=s[i];} while (s[i++]!='\0');
  15. return(t);
  16. }
  17.  
  18. typedef struct pnode {
  19. struct pnode *back;
  20. struct pnode *next;
  21. char piece;
  22. int dir;
  23. int cost;
  24. char *board;
  25. } position;
  26.  
  27. position * createPosition(position *back, position *next, char piece, int dir, int cost, char board[12]){
  28. position *p = malloc(sizeof(position));
  29. p->back = back;
  30. p->next = next;
  31. p->piece = piece;
  32. p->dir = dir;
  33. p->cost = cost;
  34. p->board = stringCopy(board);
  35. return p;
  36. }
  37.  
  38. typedef struct queue {
  39. int size;
  40. int front;
  41. int rear;
  42. int n;
  43. position *array[];
  44. } Queue;
  45.  
  46. Queue * createQueue(int n){
  47. Queue *q = malloc(sizeof(Queue));
  48. q->size = 0;
  49. q->front = 0;
  50. q->rear = 0;
  51. q->n = n;
  52. return q;
  53. }
  54.  
  55. void enqueue(Queue *q, position *p){
  56. if (q->size >= q->n){
  57. printf("Queue overflow\n");
  58. exit(1);
  59. } else {
  60. q->array[q->rear] = p;
  61. q->rear = q->rear + 1;
  62. if (q->rear == q->n){
  63. q->rear = 0;
  64. }
  65. q->size = q->size + 1;
  66. }
  67. }
  68.  
  69. position *dequeue(Queue *q){
  70. if (q->size == 0){
  71. printf("Queue underflow\n");
  72. exit(1);
  73. } else {
  74. position *temp = q->array[q->front];
  75. q->front = q->front + 1;
  76. if (q->front == q->n){
  77. q->front = 0;
  78. }
  79. q->size = q->size - 1;
  80. return temp;
  81. }
  82. }
  83.  
  84. int main(void){
  85. position *p = createPosition(NULL, NULL, 'x', 1, 4, "xyz123456002");
  86. Queue *q = createQueue(10);
  87. enqueue(q, p);
  88. position *r = dequeue(q);
  89. printf("%c", r->board[2]);
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
Success #stdin #stdout 0s 2300KB
stdin
Standard input is empty
stdout
z