fork download
  1. /******************************************************************************
  2.  
  3.   Online C Compiler.
  4.   Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. /******************************************************************************
  10.  
  11.   Online C Compiler.
  12.   Code, Compile, Run and Debug C program online.
  13. Write your code in this editor and press "Run" button to compile and execute it.
  14.  
  15. *******************************************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. typedef struct node {
  21. int data;
  22. struct node *next;
  23. struct node *prev;
  24. } node_t;
  25.  
  26. typedef struct {
  27. node_t *next;
  28. node_t * prev;
  29. int len;
  30. int size;
  31. } list_t;
  32.  
  33. #define MAX_Q 1000
  34. list_t *list_init(int size) {
  35. list_t *list;
  36.  
  37. if (size <= 0 || size > MAX_Q)
  38. return NULL;
  39.  
  40. list = malloc(sizeof(list_t));
  41. if (list == NULL)
  42. return NULL;
  43.  
  44. list->len = 0;
  45. list->size = size;
  46. list->next = NULL;
  47. list->prev = NULL;
  48.  
  49. return list;
  50. }
  51.  
  52. int list_add(list_t *list, int val) {
  53. node_t *new_node;
  54.  
  55. if (list->len + 1 > list->size)
  56. return -1;
  57.  
  58. new_node = malloc(sizeof(node_t));
  59. if (new_node == NULL)
  60. return -1;
  61.  
  62. list->len++;
  63. new_node->data = val;
  64. new_node->prev = NULL;
  65. new_node->next = list->next;
  66. list->next = new_node;
  67. if (list->prev != NULL)
  68. new_node->next->prev = new_node;
  69. else
  70. list->prev == new_node;
  71.  
  72. return 0;
  73. }
  74.  
  75. int list_pop(list_t *list, int location) {
  76. return -1;
  77. }
  78.  
  79. int list_mean(list_t *list) {
  80. return -1;
  81. }
  82.  
  83. void list_pring(list_t *list)
  84. {
  85. node_t *node;
  86.  
  87. if (list == NULL)
  88. return;
  89.  
  90. if (list->len == 0) {
  91. printf("List is empty");
  92. return;
  93. }
  94.  
  95. node = list->next;
  96. printf("list had %d items: ", list->len);
  97. while(node) {
  98. printf("%d ", node->data);
  99. node = node->next;
  100. }
  101. printf("\n");
  102.  
  103. node = list->prev;
  104. while(node) {
  105. printf("%d ", node->data);
  106. node = node->prev;
  107. }
  108. printf("\n");
  109. }
  110.  
  111. int main()
  112. {
  113. int i, ret;
  114. list_t *my_list;
  115.  
  116. my_list = list_init(10);
  117. if (!my_list) {
  118. printf("failed to allocate a list\n");
  119. return -1;
  120. }
  121.  
  122. for (i=1; i < 15; i++) {
  123. ret = list_add(my_list, i);
  124. if (ret)
  125. printf("failed to add %d\n", i);
  126. else
  127. list_pring(my_list);
  128. }
  129.  
  130. return 0;
  131. }
  132.  
Success #stdin #stdout 0.01s 5276KB
stdin
45
stdout
list had 1 items: 1 

list had 2 items: 2 1 

list had 3 items: 3 2 1 

list had 4 items: 4 3 2 1 

list had 5 items: 5 4 3 2 1 

list had 6 items: 6 5 4 3 2 1 

list had 7 items: 7 6 5 4 3 2 1 

list had 8 items: 8 7 6 5 4 3 2 1 

list had 9 items: 9 8 7 6 5 4 3 2 1 

list had 10 items: 10 9 8 7 6 5 4 3 2 1 

failed to add 11
failed to add 12
failed to add 13
failed to add 14