Advertisement
hadiuzzaman65

Untitled

Mar 17th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6. typedef struct hadi
  7. {
  8. int data;
  9. struct hadi *next;
  10. } node;
  11.  
  12.  
  13. void insertlinklist(node *newnode,int data)
  14. {
  15. while(newnode->next !=NULL)
  16. {
  17. newnode=newnode->next;
  18. }
  19. newnode->next=(node*)malloc(sizeof(node));
  20. newnode->next->data=data;
  21. newnode->next->next=NULL;
  22. }
  23. void displaylinklist(node *newnode)
  24. {
  25. while(newnode->next !=NULL)
  26. {
  27. cout<< newnode->next->data<<" ";
  28. newnode=newnode->next;
  29. }
  30. cout<<endl<<endl;
  31. }
  32. void deletenode(node *newnode,int data)
  33. {
  34. while(newnode->next !=NULL)
  35. {
  36. if(newnode->next->data==data)
  37. {
  38. newnode->next=newnode->next->next;
  39. }
  40. newnode=newnode->next;
  41. }
  42. }
  43. void deletenodeatindex(node *newnode,int data)
  44. {
  45. node *s;
  46. s=newnode;
  47. if(newnode==NULL)
  48. {
  49.  
  50. return;
  51. }
  52. if(data==0)
  53. {
  54. newnode=s->next;
  55. free(s);
  56. return;
  57. }
  58. for(int i=0; s !=NULL&&i<data-1; i++)
  59. {
  60. s=s->next;
  61. }
  62. if(s==NULL ||s->next ==NULL)
  63. {
  64. return;
  65. }
  66. node *temp;
  67. temp=s->next->next;
  68. free(s->next);
  69. s->next=temp;
  70.  
  71.  
  72. }
  73. void foundposition(node *head,int position)
  74. {
  75. node *temp;
  76. temp=head;
  77. int i=0;
  78. while(temp !=NULL)
  79. {
  80. if(position==i)
  81. {
  82. cout<< temp->data<<endl;
  83. }
  84. temp=temp->next;
  85. i++;
  86. }
  87.  
  88. }
  89. int main()
  90. {
  91. string s(50,'*');
  92. string p(55,'-');
  93. node *head;
  94. head=(node*)malloc(sizeof(node));
  95. head->next=NULL;
  96. insertlinklist(head,60);
  97. insertlinklist(head,16);
  98. insertlinklist(head,66);
  99. insertlinklist(head,8);
  100. insertlinklist(head,1);
  101. insertlinklist(head,5);
  102. insertlinklist(head,88);
  103. insertlinklist(head,69);
  104. insertlinklist(head,98);
  105. insertlinklist(head,32);
  106. insertlinklist(head,77);
  107. insertlinklist(head,33);
  108. cout<<s<<endl;
  109. cout<< "THIS IS LAB REPORT OF LINK LIST USING SWITCH CASE:"<<endl<<endl;
  110. cout<< "SUPPOSE THIS INPUT ARE ALREADY INSERT IN LINK LIST: "<<endl;
  111. cout<<p<<endl;
  112. displaylinklist(head);
  113. cout<<p<<endl;
  114. cout<< "\n1.DISPLAY LIST\n2.LINEAR SEARCH\n"
  115. "3.DELETE NODE USING INDEX\n4.DELETE NODE USING DATA"<<endl;
  116. cout<<s<<endl;
  117.  
  118. int n;
  119. cin>>n;
  120.  
  121. switch(n)
  122. {
  123. case 1:
  124. {
  125. displaylinklist(head);
  126. break;
  127.  
  128. }
  129. case 2:
  130. {
  131. int n;
  132. cout<< "inter search element: ";
  133. cin>>n;
  134. foundposition(head,n);
  135.  
  136. break;
  137. }
  138. case 3:
  139. {
  140. int n;
  141. cout<< "enter the index which you can delete? ";
  142. cin>>n;
  143. deletenodeatindex(head,n);
  144. cout<< "now update list is: "<<endl;
  145. displaylinklist(head);
  146.  
  147. break;
  148. }
  149. case 4:
  150. {
  151. int n;
  152. cout<< "enter the data which you can delete? ";
  153. cin>>n;
  154. deletenode(head,n);
  155. cout<< "now update list is: "<<endl;
  156. displaylinklist(head);
  157. break;
  158. }
  159. default:
  160. cout<< "INVALID INPUT. PLEASE SELECT CORRECT INPUT\n";
  161. }
  162.  
  163.  
  164. return 0;
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement