Advertisement
Guest User

CRISTIBOSS

a guest
Oct 31st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. typedef struct elem {
  4. int info;
  5. struct elem *pred,*urm;
  6. } element;
  7. typedef struct {
  8. element *st,*cr,*sf;
  9. int lung;
  10. } dubla;
  11. dubla L;
  12. int n;
  13.  
  14.  
  15. int printme(dubla lista,int direction)
  16. {
  17. cout<<endl;
  18. if (direction==1)
  19. {
  20. element *plimb;
  21. plimb=lista.st;
  22. while (plimb!=NULL) {
  23. cout<<plimb->info<<" ";plimb=plimb->urm;
  24. }
  25. return 1;
  26. }
  27. else
  28. if (direction==0)
  29. {
  30. element *plimb;
  31. plimb=lista.sf;
  32. while (plimb!=NULL) {
  33. cout<<plimb->info<<" ";plimb=plimb->pred;
  34. }
  35. return 1;
  36. }
  37. else return 0;
  38. }
  39.  
  40.  
  41. dubla initlist()
  42. {
  43. dubla somelist;
  44. somelist.lung=0;
  45. somelist.st=somelist.cr=somelist.sf=NULL;
  46. return somelist;
  47.  
  48. }
  49.  
  50. int isempty(dubla lista)
  51. {
  52. return (lista.lung==0);
  53. }
  54.  
  55. int foundme(dubla *lista, int valoare)
  56. {
  57. element *plimb;
  58. if (!isempty(*lista))
  59. {
  60. plimb=lista->st;
  61. int gasit=0;
  62. while ((!gasit)&&(plimb!=NULL))
  63. {
  64. if (plimb->info==valoare) { gasit=1;}
  65. else plimb=plimb->urm;
  66. }
  67. if (gasit) { lista->cr=plimb; return 1;}
  68. else return 0;
  69. }
  70. else return 0;
  71. }
  72.  
  73.  
  74. dubla addme(dubla lista,int nou,int direction)
  75. {
  76. if (direction==1)
  77. {
  78. if (!isempty(lista))
  79. {
  80. element *adaugat=new element;
  81. adaugat->info=nou;
  82. if (lista.cr!=lista.sf)
  83. {
  84. element *stanga,*dreapta;
  85. stanga=lista.cr;
  86. dreapta=lista.cr->urm;
  87. stanga->urm=adaugat;
  88. adaugat->pred=stanga;
  89. adaugat->urm=dreapta;
  90. dreapta->pred=adaugat;
  91. lista.lung++;
  92. lista.cr=adaugat;
  93. return lista;
  94. }
  95. else
  96. {
  97. element *stanga;
  98. stanga=lista.cr;
  99. stanga->urm=adaugat;
  100. adaugat->pred=stanga;
  101. adaugat->urm=NULL;
  102. lista.cr=adaugat;
  103. lista.sf=adaugat;
  104. lista.lung++;
  105. return lista;
  106. }
  107. }
  108. else
  109. { // vida !
  110. element *adaugat=new element;
  111. adaugat->info=nou;
  112. adaugat->pred=NULL;
  113. adaugat->urm=NULL;
  114. lista.st=lista.cr=lista.sf=adaugat;
  115. lista.cr=adaugat;
  116. lista.lung++;
  117. return lista;
  118.  
  119. }
  120. }
  121. else
  122. {
  123. // la stanga curentului !
  124. }
  125. }
  126.  
  127. dubla delme(dubla lista,int direction)
  128. {
  129. if(L.lung==1)
  130. {
  131.  
  132. }
  133.  
  134. if (direction==1)
  135. {
  136. if(L.cr->urm!=NULL and L.cr->pred!=NULL)
  137. {
  138. element *stanga,*dreapta;
  139. stanga=lista.cr->pred;
  140. dreapta=lista.cr->urm;
  141. delete lista.cr;
  142. stanga->urm=dreapta;
  143. dreapta->pred=stanga;
  144. lista.cr->urm=dreapta->urm;
  145. lista.cr->info=dreapta->info;
  146. lista.cr->pred=dreapta->pred;
  147. lista.lung--;
  148. return lista;
  149. }
  150. else if(L.cr->urm==NULL)
  151. {
  152. element *stanga;
  153. stanga=lista.cr->pred;
  154. delete lista.cr;
  155. stanga->urm=NULL;
  156. lista.cr->urm=stanga->urm;
  157. lista.cr->info=stanga->info;
  158. lista.cr->pred=stanga->pred;
  159. lista.lung--;
  160. return lista;
  161. }
  162. else if(L.cr->pred==NULL)
  163. {
  164. element *dreapta;
  165. dreapta=lista.cr->urm;
  166. delete lista.cr;
  167. dreapta->pred=NULL;
  168. lista.cr->urm=dreapta->urm;
  169. lista.cr->info=dreapta->info;
  170. lista.cr->pred=dreapta->pred;
  171. lista.lung--;
  172. return lista;
  173. }
  174. }
  175. else
  176. if (direction==0)
  177. {
  178. if(L.cr->urm!=NULL and L.cr->pred!=NULL)
  179. {
  180. element *stanga,*dreapta;
  181. stanga=lista.cr->pred;
  182. dreapta=lista.cr->urm;
  183. delete lista.cr;
  184. stanga->urm=dreapta;
  185. dreapta->pred=stanga;
  186. lista.cr->urm=stanga->urm;
  187. lista.cr->info=stanga->info;
  188. lista.cr->pred=stanga->pred;
  189. lista.lung--;
  190. return lista;
  191. }
  192. else if(L.cr->urm==NULL)
  193. {
  194. element *stanga;
  195. stanga=lista.cr->pred;
  196. delete lista.cr;
  197. stanga->urm=NULL;
  198. lista.cr->urm=stanga->urm;
  199. lista.cr->info=stanga->info;
  200. lista.cr->pred=stanga->pred;
  201. lista.lung--;
  202. return lista;
  203. }
  204. else if(L.cr->pred==NULL)
  205. {
  206. element *dreapta;
  207. dreapta=lista.cr->urm;
  208. delete lista.cr;
  209. dreapta->pred=NULL;
  210. lista.cr->urm=dreapta->urm;
  211. lista.cr->info=dreapta->info;
  212. lista.cr->pred=dreapta->pred;
  213. lista.lung--;
  214. return lista;
  215. }
  216. }
  217. else return lista;
  218. }
  219.  
  220.  
  221. int main()
  222. {
  223. int somevalue;
  224. int right=1;
  225. int left=0;
  226. L=initlist();
  227. cout<<"N=";cin>>n;
  228. for(int i=1;i<=n;i++) L=addme(L,i,right);
  229. printme(L,right);
  230. printme(L,left);
  231. cout<<" Value = ";cin>>somevalue;
  232. foundme(&L,somevalue);
  233. cout<<endl<<" Current "<<L.cr->info<<endl;
  234. // L=addme(L,0,right);
  235. printme(L,right);
  236. printme(L,left);
  237.  
  238. delme(L,1);
  239. cout<<endl<<" Current "<<L.cr->info<<endl;
  240. printme(L,right);
  241. printme(L,left);
  242. delme(L,0);
  243.  
  244. cout<<endl<<"Current "<<L.cr->info<<endl;
  245. printme(L,right);
  246. printme(L,left);
  247. return 0;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement