sahadat49

SLLIST

Nov 27th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node{
  5. int val;
  6. node *next;
  7. };
  8. struct SLList{
  9. node *head,*tail;
  10. SLList();
  11. void insert(int n);
  12. void addToHead(int n);
  13. void addToTail(int n);
  14. void delHead();
  15. void delTail();
  16. void print();
  17. void SLmaximum();
  18. void SLminimum();
  19. void SL2ndminimum();
  20. void SLsort();
  21. void SLfind(int n);
  22. };
  23.  
  24. SLList :: SLList(){
  25. head=tail=NULL;
  26. }
  27.  
  28. void SLList::insert (int x){
  29. node *n=new node();
  30. n->val=x;
  31. if(head == NULL){
  32. head=tail=n;
  33. tail->next=NULL;
  34. return;
  35. }
  36. tail->next=n;
  37. tail=n;
  38. tail->next=NULL;
  39. }
  40.  
  41. void SLList :: addToHead(int x){
  42. node *t=new node();
  43. t->val=x;
  44. if(head==NULL){
  45. t->next=NULL;
  46. head=tail=t;
  47. }
  48. else{
  49. t->next=head;
  50. head=t;
  51. }
  52. }
  53.  
  54. void SLList::delHead(){
  55. if(head==NULL)
  56. return;
  57. if (head==tail){
  58. delete head;
  59. head=tail=NULL;
  60. }
  61. else{
  62. node *t=head;
  63. head = head -> next;
  64. delete t;
  65. }
  66. }
  67.  
  68.  
  69. void SLList::print(){
  70. node *cur=head;
  71. while(cur!=NULL){
  72. cout<<cur->val<<" ";
  73. cur=cur->next;
  74. }
  75. cout << endl;
  76. }
  77.  
  78.  
  79. void SLList::addToTail(int x){
  80. node *t=new node();
  81. t->val=x;
  82. if(tail==NULL){
  83. t->next=NULL;
  84. tail=head=t;
  85. }
  86. else{
  87. tail->next=t;
  88. tail=t;
  89. t->next=NULL;
  90. }
  91. }
  92.  
  93.  
  94. void SLList::delTail(){
  95. if(tail==NULL)
  96. return;
  97. if(head==tail){
  98. delete tail;
  99. head=tail=NULL;
  100. }
  101. else{
  102. node *t=head,*m;
  103. while(t->next!=NULL){
  104. m=t;
  105. t=t->next;
  106. }
  107. m->next=NULL;
  108. delete t;
  109. }
  110. }
  111.  
  112.  
  113.  
  114. void SLList::SLmaximum(){
  115. if(head==NULL)
  116. return;
  117. else{
  118. node *n=head;
  119. int m=-999999999;
  120. while(n!=NULL){
  121. if(n->val >m)
  122. m=n->val;
  123. n=n->next;
  124. }
  125. cout<<"Maximum = "<<m<<endl;
  126. if(head == tail)
  127. return;
  128. n = head;
  129. int m2 = -999999999;
  130. while(n != NULL){
  131. if(n->val > m2 && n->val < m)
  132. m2 = n->val;
  133. n=n->next;
  134. }
  135. if(m2!=-999999999)
  136. cout<<"Second Maximum = "<<m2<<endl;
  137. }
  138. }
  139.  
  140. void SLList::SLminimum(){
  141. if(head==NULL)
  142. return;
  143. else{
  144. node *n=head;
  145. int m=999999999;
  146. while(n!=NULL){
  147. if(n->val < m)
  148. m=n->val;
  149. n=n->next;
  150. }
  151. cout<<"Minimum = "<<m<<endl;
  152. if(head==tail)
  153. return;
  154. n = head;
  155. int m2 = 999999999;
  156. while(n != NULL){
  157. if(n->val < m2 && n->val > m)
  158. m2 = n->val;
  159. n=n->next;
  160. }
  161. if(m2!=999999999)
  162. cout<<"Second Maximum = "<<m2<<endl;
  163. }
  164. }
  165.  
  166.  
  167. void SLList::SLsort(){
  168. if(head==NULL || head==tail)
  169. return;
  170. node *n=head,*t;
  171. int temp,ma;
  172. while(n!=NULL){
  173. t=n->next;
  174. while(t!=NULL){
  175. if(n->val > t-> val){
  176. temp=n->val;
  177. n->val=t->val;
  178. t->val=temp;
  179. }
  180. t=t->next;
  181. }
  182. n=n->next;
  183. }
  184. }
  185.  
  186. void SLList::SLfind(int n){
  187. node *t=head;
  188. if(head==NULL){
  189. cout<<"Not Found"<<endl;
  190. return;
  191. }
  192. while(t!=NULL){
  193. if(t->val==n){
  194. cout<<"Found"<<endl;
  195. return;
  196. }
  197. t=t->next;
  198. }
  199. cout<<"Not Found"<<endl;
  200. }
  201.  
  202. int main(){
  203. SLList m;
  204. m.insert(6);
  205. m.insert(4);
  206. m.insert(3);
  207. m.insert(9);
  208. m.insert(7);
  209. m.insert(1);
  210. m.delTail();
  211. m.delHead();
  212. m.SLfind(3);
  213. m.print();
  214.  
  215. return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment