Advertisement
Hydrase

LLInsertMenu

Sep 25th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #define null 0
  5. using namespace std;
  6. struct node
  7. {
  8. int data;
  9. struct node *ptr;
  10. };
  11. typedef struct node node;
  12. class llist
  13. {
  14. node *head;
  15. public:void create(void)
  16. {
  17. head=null;
  18. }
  19. void insert_beg(int data);
  20. void insert_pos(int pos,int data);
  21. void insert_end(int data);
  22. void display(void);
  23. };
  24. void llist::insert_beg(int data)
  25. {
  26. node *temp=new node;
  27. temp->data=data;
  28. temp->ptr=head;
  29. head=temp;
  30. }
  31. void llist::insert_pos(int pos,int data)
  32. {
  33. node *temp=new node;
  34. temp->data=data;
  35. node *p=head;
  36. int i=0;
  37. while(i!=pos-1)
  38. {
  39. p=p->ptr;
  40. i++;
  41. }
  42. temp->ptr=p->ptr;
  43. p->ptr=temp;
  44. }
  45. void llist::insert_end(int data)
  46. {
  47. node *temp=new node;
  48. temp->data=data;
  49. node *p=head;
  50. while(p->ptr!=null)
  51. {
  52. p=p->ptr;
  53. }
  54. p->ptr=temp;
  55. temp->ptr=NULL;
  56. }
  57. void llist::display()
  58. {
  59. node *temp=head;
  60. while(temp!=null)
  61. {
  62. cout<<"\nElement:"<<temp->data;
  63. temp=temp->ptr;
  64. }
  65. }
  66.  
  67. int main()
  68. {
  69. int choice,pos,data;
  70. char ch;
  71. llist l1;
  72. l1.create();
  73. do
  74. {
  75. cout<<"\nMAIN MENU";
  76. cout<<"\n1.First\n2.Inbetween\n3.Last\n4.Display";
  77. cout<<"\nEnter your choice of insertion:";
  78. cin>>choice;
  79. switch(choice)
  80. {
  81. case 1:cout<<"\Enter the data:";
  82. cin>>data;
  83. l1.insert_beg(data);
  84. break;
  85. case 2:cout<<"\nEnter the data:";
  86. cin>>data;
  87. cout<<"\nEnter the position:";
  88. cin>>pos;
  89. l1.insert_pos(pos,data);
  90. break;
  91. case 3:cout<<"\nEnter the data:";
  92. cin>>data;
  93. l1.insert_end(data);
  94. break;
  95. case 4:l1.display();
  96. break;
  97. case 5:cout<<"\nInvalid choice";
  98. }
  99. cout<<"\nDo you wish to continue(y/n):";
  100. cin>>ch;
  101. fflush(stdin);
  102.  
  103. }while(ch=='y');
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement