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. if (head == NULL) {
  103. head = createN(x);
  104. return;
  105. }
  106. p = head;
  107. while (p->next != NULL) {
  108. p = p->next;
  109. }
  110. p->next = createN(x);
  111. }
  112.  
  113. void delHead() {
  114. if (head == NULL) return;
  115. Node *p;
  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. if (head == NULL) return;
  135. if (head->next == NULL) {
  136. free(head);
  137. head = NULL;
  138. return;
  139. }
  140. Node *p;
  141. p = head;
  142. while (p->next->next != NULL) {
  143. p = p->next;
  144. }
  145. free(p->next);
  146. p->next = NULL;
  147. }
  148.  
  149. // --- 以下、追加・修正した関数 ---
  150.  
  151. void push(int x) {
  152. // スタックのプッシュは先頭への挿入
  153. insHead(x);
  154. }
  155.  
  156. int pop() {
  157. // スタックのポップは先頭から値を取り出し、ノードを削除
  158. if (head == NULL) return -1; // エラー処理(空の場合)
  159.  
  160. int val = head->val; // 1. 値を保持
  161. delHead(); // 2. 先頭を削除
  162. return val; // 3. 値を返す
  163. }
  164.  
  165. void enqueue(int x) {
  166. // キューのエンキューは末尾への挿入
  167. insTail(x);
  168. }
  169.  
  170. int dequeue() {
  171. // キューのデキューは先頭から値を取り出し、ノードを削除
  172. if (head == NULL) return -1; // エラー処理(空の場合)
  173.  
  174. int val = head->val; // 1. 値を保持
  175. delHead(); // 2. 先頭を削除
  176. return val; // 3. 値を返す
  177. }
  178.  
  179. int main(void) {
  180. int s1, s2, s3, q1, q2, q3;
  181.  
  182. // スタックの動作確認 (LIFO: 3, 2, 1 の順に出るはず)
  183. push(1);
  184. push(2);
  185. push(3);
  186. s1 = pop();
  187. s2 = pop();
  188. s3 = pop();
  189. printf("%d %d %d\n", s1, s2, s3);
  190.  
  191. // キューの動作確認 (FIFO: 1, 2, 3 の順に出るはず)
  192. enqueue(1);
  193. enqueue(2);
  194. enqueue(3);
  195. q1 = dequeue();
  196. q2 = dequeue();
  197. q3 = dequeue();
  198. printf("%d %d %d\n", q1, q2, q3);
  199.  
  200. freeL();
  201. return 0;
  202. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
3 2 1
1 2 3