Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 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 (direction==1)
  130. {
  131. element *stanga,*dreapta;
  132. stanga=lista.cr->pred;
  133. dreapta=lista.cr->urm;
  134. delete lista.cr;
  135. stanga->urm=dreapta;
  136. dreapta->pred=stanga;
  137. lista.cr->info=dreapta->info;
  138. lista.cr->urm=dreapta->urm;
  139. lista.cr->pred=dreapta->pred;
  140. lista.lung--;
  141. return lista;
  142. }
  143. else
  144. if (direction==0)
  145. {
  146. element *stanga,*dreapta;
  147. stanga=lista.cr->pred;
  148. dreapta=lista.cr->urm;
  149. stanga->urm=dreapta;
  150. dreapta->pred=stanga;
  151. element *tarus=new element;
  152. tarus=lista.cr;
  153. lista.cr=stanga;
  154. delete tarus;
  155. lista.lung--;
  156. return lista;
  157. }
  158. else return lista;
  159. }
  160.  
  161.  
  162. int main()
  163. {
  164. int somevalue;
  165. int right=1;
  166. int left=0;
  167. L=initlist();
  168. cout<<"N=";cin>>n;
  169. for(int i=1;i<=n;i++) L=addme(L,i,right);
  170. printme(L,right);
  171. printme(L,left);
  172. cout<<" Value = ";cin>>somevalue;
  173. foundme(&L,somevalue);
  174. cout<<endl<<" Current "<<L.cr->info<<endl;
  175. // L=addme(L,0,right);
  176. printme(L,right);
  177. printme(L,left);
  178.  
  179. L=delme(L,0);
  180. cout<<endl<<" Current "<<L.cr->info<<endl;
  181.  
  182. printme(L,right);
  183. printme(L,left);
  184. // delme(L,1);
  185. // printme(L,right);
  186. // printme(L,left);
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement