Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node{
  5. int data;
  6. node *next;
  7. node(int d)
  8. {
  9. data=d;
  10. next=NULL;
  11. }
  12. };
  13. void pushlast(node *& head,int value)
  14. {
  15. node *temp=new node(value);
  16. if(head==NULL)
  17. {
  18. head=temp;
  19. }
  20. else
  21. {
  22. node *p=NULL;
  23. p=head;
  24. while(p->next!=NULL)
  25. {
  26. p=p->next;
  27. }
  28. p->next=temp;
  29. }
  30. }
  31. void pushfront(node *& head,int value)
  32. {
  33. node* temp=new node(value);
  34. temp->next=head;
  35. head=temp;
  36. }
  37. void showlist(node * head)
  38. {
  39. node *p=NULL;
  40. p=head;
  41. while(p->next!=NULL)
  42. {
  43. cout<<p->data<<" ";
  44. p=p->next;
  45. }
  46. cout<<p->data<<"\n";
  47. }
  48. void mid(node *head)
  49. {
  50. node *slow,*fast;
  51. slow=head;
  52. fast=head;
  53. int check=0;
  54. while(fast!= NULL)
  55. {
  56. fast=fast->next;
  57. check=++check%2;
  58. if(check==0)
  59. {
  60. slow=slow->next;
  61. }
  62. }
  63. cout<<"mid element:"<<slow->data<<"\n";
  64. }
  65. void kth_node_last(node * head,int k)
  66. {
  67. node *slow,*fast;
  68. slow=head;
  69. fast=head;
  70. cout<<k;
  71. while(k--)
  72. {
  73. fast=fast->next;
  74. }
  75. while(fast!=NULL)
  76. {
  77. fast=fast->next;
  78. slow=slow->next;
  79. }
  80. cout<<"th element from last:"<<slow->data<<"\n";
  81. }
  82. void reverse(node *& head)
  83. {
  84. node *curr,*prev,*upnext;
  85. curr=head;
  86. prev=NULL;
  87. upnext=head;
  88. while(curr!=NULL)
  89. {
  90. upnext=curr->next;
  91. curr->next=prev;
  92. prev=curr;
  93. curr=upnext;
  94. }
  95. head=prev;
  96. }
  97.  
  98. node * merge(node *a,node * b)
  99. {
  100.  
  101. if(b==NULL)
  102. {
  103. return a;
  104. }
  105.  
  106. if((a->data) <= (b->data))
  107. {
  108. node *temp=NULL;
  109. temp=a->next;
  110. a->next=b;
  111. b=temp;
  112.  
  113. }
  114. else
  115. {
  116. node *temp=NULL;
  117. temp=b->next;
  118. b->next=a;
  119. a=b;
  120. b=temp;
  121. }
  122. a->next=merge(a->next,b);
  123. return a;
  124. }
  125.  
  126. void loop(node *head)
  127. {
  128. node *slow,*fast;
  129. slow=head;
  130. fast=head->next;
  131. while(slow!=fast){
  132. slow=slow->next;
  133. fast=fast->next->next;
  134. if(fast->next==NULL)
  135. {
  136. cout<<"loop is not present";
  137. return;
  138. }
  139. }
  140. cout<<"loop is present";
  141. }
  142.  
  143. int main()
  144. {
  145. node *a=NULL,*b=NULL;
  146. pushlast(a,2);
  147. pushlast(a,5);
  148. pushlast(a,6);
  149. pushlast(a,9);
  150. pushlast(a,10);
  151. pushlast(b,4);
  152. pushlast(b,7);
  153. pushlast(b,8);
  154. pushlast(b,12);
  155. pushfront(b,1);
  156. cout<<"a is :";
  157. showlist(a);
  158. cout<<"\nb is: ";
  159. showlist(b);
  160. a=merge(a,b);
  161. cout<<"\nnew a :";
  162. showlist(a);
  163. cout<<"\nnew b :";
  164. showlist(b);
  165.  
  166. // node *head=NULL;
  167. // pushlast(head,5);
  168. // pushlast(head,6);
  169. // pushlast(head,8);
  170. // pushfront(head,9);
  171. // pushfront(head,1);
  172. // pushfront(head,10);
  173. // pushfront(head,11);
  174. // pushfront(head,15);
  175. // showlist(head);
  176. // mid(head);
  177. // kth_node_last(head,5);
  178. // reverse(head);
  179. // showlist(head);
  180.  
  181. }
  182.  
  183.  
  184.  
  185. // if((a->next==NULL)||(b==NULL))
  186. // {
  187. // return NULL;
  188. // }
  189. // if(a->data <= b->data)
  190. // {
  191. // node *temp=a;
  192. // temp=temp->next;
  193. // a->next=b;
  194. // b=temp;
  195.  
  196. // }
  197. // else if(a-> data > b->data)
  198. // {
  199. // a=a->next;
  200. // }
  201. // a->next=merge(a,b);
  202. // return a;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement