fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5. int val;
  6. struct node *next;
  7. } Node;
  8.  
  9. Node *head = NULL;
  10.  
  11. Node* createN(int x){
  12. Node *newnode;
  13. newnode = (Node *)malloc(sizeof(Node));
  14. newnode->val = x;
  15. newnode->next = NULL;
  16. return newnode;
  17. }
  18.  
  19. void initL(int n){
  20. int x, i;
  21. Node *p;
  22. scanf("%d", &x);
  23. head = createN(x);
  24. p = head;
  25. for(i = 1; i < n; i++){
  26. scanf("%d", &x);
  27. p->next = createN(x);
  28. p = p->next;
  29. }
  30. }
  31.  
  32. void freeL(){
  33. Node *p;
  34. while(head != NULL){
  35. p = head->next;
  36. free(head);
  37. head = p;
  38. }
  39. }
  40.  
  41. void printN(Node *a){
  42. if(a == NULL) printf("NULL\n");
  43. else printf("%d\n", a->val);
  44. }
  45.  
  46. void printL(){
  47. Node *p = head;
  48. while(p != NULL){
  49. printf("%d ", p->val);
  50. p = p->next;
  51. }
  52. printf("\n");
  53. }
  54.  
  55. Node* getN(int n){
  56. int i;
  57. Node *p;
  58. p = head;
  59. for(i = 1; i < n; i++) p = p->next;
  60. return p;
  61. }
  62.  
  63. int countL(){
  64. int ret = 0;
  65. Node *p = head;
  66. while(p != NULL){
  67. p = p->next;
  68. ret++;
  69. }
  70. return ret;
  71. }
  72.  
  73. Node* searchX(int x){
  74. Node *p;
  75. for(p = head; p != NULL; p = p->next){
  76. if(p->val == x) break;
  77. }
  78. return p;
  79. }
  80.  
  81. void insHead(int x){
  82. Node *p;
  83. p = createN(x);
  84. p->next = head;
  85. head = p;
  86. }
  87.  
  88. void insMiddle(int n, int x){
  89. int i;
  90. Node *p, *q;
  91. p = head;
  92. for(i = 1; i < n; i++){
  93. p = p->next;
  94. }
  95. q = createN(x);
  96. q->next = p->next;
  97. p->next = q;
  98. }
  99.  
  100. void insTail(int x){
  101. Node *p;
  102. p = head;
  103. if(p == NULL){
  104. head = createN(x);
  105. return;
  106. }
  107. while(p->next != NULL){
  108. p = p->next;
  109. }
  110. p->next = createN(x);
  111. }
  112.  
  113. void delHead(){
  114. Node *p;
  115. if (head == NULL) return; // 安全対策
  116. p = head;
  117. head = head->next;
  118. free(p);
  119. }
  120.  
  121. void delMiddle(int n){
  122. int i;
  123. Node *p, *q;
  124. p = head;
  125. for(i = 1; i < n - 1; i++){
  126. p = p->next;
  127. }
  128. q = p->next;
  129. p->next = q->next;
  130. free(q);
  131. }
  132.  
  133. void delTail(){
  134. Node *p;
  135. p = head;
  136. if (p == NULL || p->next == NULL) {
  137. freeL();
  138. return;
  139. }
  140. while(p->next->next != NULL){
  141. p = p->next;
  142. }
  143. free(p->next);
  144. p->next = NULL;
  145. }
  146.  
  147. void push(int x){
  148. insHead(x);
  149. }
  150.  
  151. int pop(){
  152. if (head == NULL) return -1;
  153.  
  154. int val = head->val;
  155. delHead();
  156. return val;
  157. }
  158.  
  159. void enqueue(int x){
  160. insTail(x);
  161. }
  162.  
  163. int dequeue(){
  164. if (head == NULL) return -1;
  165.  
  166. int val = head->val;
  167. delHead();
  168. return val;
  169. }
  170.  
  171. int main(void){
  172. int s1, s2, s3, q1, q2, q3;
  173.  
  174. // スタックのテスト
  175. push(1);
  176. push(2);
  177. push(3);
  178. s1 = pop();
  179. s2 = pop();
  180. s3 = pop();
  181. printf("%d %d %d\n", s1, s2, s3);
  182.  
  183.  
  184. enqueue(1);
  185. enqueue(2);
  186. enqueue(3);
  187. q1 = dequeue();
  188. q2 = dequeue();
  189. q3 = dequeue();
  190. printf("%d %d %d\n", q1, q2, q3);
  191.  
  192. freeL();
  193. return 0;
  194. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
3 2 1
1 2 3