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