fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *link;
  8. }*first;
  9.  
  10. void create(int A[],int n)
  11. {
  12. struct node *head,*t;
  13. int i;
  14.  
  15.  
  16. first=(struct node *)malloc(sizeof(struct node ));
  17. first->data=A[0];
  18. first->link=first;
  19. head=first;
  20.  
  21. for(i=1;i<n;i++)
  22. {
  23. t=(struct node*)malloc(sizeof(struct node));
  24. t->data=A[i];
  25. t->link=head->link;
  26. head->link=t;
  27. head=t;
  28.  
  29.  
  30. }
  31.  
  32.  
  33. }
  34. void display(struct node *p)
  35. {
  36. do
  37. {
  38. printf("%d\t\n",p->data);
  39. p=p->link;
  40.  
  41. }while(p!=first);
  42.  
  43.  
  44. }
  45. void insert(struct node *h,int index,int x,int length)
  46. {
  47. int i;struct node *t;
  48. if(index<0||index>length)
  49. {
  50. return ;
  51.  
  52. }
  53. if (index==0)
  54. {
  55. t=(struct node *)malloc(sizeof(struct node));
  56. t->data=x;
  57. if(first==NULL)
  58. {
  59. first=t;
  60. first->link=first;
  61. }
  62. else
  63. {
  64. while(h->link!=first)
  65. {
  66. h=h->link;
  67. }
  68. h->link=t;
  69. t->link=first;
  70. first=t;
  71.  
  72. }
  73.  
  74. }
  75. else
  76. {
  77. for(i=0;i<index-2;i++)
  78. {
  79. h=h->link;
  80.  
  81. }
  82. t=(struct node *)malloc(sizeof(struct node ));
  83. t->data=x;
  84. t->link=h->link;
  85. h->link=t;
  86.  
  87. }
  88. }
  89.  
  90. int del(struct node *p,int index)
  91. {
  92. struct node *q;
  93. int i,x;
  94. if(index==1)
  95. {
  96.  
  97.  
  98. while(p->link!=first)
  99. {
  100.  
  101.  
  102. p=p->link;
  103. }
  104. x=first->data;
  105. if(first==p)
  106. {
  107. free(first);
  108. first==NULL;
  109.  
  110. }
  111. else
  112. {
  113. p->link=first->link;
  114. free(first);
  115. first=first->link;
  116.  
  117. }
  118. }
  119. else
  120. {
  121. for(i=0;i<index-2;i++)
  122. {
  123. p=p->link;
  124. }
  125. q=p->link;
  126. x=q->data;
  127. free(q);
  128.  
  129. }
  130. return x;
  131. }
  132.  
  133. int main()
  134. {
  135. int A[]={2,3,4,5,6};
  136. create(A,5);
  137. // display(first);
  138. // del(first,3);
  139. insert(first,3,56,5);
  140.  
  141. display(first);
  142.  
  143.  
  144. // display(first);
  145. return 0;
  146. }
  147.  
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
2	
3	
56	
4	
5	
6