fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node{
  4. int data;
  5. struct node *nxt;
  6. }node;
  7.  
  8. node* createNode(int data){
  9. node *tmp=(node*)malloc(sizeof(node));
  10. tmp->data=data;
  11. tmp->nxt=NULL;
  12. return tmp;
  13. }
  14. node* addHead(node * head, int data){
  15. if(!head)
  16. return head=createNode(data);
  17. node *tmp=createNode(data);
  18. tmp->nxt=head;
  19. return head=tmp;
  20. }
  21. node *addTail(node*head, int data){
  22. if(!head)
  23. return head=createNode(data);
  24. node*tmp=head;
  25. while(tmp->nxt)
  26. tmp=tmp->nxt;
  27. tmp->nxt=createNode(data);
  28. return head;
  29. }
  30. node* addBefore(node* head, int x, int data){
  31. if(!head)
  32. return head;
  33. if(head->data==x){
  34. node*tmp=createNode(data);
  35. tmp->nxt=head;
  36. return head=tmp;
  37. }
  38. node*tmp=head;
  39. while(tmp->nxt && tmp->nxt->data!=x)
  40. tmp=tmp->nxt;
  41. if(tmp->nxt)
  42. {
  43. node* tmp2=createNode(data);
  44. tmp2->nxt=tmp->nxt;
  45. tmp->nxt=tmp2;
  46. }
  47. return head;
  48.  
  49. }
  50.  
  51. node* deleteNode(node* head, int x){
  52. if(!head)
  53. return head;
  54. node *tmp=head;
  55. if (head->data==x)
  56. {
  57. head=head->nxt;
  58. free(tmp);
  59. return head;
  60. }
  61. while(tmp->nxt && tmp->nxt->data!=x)
  62. tmp=tmp->nxt;
  63. if(tmp->nxt)
  64. {
  65. node* tmp2=tmp->nxt;
  66. tmp->nxt=tmp2->nxt;
  67. free(tmp2);
  68. }
  69. return head;
  70. }
  71. void display(node * head){
  72. node *tmp=head;
  73. while(tmp){
  74. printf("%d\t", tmp->data);
  75. tmp=tmp->nxt;
  76. }
  77. }
  78. int nbElemets(node *head){
  79. int nb=0;
  80. node *tmp=head;
  81. while(tmp){
  82. nb++;
  83. tmp=tmp->nxt;
  84. }
  85. return nb;
  86. }
  87. node* nthElement(node *head, int n){
  88. if(nbElemets(head)<n||n<=0)
  89. return NULL;
  90. node* p1, *p2;
  91. p1=p2=head;
  92. while(n){
  93. p1=p1->nxt;
  94. n--;
  95. }
  96. while(p1){
  97. p1=p1->nxt;
  98. p2=p2->nxt;
  99. }
  100. return p2;
  101. }
  102. node * deleteLast(node *head, int x){
  103. node * tmp=head;
  104. node *cur=NULL;
  105. node *el =NULL;
  106. if(!head)
  107. return head;
  108. while(tmp){
  109. if(tmp->data==x){
  110. cur=tmp;
  111. }
  112. tmp=tmp->nxt;
  113. }
  114. if(!cur)
  115. return head;
  116. if(cur==head)
  117. {
  118. head=head->nxt;
  119. free(cur);
  120. return head;
  121. }
  122. tmp=head;
  123. while(tmp->nxt!=cur){
  124. tmp=tmp->nxt;
  125. }
  126. el=tmp->nxt;
  127. tmp->nxt=el->nxt;
  128. free(el);
  129. return head;
  130.  
  131. }
  132.  
  133. int main(int argc, char const *argv[])
  134. {
  135. int n=10, pos;
  136. node * head=NULL;
  137. while(n){
  138. head=addHead(head, n);
  139. n--;
  140. }
  141. display(head);
  142. printf("\n");
  143. head=addTail(head, 42);
  144. printf("After adding 42 to the tail\n");
  145. display(head);
  146. printf("\n");
  147. head=addBefore(head,1,0);
  148. head=addBefore(head,5,67);
  149. printf("After adding before 1 and 5 to the tail\n");
  150. display(head);
  151. printf("\n");
  152. head=deleteNode(head, 6);
  153. head=deleteNode(head, 0);
  154. head=deleteNode(head, 10);
  155. printf("After deleting some elements from the linked list\n");
  156. display(head);
  157. printf("\n");
  158.  
  159. printf("the number of elements is: %d\n",nbElemets(head));
  160.  
  161. //printf("Donnez la position de l element a rechercher depuis la fin");
  162. //scanf("%d",&pos);
  163. pos=4;
  164. node * nth=nthElement(head, pos);
  165. printf("\n");
  166. if(nth){
  167. printf("the %d elelemt from tail is: %d",pos, nth->data);
  168. }
  169. else
  170. printf("No %d th elelemt from tail ",pos);
  171. printf("\n");
  172. head=addBefore(head,42,9);
  173. head=addBefore(head,42,1);
  174. display(head);
  175. printf("\n");
  176. head=deleteLast(head,42);
  177. printf("\n");
  178. display(head);
  179. printf("\n");
  180.  
  181. return 0;
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1	2	3	4	5	6	7	8	9	10	
After adding 42 to the tail
1	2	3	4	5	6	7	8	9	10	42	
After adding before 1 and 5 to the tail
0	1	2	3	4	67	5	6	7	8	9	10	42	
After deleting some elements from the linked list
1	2	3	4	67	5	7	8	9	42	
the number of elements is: 10

the 4 elelemt from tail is: 7
1	2	3	4	67	5	7	8	9	9	1	42	

1	2	3	4	67	5	7	8	9	9	1