bishalbiswas

linked list

Sep 3rd, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<malloc.h>
  3. #include<iostream>
  4. using namespace std;
  5. void insert_last();
  6. void display();
  7. struct node
  8. {
  9. int info;
  10. struct node *link;
  11. } *start=NULL;
  12. int item;
  13. int main()
  14. {
  15. int choice;
  16. do
  17. {
  18. printf("\n\n\n1. Insert Last\n2. Display\n3. Exit\n");
  19. printf("\nEnter your choice: ");
  20. scanf("%d",&choice);
  21. switch(choice)
  22. {
  23. case 1:
  24. insert_last();
  25. break;
  26. case 2:
  27. display();
  28. break;
  29. case 3:
  30. return 0;
  31. default:
  32. printf("\n\nInvalid choice. Please try again.\n");
  33. }
  34. } while (1);
  35. }
  36.  
  37. void insert_last()
  38. {
  39. struct node *ptr;
  40. printf("\n\nEnter item: ");
  41. scanf("%d", &item);
  42. if(start == NULL)
  43. {
  44. start = (struct node *)malloc(sizeof(struct node));
  45. start->info = item;
  46. start->link = NULL;
  47. }
  48. else
  49. {
  50. ptr =start;
  51. while (ptr->link != NULL)
  52. {
  53. ptr = ptr->link;
  54. }
  55.  
  56. ptr->link = (struct node *)malloc(sizeof(struct node));
  57. ptr = ptr->link;
  58. ptr->info = item;
  59. ptr->link = NULL;
  60.  
  61.  
  62. }
  63. printf("\nItem inserted: %d\n", item);
  64. }
  65. void display()
  66. {
  67. struct node *ptr = start;
  68. int i=1;
  69. if (ptr == NULL)
  70. printf("\nLinklist is empty.\n");
  71. else
  72. {
  73. printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
  74. while(ptr != NULL)
  75. {
  76. printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,ptr->link);
  77. ptr = ptr->link;
  78. i++;
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment