Saturday, March 26, 2016

Linked List C Example-Singly linked list with three methods


Linked list example:Singly linked list with three methods

In this c code I've just write the code for a singly linked list. Here, I have used three methods. Which are
  1. push or insert data in the linked list in line no - 10
  2. print or display the data in the linked list in line no - 24
  3. Get count the total data in the linked list in line no - 41


For more on linked list, you can see my previous large lecture on linked list
See Linked List Details



  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node
  4. {
  5. int data;
  6. struct Node *next;
  7. };
  8. typedef struct Node node;
  9. void pushData(node *list, int data)
  10. {
  11. /* Iterate through the list till we encounter the last node.*/
  12. while(list->next != NULL)
  13. {
  14. list = list -> next;
  15. }
  16. /* Allocate memory for the new node and put data in it.*/
  17. list->next = (struct node*)malloc(sizeof(struct node*));
  18. list = list->next;
  19. list->data = data;
  20. list->next = NULL;
  21. printf("Data inserted successfully..\n");
  22. }
  23. void printData(node *list)
  24. {
  25. printf("%d ", list ->data);
  26. while(list ->next != NULL){
  27. list = list ->next;
  28. printf("%d ",list->data);
  29. }
  30. /* if(list==NULL)
  31. {
  32. }
  33. printf("%d ",list->data);
  34. printData(list->next); */
  35. }
  36. int getCount(node *list){
  37. int count = 0;
  38. while(list -> next != NULL){
  39. count++;
  40. list = list->next;
  41. }
  42. return count;
  43. }
  44. int main( ){
  45. node *head,*temp;
  46. head = (struct node*)malloc(sizeof(struct node*));
  47. //head = (node *)malloc(sizeof(node));
  48. temp = head;
  49. temp -> next = NULL;
  50. int choose, value;
  51. printf("\n\nLinkedList");
  52. printf("\n\t\tEnter 1 to push data in the linked list : \n\t\tEnter 2 to see the linked list : \n\t\tEnter 3 to know the size of linked list : \n\t\tEnter 0 to exit\n---------------------------------------------\n");
  53. while(1){
  54. printf("Enter your Choose : ");
  55. scanf("%d", &choose);
  56. switch(choose){
  57. case 1:
  58. printf("Enter a value in the linked list : ");
  59. scanf("%d", &value);
  60. pushData(head, value);
  61. break;
  62. case 2:
  63. if(temp ->next == NULL){
  64. printf("Sorry No entry here ..");
  65. }else{
  66. printf("Total element in the linked list : ");
  67. printData(temp -> next);
  68. printf("\n");
  69. }
  70. break;
  71. case 3:
  72. printf("Total size of Linked list is : ");
  73. printf(" %d\n", getCount(temp->next) + 1);
  74. break;
  75. case 0:
  76. printf("Program exits\n");
  77. return 0;
  78. break;
  79. default:
  80. printf("Not match to any chosen value .\tPlease enter a correct chosen value.\n");
  81. }
  82. }
  83. }






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










No comments:

Post a Comment