Share Pastebin
Guest
Public paste!

ejemplo de listas simples en C

By: a guest | Mar 17th, 2010 | Syntax: C++ | Size: 2.96 KB | Hits: 375 | Expires: Never
Copy text to clipboard
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. struct lista
  7. {
  8. int num;
  9. struct lista *sig;
  10. };
  11. typedef struct lista nodo;
  12.  
  13. void inicio();
  14. void final();
  15. void mostrar();
  16. void del();
  17. void antes();
  18. void despues();
  19.  
  20. nodo *cab=NULL;
  21. nodo *ult=NULL;
  22.  
  23. void main()
  24. {
  25. int opc=0;
  26. while(opc!=7)
  27. {
  28. clrscr();
  29. printf("MENU\n1.- ingresar al inicio\n2.- ingresar al final\n3.- insertar antes\n4.- insertar despues\n5.- imprimir\n6.- eliminar\n7.- salir\ningrese su opcion: ");
  30. scanf("%d",&opc);
  31. switch(opc)
  32. {
  33. case 1:
  34. inicio();
  35. break;
  36. case 2:
  37. final();
  38. break;
  39. case 3:
  40. antes();
  41. break;
  42. case 4:
  43. despues();
  44. break;
  45. case 5:
  46. mostrar();
  47. break;
  48. case 6:
  49. del();
  50. break;
  51. }
  52. }
  53. }
  54.  
  55. void inicio()
  56. {
  57. nodo *nuevo;
  58. char resp='s';
  59. while(resp!='n')
  60. {
  61. nuevo=((nodo*)malloc(sizeof(nodo)));
  62. printf("\ningrese el nuevo numero: ");
  63. scanf("%d",&nuevo->num);
  64. nuevo->sig=cab;
  65. cab=nuevo;
  66. if(ult==NULL)
  67. {
  68. ult=nuevo;
  69. }
  70. printf("\ndesea agregar otro numero? s/n: ");
  71. fflush(stdin);
  72. scanf("%c",&resp);
  73. }
  74. }
  75.  
  76. void final()
  77. {
  78. nodo *nuevo;
  79. char resp='s';
  80. while(resp!='n')
  81. {
  82. nuevo=((nodo*)malloc(sizeof(nodo)));
  83. printf("\ningrese el nuevo numero: ");
  84. scanf("%d",&nuevo->num);
  85. nuevo->sig=NULL;
  86. ult->sig=nuevo;
  87. ult=nuevo;
  88. if(cab==NULL)
  89. {
  90. cab=nuevo;
  91. }
  92. printf("\ndesea agregar otro numero? s/n: ");
  93. fflush(stdin);
  94. scanf("%c",&resp);
  95. }
  96. return;
  97. }
  98.  
  99. void mostrar()
  100. {
  101. nodo *ptr;
  102. ptr=cab;
  103. while(ptr)
  104. {
  105. printf(" %d ",ptr->num);
  106. ptr=ptr->sig;
  107. }
  108. getch();
  109. return;
  110. }
  111.  
  112. void del()
  113. {
  114. nodo *ptr;
  115. nodo *pkr;
  116. nodo *aux=NULL;
  117. int num, y=0, x=0;
  118. ptr=cab;
  119. pkr=cab;
  120. printf("\ningrese el numero a borrar: ");
  121. scanf("%d",&num);
  122. while(ptr)
  123. {
  124. if(num==ptr->num)
  125. {
  126. aux=ptr;
  127. y=1;
  128. }
  129. while(y==1)
  130. {
  131. if(aux==cab)
  132. {
  133. cab=aux->sig;
  134. x=1;
  135. y=0;
  136. printf("\nNUMERO ELIMINADO");
  137. getch();
  138. }
  139. if((pkr->sig==aux)&&(x==0))
  140. {
  141. pkr->sig=aux->sig;
  142. printf("\nNUMERO ELIMINADO");
  143. getch();
  144. y=0;
  145. }
  146. pkr=pkr->sig;
  147. }
  148. ptr=ptr->sig;
  149. }
  150. }
  151.  
  152. void antes()
  153. {
  154. nodo *nuevo, *ptr, *pkr, *aux;
  155. int num, y=0;
  156. char resp='s';
  157. ptr=cab;
  158. pkr=cab;
  159. while(resp!='n')
  160. {
  161. nuevo=((nodo*)malloc(sizeof(nodo)));
  162. printf("\ningrese el nuevo numero: ");
  163. scanf("%d",&nuevo->num);
  164. printf("\nantes de quien quiere ingresar? INGRESE EL NUMERO: ");
  165. scanf("%d",&num);
  166. while(ptr)
  167. {
  168. if(num==ptr->num)
  169. {
  170. aux=ptr;
  171. y=1;
  172. }
  173. while(y==1)
  174. {
  175. if(pkr->sig==aux)
  176. {
  177. pkr->sig=nuevo;
  178. nuevo->sig=aux;
  179. y=0;
  180. }
  181. pkr=pkr->sig;
  182. }
  183. ptr=ptr->sig;
  184. }
  185. printf("\ndesea agregar otro numero? s/n: ");
  186. fflush(stdin);
  187. scanf("%c",&resp);
  188. }
  189.  
  190. }
  191.  
  192. void despues()
  193. {
  194. nodo *ptr, *nuevo;
  195. ptr=cab;
  196. char resp='s';
  197. int num;
  198. while(resp!='n')
  199. {
  200. nuevo=((nodo*)malloc(sizeof(nodo)));
  201. printf("\ningrese el nuevo numero: ");
  202. scanf("%d",&nuevo->num);
  203. printf("\ndespues de quien quiere ingresar? INGRESE EL NUMERO: ");
  204. scanf("%d",&num);
  205. while(ptr)
  206. {
  207. if(num==ptr->num)
  208. {
  209. nuevo->sig=ptr->sig;
  210. ptr->sig=nuevo;
  211. }
  212. ptr=ptr->sig;
  213. }
  214. printf("\ndesea agregar otro numero? s/n: ");
  215. fflush(stdin);
  216. scanf("%c",&resp);
  217. }
  218. }