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; //1
  83. p = createN(x); //1
  84. p->next = head; //2
  85. head = p; //3
  86. }
  87.  
  88. void insMiddle(int n, int x){
  89. int i;
  90. Node *p,*q;
  91. p = head; //1
  92. for(i=1;i<n;i++){ //2
  93. p = p->next; //2
  94. }
  95. q = createN(x); //3
  96. q->next = p->next; //4
  97. p->next = q; //5
  98. }
  99.  
  100. void insTail(int x){
  101. Node *p;
  102. p = head; //1
  103. if(p==NULL){
  104. head = createN(x);
  105. return;
  106. }
  107. while(p->next != NULL){ //2
  108. p = p->next; //2
  109. }
  110. p->next = createN(x); //3
  111. }
  112.  
  113. void delHead(){
  114. Node *p;
  115. p = head; //1
  116. head = head->next; //2
  117. free(p); //3
  118. }
  119.  
  120. void delMiddle(int n){
  121. int i;
  122. Node *p,*q;
  123. p = head; //1
  124. for(i=1;i<n-1;i++){ //2
  125. p = p->next; //2
  126. }
  127. q = p->next; //3
  128. p->next = q->next; //4
  129. free(q); //5
  130. }
  131.  
  132. void delTail(){
  133. Node *p;
  134. p = head; //1
  135. while(p->next->next != NULL){ //2
  136. p = p->next; //2
  137. }
  138. free(p->next); //3
  139. p->next = NULL; //4
  140. }
  141.  
  142. void delX(int x){
  143. int i=1;
  144. Node *p;
  145. for(p=head;p!=NULL;p=p->next){
  146. if(x==p->val) break;
  147. i++;
  148. }
  149. if(p==NULL) return ;
  150. else if(i==1) delHead();
  151. else if(i==countL());
  152. else delMiddle(i);
  153. }
  154.  
  155. int main(void){
  156. int i,n,x;
  157. scanf("%d",&n);
  158. initL(n);
  159. scanf("%d",&x);
  160. delX(x);
  161. printL();
  162. freeL();
  163. return 0;
  164. }
Success #stdin #stdout 0s 5292KB
stdin
4
1
3
5
7
3
stdout
1 5 7