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 printL(){
  42. Node *p = head;
  43. while(p != NULL){
  44. printf("%d ",p->val);
  45. p = p->next;
  46. }
  47. printf("\n");
  48. }
  49.  
  50. void insMiddle(int n, int x){
  51. int i;
  52. Node *p,*q;
  53. p=head;
  54. for(i=1;i<n;i++){
  55. p=p->next;
  56. }
  57. q=createN(x);
  58. q->next=p->next;
  59. p->next=q;
  60. }
  61.  
  62. void delMiddle(int n){
  63. int i;
  64. Node *p,*q;
  65. p=head;
  66. for(i=1;i<n-1;i++){
  67. p=p->next;
  68. }
  69. q=p->next;
  70. p->next=q->next;
  71. free(q);
  72. }
  73.  
  74.  
  75. int main(void){
  76. int i,n,z;
  77. scanf("%d",&n);
  78. initL(n);
  79. scanf("%d %d %d",&z);
  80. //insMiddle(x,y);
  81. //printL();
  82. delMiddle(z);
  83. printL();
  84. freeL();
  85. return 0;
  86. }
Success #stdin #stdout 0s 5280KB
stdin
3
2
5
7
2
stdout
2 7