• Breaking News

    Funny Coder

    Funny coder is an open source web for interested programmer. It is a programming environment.It's a way where you can code with fun.

    Saturday, March 26, 2016

    C code of full linked list all the methods(insert, delete, search, display,exit)



    In this c code I've tried to complete the all of the task of linked list.Here, four methods of linked list has been used :

    1. insert method at line no : 9
    2. search method at line no : 24
    3. delete method at line no : 40
    4. print method at line no : 64
    And start my main method at line no : 75





    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. typedef struct Node
    4. {
    5. int data;
    6. struct Node *next;
    7. }
    8. }node;
    9. void insert(node *pointer, int data)
    10. {
    11. {
    12. while(pointer->next!=NULL)
    13. {
    14. pointer = pointer -> next;
    15. }
    16. pointer->next = (node *)malloc(sizeof(node));
    17. pointer = pointer->next;
    18. pointer->data = data;
    19. pointer->next = NULL;
    20. }
    21. }
    22. int search(node *pointer, int key)
    23. {
    24. {
    25. pointer = pointer -> next;
    26. while(pointer!=NULL)
    27. {
    28. if(pointer->data == key) .
    29. {
    30. return 1;
    31. }
    32. pointer = pointer -> next;
    33. }
    34. return 0;
    35. }
    36. }
    37. void delete(node *pointer, int data)
    38. {
    39. {
    40. while(pointer->next!=NULL && (pointer->next)->data != data)
    41. {
    42. pointer = pointer -> next;
    43. }
    44. if(pointer->next==NULL)
    45. {
    46. printf("Element %d is not present in the list\n",data);
    47. return;
    48. }
    49. node *temp;
    50. temp = pointer -> next;
    51. pointer->next = temp->next;
    52. free(temp);
    53. return;
    54. }
    55. }
    56. void print(node *pointer)
    57. {
    58. {
    59. if(pointer==NULL)
    60. {
    61. return;
    62. }
    63. printf("%d ",pointer->data);
    64. print(pointer->next);
    65. }
    66. }
    67. int main()
    68. {
    69. {
    70. node *start,*temp;
    71. start = (node *)malloc(sizeof(node));
    72. temp = start;
    73. temp -> next = NULL;
    74. printf("\t\tLinked List\n-------------------------------------\n");
    75. printf(" Press 1 to Insert Data in the linked list\n");
    76. printf(" Press 2 Delete data from the linked list\n");
    77. printf(" Press 3 Print data of the linked list\n");
    78. printf(" Press 4 Find data from the linked data from the linked list\n");
    79. printf(" Press 0 To exit the program\n");
    80. while(1)
    81. {
    82. int query;
    83. printf("Enter your option: ");
    84. scanf("%d",&query);
    85. if(query==1)
    86. {
    87. int data;
    88. printf("Enter a value in the linked list : ");
    89. scanf("%d",&data);
    90. insert(start,data);
    91. printf("Value inserted successfully\n");
    92. }
    93. else if(query==2)
    94. {
    95. int data;
    96. printf("Enter a data do you want to delete from the linked list: ");
    97. scanf("%d",&data);
    98. delete(start,data);
    99. }
    100. else if(query==3)
    101. {
    102. printf("The list is ");
    103. print(start->next);
    104. printf("\n");
    105. }
    106. else if(query==4)
    107. {
    108. int data;
    109. scanf("%d",&data);
    110. int status = search(start,data);
    111. if(status)
    112. {
    113. printf("Element Found\n");
    114. }
    115. else
    116. {
    117. printf("Element Not Found\n");
    118. }
    119. }else if(query == 0){
    120. print("!!!..Program exited successfully..!!!");
    121. return 0;
    122. }
    123. }
    124. return 0;
    125. }
    126. }
    
    
    Suggestion:
    
    
    Don't try to direct copy paste please. This habit will damage your coding power. 
    Coding is an energy, is a habit, is a funny....
    So, try to solve your coding problem by yourself.
    For more on linked list, you can see my previous  large lecture on linked list.
    
    
    See Linked List Details
    
    
    Ok, since if you face any problem, you kindly write your comment in the comment box...Or mail me:
    manirujjamanakash@gmail.com. Or in facebook : https://web.facebook.com/manirujjaman.akash. You can better take your eyes in our facebook fanfage for getting regular post from funny-coder https://web.facebook.com/prgraamming.jagath





    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel