Listeye Eleman Ekleme, Sondan Silme

    • Listeye Eleman Ekleme, Sondan Silme

      C Kaynak Kodu

      1. #include<stdio.h>
      2. #include<conio.h>
      3. #include<stdlib.h>
      4. struct node
      5. {
      6. int data;
      7. struct node *next;
      8. }*head,*var,*trav;
      9. void insert_at_begning(int value)
      10. {
      11. var=(struct node *)malloc(sizeof (struct node));
      12. var->data=value;
      13. if(head==NULL)
      14. {
      15. head=var;
      16. head->next=NULL;
      17. }
      18. else
      19. {
      20. var->next=head;
      21. head=var;
      22. }
      23. }
      24. void insert_at_end(int value)
      25. {
      26. struct node *temp;
      27. temp=head;
      28. var=(struct node *)malloc(sizeof (struct node));
      29. var->data=value;
      30. if(head==NULL)
      31. {
      32. head=var;
      33. head->next=NULL;
      34. }
      35. else
      36. {
      37. while(temp->next!=NULL)
      38. {
      39. temp=temp->next;
      40. }
      41. var->next=NULL;
      42. temp->next=var;
      43. }
      44. }
      45. void insert_at_middle(int value, int loc)
      46. {
      47. struct node *var2,*temp;
      48. var=(struct node *)malloc(sizeof (struct node));
      49. var->data=value;
      50. temp=head;
      51. if(head==NULL)
      52. {
      53. head=var;
      54. head->next=NULL;
      55. }
      56. else
      57. {
      58. while(temp->data!=loc && temp->next!=NULL ) //eleman yoksa son eleman olarak ekler
      59. {
      60. temp=temp->next;
      61. }
      62. var2=temp->next;
      63. temp->next=var;
      64. var->next=var2;
      65. }
      66. }
      67. int delete_from_end()
      68. {
      69. struct node *temp;
      70. temp=head;
      71. while(temp->next != NULL)
      72. {
      73. var=temp;
      74. temp=temp->next;
      75. }
      76. if(temp ==head)
      77. {
      78. head=temp->next;
      79. free(temp);
      80. return 0;
      81. }
      82. printf("Listenin son elemani silindi:%d",temp->data);
      83. var->next=NULL;
      84. free(temp);
      85. return 0;
      86. }
      87. void display()
      88. {
      89. trav=head;
      90. if(trav==NULL)
      91. {
      92. printf("\nListe Bos");
      93. }
      94. else
      95. {
      96. printf("\nListenin Elemanlari: ");
      97. while(trav!=NULL)
      98. {
      99. printf(" -> %d ",trav->data);
      100. trav=trav->next;
      101. }
      102. printf("\n");
      103. }
      104. }
      105. int main()
      106. {
      107. int i=0;
      108. head=NULL;
      109. insert_at_begning(10);
      110. insert_at_begning(20);
      111. insert_at_begning(1);
      112. insert_at_begning(9);
      113. insert_at_end(49);
      114. delete_from_end();
      115. display();
      116. getch();
      117. }
      Hepsini Göster