Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. struct elem
  9. {
  10. int st;
  11. elem *linkPre;
  12. elem *linkAf;
  13. };
  14. int br;
  15. elem *first;
  16. elem *last;
  17.  
  18. void init()
  19. {
  20. br=0;
  21. first=NULL;
  22. last=NULL;
  23. }
  24.  
  25. void addfirst(int el)
  26. {
  27. elem *p=new elem;
  28. p->st=el;
  29. if(!first)
  30. {
  31. p->linkPre=NULL;
  32. p->linkAf=NULL;
  33. br++;
  34. first=p;
  35. last=p;
  36. return;
  37. }
  38. p->linkAf=first;
  39. p->linkPre=NULL;
  40. first=p;
  41. br++;
  42. }
  43.  
  44. void addlast(int el)
  45. {
  46. elem *p=new elem;
  47. p->st=el;
  48. if(!first)
  49. {
  50. p->linkPre=NULL;
  51. p->linkAf=NULL;
  52. br++;
  53. first=p;
  54. last=p;
  55. return;
  56. }
  57. p->linkPre=last;
  58. p->linkAf=NULL;
  59. last=p;
  60. br++;
  61. }
  62.  
  63. void addposition(int el,int pos)
  64. {
  65. elem *p=new elem;
  66. p->st=el;
  67. if(!first)
  68. {
  69. p->linkPre=NULL;
  70. p->linkAf=NULL;
  71. br++;
  72. first=p;
  73. last=p;
  74. return;
  75. }
  76. if(pos<=1)
  77. {
  78. addfirst(el);
  79. return;
  80. }
  81. if(pos>br)
  82. {
  83. addlast(el);
  84. return;
  85. }
  86. int i=2;
  87. elem *q=first;
  88. while(i<pos)
  89. {
  90. q=q->linkAf;
  91. i++;
  92. }
  93. p->linkAf=q->linkAf;
  94. p->linkPre=q;
  95. q->linkAf=p;
  96. q=p->linkAf;
  97. q->linkPre=p;
  98. br++;
  99. }
  100.  
  101. void delfirst(int *el)
  102. {
  103. if(!first)
  104. {
  105. return;
  106. }
  107. elem *p=first;
  108. *el=p->st;
  109. first=first->linkAf;
  110. first->linkPre=NULL;
  111. delete p;
  112. br--;
  113. }
  114.  
  115. void dellast(int *el)
  116. {
  117. if(!first)
  118. {
  119. return;
  120. }
  121. elem *p=last;
  122. *el=p->st;
  123. last=last->linkPre;
  124. last->linkAf=NULL;
  125. delete p;
  126. br--;
  127. }
  128.  
  129. void delposition(int *el,int pos)
  130. {
  131. if(!first)
  132. {
  133. return;
  134. }
  135. if(pos<=1)
  136. {
  137. delfirst(el);
  138. return;
  139. }
  140. if(pos>=br)
  141. {
  142. dellast(el);
  143. return;
  144. }
  145. elem *p=first;
  146. int i=2;
  147. while(i<=pos)
  148. {
  149. p=p->linkAf;
  150. i++;
  151. }
  152. *el=p->st;
  153. elem *q=p->linkAf;
  154. q->linkPre=p->linkPre;
  155. p=p->linkPre;
  156. p->linkAf=q;
  157. delete p;
  158. br--;
  159. }
  160.  
  161. void delstruct()
  162. {
  163. if(!first)
  164. {
  165. return;
  166. }
  167. elem *p=first;
  168. while(p)
  169. {
  170. first=first->linkAf;
  171. delete p;
  172. p=first;
  173. }
  174. first=NULL;
  175. last=NULL;
  176. br=0;
  177. }
  178.  
  179. int main()
  180. {
  181. int el;
  182. addlast(5);
  183. addlast(7);
  184. addlast(1);
  185. dellast(&el);
  186. cout<<el<<' ';
  187. dellast(&el);
  188. cout<<el<<' ';
  189. dellast(&el);
  190. cout<<el<<' ';
  191. delstruct();
  192. return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement