sahadat49

DLLIST

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