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 delX(int x){
  145. //ここを書く
  146. Node *p, *prep;
  147. p = head;
  148. prep = NULL;
  149.  
  150. while(p != NULL)
  151. {
  152. if(p->val == x)
  153. {
  154. if(prep == NULL)
  155. {
  156. head = p->next;
  157. }
  158.  
  159. else
  160. {
  161. prep->next = p->next;
  162. }
  163.  
  164. free(p);
  165. return;
  166. }
  167.  
  168. prep = p;
  169. p = p->next;
  170. }
  171.  
  172. }
  173.  
  174. int main(void){
  175. int i,n,x;
  176. scanf("%d",&n);
  177. initL(n);
  178. scanf("%d",&x);
  179. delX(x);
  180. printL();
  181. freeL();
  182. return 0;
  183. }
  184.  
Success #stdin #stdout 0.01s 5280KB
stdin
4
1 3 5 7
3
stdout
1 5 7