Advertisement
Guest User

list

a guest
May 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #define show show_list(lista);
  6.  
  7. typedef struct Node{
  8.     int *data;
  9.     struct Node *next;
  10.     struct Node *prev;
  11. }Node;
  12.  
  13.  
  14.  
  15. Node * new_node(void);
  16. void show_list(Node *L);
  17. void push_back  (int *pt_ENTRADA, Node **L);
  18. void push_front (int *pt_ENTRADA, Node **L);
  19. void destroi_lista( Node **L );
  20. int  comprimento_lista( Node **L );
  21.  
  22. int main() {
  23.     Node *lista = new_node();
  24.    
  25.     push_front(in,&lista);
  26.     show;
  27.     return 0;
  28. }
  29.  
  30. Node * new_node(void){
  31.  Node  *new_node;
  32.  new_node = (Node *) malloc(sizeof(Node)) ;
  33.  
  34.  if( new_node == NULL){
  35.         printf("\n Erro de alocacao .... falta memoria!");
  36.         getchar();
  37.         exit(1);
  38.     }
  39.     return(new_node);
  40. }
  41.  
  42. void show_list(Node *L){
  43.   int i=1;
  44.   while( L != NULL ){
  45.       printf("\n[%d = %d]", i , *( L -> data));//TODO
  46.       L = L -> next;
  47.       i++;
  48.     }
  49.     return;
  50. }
  51.  
  52.  
  53. void push_front(int *pt_data, Node ** head){
  54.     Node * new_node;
  55.     new_node = (Node *) malloc(sizeof(Node));
  56.     new_node -> data = pt_data;
  57.     new_node -> next = (*head);
  58.     new_node -> prev = NULL ;
  59.     (*head) = new_node ;
  60. }
  61.  
  62. void push_back(int *pt_data,  Node **L ){
  63.   if( (*L) == NULL ){
  64.         Node * novo_no = new_node();
  65.         (*L) = novo_no;
  66.         novo_no  -> data = pt_data;
  67.         novo_no -> next = NULL;
  68.         novo_no -> prev = NULL;
  69.         return ;
  70.     }
  71.     else{
  72.         Node * novo_no , * corrente ;
  73.         corrente = (*L);
  74.         for( ; corrente -> next != NULL ; ) corrente = corrente -> next ;
  75.         novo_no = new_node ();
  76.         novo_no  -> data = pt_data;
  77.         novo_no -> next = NULL;
  78.         novo_no -> prev = corrente;
  79.         corrente -> next = novo_no;
  80.         return ;
  81.     }
  82. }
  83.  
  84. // TODO
  85. int is_empty( Node *L )
  86.     {
  87.         return( L->next == NULL );
  88.     }
  89. //
  90. // /* se eh o ultimo */
  91. // int is_last( Node *L )
  92. // {
  93. //  return( L->next == NULL );
  94. // }
  95. //
  96. // // IGUAL a LSE
  97. // void destroi_lista( Node **L )
  98. //   {
  99. //    if( (*L) != NULL )
  100. //       { int i=1;
  101. //         Node  * aux , *prox;
  102. //         aux =(*L);
  103. //        while( aux -> next != NULL )
  104. //        {
  105. //          prox = aux -> next;
  106. //          //free( aux ->  data );  NAO .....
  107. //          free(aux); // AQUI LIBERA TUDO
  108. //          aux = prox;
  109. //          //puts(".");
  110. //          i++;
  111. //        } ;
  112. //
  113. //        free( aux ); // PARA O ULTIMO NO
  114. //        printf("\n Total de nosh liberados: %d ", i );
  115. //        printf("\n LISTA LIBERADA OK\n " );
  116. //
  117. //        return;
  118. //        }
  119. //
  120. //      else
  121. //      {
  122. //         printf("\n LISTA VAZIA ... NADA a LIBERAR \n" );
  123. //         return;
  124. //      }
  125. //
  126. //   }
  127. //
  128. //
  129. // // IGUAL A LSE
  130. // int comprimento_lista( Node **L )
  131. //   {
  132. //    if( (*L) == NULL )
  133. //    return 0;
  134. //
  135. //     int i=1; // UM NO
  136. //     // PODE SER SIMPLIFICADO
  137. //         Node  * aux , *prox;
  138. //         aux =(*L);
  139. //        while( aux -> next != NULL )
  140. //        {
  141. //          prox = aux -> next;
  142. //          aux = prox;
  143. //          //puts(".");
  144. //          i++;
  145. //        } ;
  146. //
  147. //      // da para fazer sem *prox
  148. //     //printf("\n Comprimento da lista: %d ", i );
  149. //
  150. //     return i;
  151. //
  152. //   }
  153. //
  154. //
  155. // bool exclui_n_esimo_lista( int n, Node **L )
  156. //   {
  157. //    if( n > comprimento_lista( L ) || n < 1)
  158. //    {
  159. //    printf("\n Erro na exclusao da n-esima posicao ... INVALIDA=>  %d", n);
  160. //    getchar();
  161. //    return false;
  162. //  }
  163. //
  164. //     int i=1; // UM NO
  165. //      Node  * corrente , *anterior;
  166. //      corrente = anterior = (*L);
  167. //
  168. //  if( n == 1) // caso do 1o. da lista
  169. //      {
  170. //    printf("\n NOH: %d => EXCLUINDO NOME: %s", i, ( corrente -> data));
  171. //       (*L) = corrente -> next;
  172. //       free(corrente);
  173. //       return true;
  174. //  }
  175. //  // OS DEMAIS ... CREIO QUE PODE SER MELHORADO
  176. //     while( i < n ) // avancar ate o no em questao
  177. //   {
  178. //     anterior = corrente;
  179. //     corrente = corrente -> next ; // avanca
  180. //      i++;
  181. //   } ;
  182. //
  183. //     anterior -> next = corrente -> next ;
  184. //  printf("\n NOH: %d => EXCLUINDO NOME: %s", i, ( corrente -> data));
  185. //
  186. //     free(corrente);
  187. //     return true;
  188. //
  189. //   }
  190. //
  191. // // REUSO DE METODOS -- BOM EXEMPLO
  192. // bool exclui_o_ultimo_lista(  Node **header_L )
  193. //  {
  194. //    int posicao_ultimo = comprimento_lista( &(*header_L) );
  195. //    return ( exclui_n_esimo_lista( posicao_ultimo , &(*header_L) ));
  196. //    // SINTAXE ESTRANHA ... mas era o endereço do ponteiro do header LISTA original
  197. //    // ilustra o REUSO de METODOS
  198. //  }
  199. //
  200. //
  201. //  // Insercao na N-esima posicao ....
  202. //
  203. // bool inclui_n_esima_lista( int n, char *pt_data, Node **head_L )
  204. //   {
  205. //
  206. //    if( n > comprimento_lista( head_L ) || n < 1)
  207. //    {
  208. //    printf("\n Erro na inclusao .... posicao INVALIDA!");
  209. //    getchar();
  210. //    return false;
  211. //  }
  212. //
  213. //     int i=1; // UM NO
  214. //     Node * new_node;
  215. //     new_node = (Node *) malloc(sizeof(Node));
  216. //
  217. //      Node  * corrente , *anterior;
  218. //      corrente = anterior = (*head_L);
  219. //
  220. //  if( n == 1) // caso do 1o. da lista
  221. //      {
  222. //     //new_node-> x_nome = val;
  223. //      new_node  -> data = pt_data;
  224. //      new_node -> next = (*head_L);
  225. //      // para o primeiro da lista  onde head estava apontando
  226. //      (*head_L) = new_node ; // atualiza head
  227. //       return true;
  228. //  }
  229. //  // OS DEMAIS ... CREIO QUE PODE SER MELHORADO
  230. //     while( i < n ) // avancar ate o no em questao
  231. //   {
  232. //     anterior = corrente;
  233. //     corrente = corrente -> next ; // avanca
  234. //        i++;
  235. //   } ;
  236. //
  237. //     new_node  -> data = pt_data;
  238. //     anterior -> next = new_node ;
  239. //     new_node -> next = corrente ;
  240. //
  241. //     return true;
  242. //
  243. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement