Guest User

Untitled

a guest
Dec 15th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct lista{
  5. int info;
  6. struct lista *prox;
  7. } TLSE;
  8.  
  9. typedef struct pilha{
  10. TLSE *prim;
  11. }TPilha;
  12.  
  13. TLSE* inserel(TLSE *l, int elem);
  14. void imprimel(TLSE *l);
  15. void liberal(TLSE *l);
  16. TLSE* retiral(TLSE *l, int elem);
  17. TLSE* buscal(TLSE *l, int elem);
  18.  
  19. void push(TPilha *p, int elem);
  20. int pop(TPilha *p);
  21. int vazia_p(TPilha *p);
  22. TPilha *inicializa_p(void);
  23. void libera_p(TPilha *p);
  24. void imprime_p(TPilha *p);
  25.  
  26. TPilha*copia(TPilha*p);
  27. TPilha* misc (TPilha*p, int y);
  28.  
  29. int main(void){
  30. int n;
  31. TPilha*p=inicializa_p();
  32. int op=1;
  33. while(op==1)
  34. {
  35. printf("Digite um valor para alocar na pilha: ");
  36. scanf("%d",&n);
  37. push(p,n);
  38. printf("Operar novamente? 1 ou 0? ");
  39. scanf("%d",&op);
  40. }
  41. printf("Pilha original:\n");
  42. imprime_p(p);
  43. printf("Qual valor de miscelania? ");
  44. int m;
  45. scanf("%d",&m);
  46. TPilha*resp=misc(p,m);
  47. printf("\n");
  48. printf("Pilha misc:\n");
  49. imprime_p(resp);
  50. libera_p(p);
  51. libera_p(resp);
  52. return 0;
  53. }
  54. TPilha* misc (TPilha*p, int y)
  55. {
  56. TPilha*resp=inicializa_p();
  57. TPilha*aux=inicializa_p();
  58. resp=copia(p);
  59. int x;
  60. int fora;
  61. while(!vazia_p(resp))
  62. {
  63. x=pop(resp);
  64. if(x==y)
  65. {
  66. if(!vazia_p(resp))fora=pop(resp);
  67. if(!vazia_p(aux))fora=pop(aux);
  68. push(aux,x);
  69. }
  70. else
  71. {
  72. push(aux,x);
  73. }
  74. }
  75. while(!vazia_p(aux))
  76. {
  77. x=pop(aux);
  78. push(resp,x);
  79. }
  80. libera_p(aux);
  81. return resp;
  82. }
  83. TPilha*copia(TPilha*p)
  84. {
  85. TPilha*Copia=inicializa_p();
  86. TPilha*aux=inicializa_p();
  87. int aux_i;
  88. while(!vazia_p(p))
  89. {
  90. aux_i=pop(p);
  91. push(aux,aux_i);
  92. }
  93. while(!vazia_p(aux))
  94. {
  95. aux_i=pop(aux);
  96. push(Copia,aux_i);
  97. push(p,aux_i);
  98. }
  99. libera_p(aux);
  100. return Copia;
  101. }
  102. TLSE* inserel(TLSE *l, int elem){
  103. TLSE *novo = (TLSE *) malloc(sizeof(TLSE));
  104. novo->prox = l;
  105. novo->info = elem;
  106. return novo;
  107. }
  108.  
  109. void imprimel(TLSE *l){
  110. TLSE *p = l;
  111. while(p){
  112. printf("% d ", p->info);
  113. p = p->prox;
  114. }
  115. }
  116.  
  117. void liberal(TLSE *l){
  118. TLSE *p = l, *q;
  119. while(p){
  120. q = p;
  121. p = p->prox;
  122. free(q);
  123. }
  124. }
  125.  
  126. TLSE* retiral(TLSE *l, int elem){
  127. TLSE *p = l, *ant = NULL;
  128. while((p) && (p->info != elem)){
  129. ant = p;
  130. p = p->prox;
  131. }
  132. if(!p) return l;
  133. if(!ant) l = l->prox;
  134. else ant->prox = p->prox;
  135. free(p);
  136. return l;
  137. }
  138.  
  139. TLSE* buscal(TLSE *l, int elem){
  140. TLSE *p = l;
  141. while((p) && (p->info != elem)) p = p->prox;
  142. return p;
  143. }
  144. TPilha *inicializa_p(void){
  145. TPilha *p = (TPilha *)malloc(sizeof(TPilha));
  146. p->prim = NULL;
  147. return p;
  148. }
  149.  
  150. int vazia_p(TPilha *p){
  151. return (p->prim == NULL);
  152. }
  153.  
  154. void push(TPilha *p,int elem){
  155. TLSE *novo = (TLSE *)malloc(sizeof(TLSE));
  156. novo->info = elem;
  157. novo->prox = p->prim;
  158. p->prim = novo;
  159. }
  160.  
  161. int pop(TPilha *p){
  162. if (!vazia_p(p)){
  163. TLSE *aux = p->prim;
  164. int x = aux->info;
  165. p->prim = aux->prox;
  166. free(aux);
  167. return x;
  168. }
  169. else{ // pilha vazia
  170. exit(1);
  171. }
  172. }
  173.  
  174. void libera_p(TPilha *p){
  175. TLSE *q = p->prim,*r ;
  176. while(q){
  177. r = q;
  178. q=q->prox;
  179. free(r);
  180. }
  181. free(p);
  182. }
  183.  
  184. void imprime_p(TPilha *p){
  185. TPilha *aux = inicializa_p();
  186. while (!vazia_p(p)){
  187. int x = pop(p);
  188. printf("%d ",x);
  189. push(aux,x);
  190. }
  191. while(!vazia_p(aux)){
  192. push(p,pop(aux));
  193. }
  194.  
  195. libera_p(aux);
  196. }
Add Comment
Please, Sign In to add comment