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.  
  88. int main(int argc, char const *argv[])
  89. {
  90. int n=10;
  91. node * head=NULL;
  92. while(n){
  93. head=addHead(head, n);
  94. n--;
  95. }
  96. display(head);
  97. printf("\n");
  98. head=addTail(head, 42);
  99. printf("After adding 42 to the tail\n");
  100. display(head);
  101. printf("\n");
  102. head=addBefore(head,1,0);
  103. head=addBefore(head,5,67);
  104. printf("After adding before 1 and 5 to the tail\n");
  105. display(head);
  106. printf("\n");
  107. head=deleteNode(head, 6);
  108. head=deleteNode(head, 0);
  109. head=deleteNode(head, 10);
  110. printf("After deleting some elements from the linked list\n");
  111. display(head);
  112. printf("\n");
  113.  
  114. printf("the number of elements is: %d\n",nbElemets(head));
  115. return 0;
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
Success #stdin #stdout 0s 5324KB
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