Posted By

CyKy on 12/29/10


Tagged

datastructersdata structerslistsstack


Versions (?)

Lists


 / Published in: C
 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include<conio.h>
  5.  
  6.  
  7. struct ListCell{
  8. int info;
  9. ListCell *urmatorul;
  10. ListCell *anterior;
  11. };
  12.  
  13. ListCell *begin,*end;
  14. int cSize;
  15.  
  16.  
  17. void init();
  18. void afisare();
  19. void delElem(int);
  20. void delEnd();
  21. void delBegin();
  22. int append(int);
  23. int addBegin(int);
  24. int addEnd(int);
  25. int insert(int,int);
  26. int isEmpty();
  27.  
  28. int main(){
  29. int info,optiune,index;
  30. init();
  31. while(optiune>0){
  32. printf(">Optiune:");
  33. scanf("%d",&optiune);
  34. printf("\n");
  35. switch(optiune){
  36. case 0:{
  37. printf("\t>Exit!\n");
  38. optiune=0;
  39. break;
  40. }
  41. default:{
  42. printf("\t>Optiune necunoscuta!\n");
  43. break;
  44. }
  45. case 1:{
  46. printf("\t>Adauga la inceput!\n");
  47. printf("\t>Informatia=");
  48. scanf("%d",&info);
  49. if(addBegin(info)){
  50. printf("\t>Done\n");
  51. }else{
  52. printf("\t>Error\n");
  53. }
  54. break;
  55. }
  56. case 2:{
  57. printf("\t>Afisare informatie din lista!\n");
  58. printf("\t>Informatia:");
  59. afisare();
  60. printf("\n");
  61. break;
  62. }
  63. case 3:{
  64. printf("\t>Adaugare la sfarsit!\n");
  65. printf("\t>Informatia=");
  66. scanf("%d",&info);
  67. if(append(info)){
  68. printf("\t>Done\n");
  69. }else{
  70. printf("\t>Error\n");
  71. }
  72. break;
  73. }
  74. case 4:{
  75. printf("\t>Adaugare la un index!\n");
  76. printf("\t>Informatia=");
  77. scanf("%d",&info);
  78. printf("\t>Index=");
  79. scanf("%d",&index);
  80. if(insert(info,index)){
  81. printf("\t>Done\n");
  82. }else{
  83. printf("\t>Error\n");
  84. }
  85. break;
  86. }
  87. case 5:{
  88. printf("\t>Golirea listei!\n");
  89. init();
  90. printf("\t>Done\n");
  91. break;
  92. }
  93. case 6:{
  94. printf("\t>Sterge de la inceput!\n");
  95. delBegin();
  96. break;
  97. }
  98.  
  99. case 7:{
  100. printf("\t>Sterge de la sfarsit!\n");
  101. delEnd();
  102. break;
  103. }
  104.  
  105. case 8:{
  106. printf("\t>Stergere o informatie!\n");
  107. printf("\t>Informatie=");
  108. scanf("%d",&info);
  109. delElem(info);
  110. break;
  111. }
  112. }
  113. printf("\n");
  114. }
  115. //getch();
  116. return 0;
  117. }
  118.  
  119. void init(){
  120. begin=NULL;
  121. end=NULL;
  122. cSize=0;
  123. }
  124.  
  125. int isEmpty(){
  126. if(begin==end && end==NULL){
  127. return 0;
  128. }else{
  129. return 1;
  130. }
  131. }
  132.  
  133. int insert(int info,int index){
  134. ListCell *aux=new ListCell;
  135. if(aux==NULL){
  136. return 0;
  137. }else{
  138. aux->info=info;
  139. ListCell *buf;
  140. buf=begin;
  141. int i=1;
  142. while(buf!=NULL && i<index){
  143. buf=buf->urmatorul;
  144. i++;
  145. }
  146. if(buf!=NULL){
  147. aux->anterior=buf;
  148. aux->urmatorul=buf->urmatorul;
  149. buf->urmatorul->anterior=aux;
  150. buf->urmatorul=aux;
  151. return 1;
  152. }
  153. }
  154. }
  155. int addBegin(int info){
  156. ListCell *aux=new ListCell;
  157. if(aux==NULL){
  158. return 0;
  159. }else{
  160. if(begin!=NULL){
  161. aux->info=info;
  162. aux->urmatorul=begin;
  163. begin->anterior=aux;
  164. aux->anterior=NULL;
  165. begin=aux;
  166. }else{
  167. aux->info=info;
  168. aux->urmatorul=NULL;
  169. aux->anterior=NULL;
  170. begin=aux;
  171. end=begin;
  172. }
  173. cSize++;
  174. return 1;
  175. }
  176. }
  177.  
  178. void afisare(){
  179. ListCell *contor=begin;
  180. while(contor!=NULL){
  181. printf("%d ",contor->info);
  182. contor=contor->urmatorul;
  183. }
  184. }
  185. int append(int info){
  186. ListCell *aux=new ListCell;
  187. aux->urmatorul=NULL;
  188.  
  189. if(aux==NULL){
  190. return 0;
  191. }
  192.  
  193. aux->info=info;
  194. if(end==NULL){
  195. end=begin=aux;
  196. }
  197. else{
  198. end->urmatorul=aux;
  199. aux->anterior=end;
  200. end=aux;
  201. cSize++;
  202. }
  203.  
  204. return 1;
  205. }
  206. void delBegin(){
  207. if(begin!=NULL){
  208. ListCell *aux=begin->urmatorul;
  209. delete begin;
  210. begin=aux;
  211. if(aux!=NULL){
  212. begin->anterior=NULL;
  213. cSize--;
  214. }else{
  215. cSize=0;
  216. begin=end=NULL;
  217. }
  218. }else{
  219. init();
  220. }
  221. }
  222.  
  223.  
  224. void delEnd(){
  225. ListCell *aux=end;
  226. if(aux==NULL){
  227. begin=end=NULL;
  228. }else{
  229. end=aux->anterior;
  230. delete aux;
  231. if(end!=NULL){
  232. end->urmatorul=NULL;
  233. cSize--;
  234. }else{
  235. init();
  236. }
  237. }
  238. }
  239.  
  240. void delElem(int info){
  241. ListCell *aux=begin;
  242. while(aux!=NULL && aux->info!=info && aux->urmatorul!=NULL){
  243. aux=aux->urmatorul;
  244. }
  245. if(aux!=NULL){
  246. if (aux == end){
  247. delEnd();
  248. cSize=0;
  249. }else{
  250. aux->anterior->urmatorul=aux->urmatorul;
  251. aux->urmatorul->anterior=aux->anterior;
  252. delete aux;
  253. cSize--;
  254. }
  255. }else{
  256. init();
  257. }
  258. }

Report this snippet  

You need to login to post a comment.