Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define show show_list(lista);
- typedef struct Node{
- int *data;
- struct Node *next;
- struct Node *prev;
- }Node;
- Node * new_node(void);
- void show_list(Node *L);
- void push_back (int *pt_ENTRADA, Node **L);
- void push_front (int *pt_ENTRADA, Node **L);
- void destroi_lista( Node **L );
- int comprimento_lista( Node **L );
- int main() {
- Node *lista = new_node();
- push_front(in,&lista);
- show;
- return 0;
- }
- Node * new_node(void){
- Node *new_node;
- new_node = (Node *) malloc(sizeof(Node)) ;
- if( new_node == NULL){
- printf("\n Erro de alocacao .... falta memoria!");
- getchar();
- exit(1);
- }
- return(new_node);
- }
- void show_list(Node *L){
- int i=1;
- while( L != NULL ){
- printf("\n[%d = %d]", i , *( L -> data));//TODO
- L = L -> next;
- i++;
- }
- return;
- }
- void push_front(int *pt_data, Node ** head){
- Node * new_node;
- new_node = (Node *) malloc(sizeof(Node));
- new_node -> data = pt_data;
- new_node -> next = (*head);
- new_node -> prev = NULL ;
- (*head) = new_node ;
- }
- void push_back(int *pt_data, Node **L ){
- if( (*L) == NULL ){
- Node * novo_no = new_node();
- (*L) = novo_no;
- novo_no -> data = pt_data;
- novo_no -> next = NULL;
- novo_no -> prev = NULL;
- return ;
- }
- else{
- Node * novo_no , * corrente ;
- corrente = (*L);
- for( ; corrente -> next != NULL ; ) corrente = corrente -> next ;
- novo_no = new_node ();
- novo_no -> data = pt_data;
- novo_no -> next = NULL;
- novo_no -> prev = corrente;
- corrente -> next = novo_no;
- return ;
- }
- }
- // TODO
- int is_empty( Node *L )
- {
- return( L->next == NULL );
- }
- //
- // /* se eh o ultimo */
- // int is_last( Node *L )
- // {
- // return( L->next == NULL );
- // }
- //
- // // IGUAL a LSE
- // void destroi_lista( Node **L )
- // {
- // if( (*L) != NULL )
- // { int i=1;
- // Node * aux , *prox;
- // aux =(*L);
- // while( aux -> next != NULL )
- // {
- // prox = aux -> next;
- // //free( aux -> data ); NAO .....
- // free(aux); // AQUI LIBERA TUDO
- // aux = prox;
- // //puts(".");
- // i++;
- // } ;
- //
- // free( aux ); // PARA O ULTIMO NO
- // printf("\n Total de nosh liberados: %d ", i );
- // printf("\n LISTA LIBERADA OK\n " );
- //
- // return;
- // }
- //
- // else
- // {
- // printf("\n LISTA VAZIA ... NADA a LIBERAR \n" );
- // return;
- // }
- //
- // }
- //
- //
- // // IGUAL A LSE
- // int comprimento_lista( Node **L )
- // {
- // if( (*L) == NULL )
- // return 0;
- //
- // int i=1; // UM NO
- // // PODE SER SIMPLIFICADO
- // Node * aux , *prox;
- // aux =(*L);
- // while( aux -> next != NULL )
- // {
- // prox = aux -> next;
- // aux = prox;
- // //puts(".");
- // i++;
- // } ;
- //
- // // da para fazer sem *prox
- // //printf("\n Comprimento da lista: %d ", i );
- //
- // return i;
- //
- // }
- //
- //
- // bool exclui_n_esimo_lista( int n, Node **L )
- // {
- // if( n > comprimento_lista( L ) || n < 1)
- // {
- // printf("\n Erro na exclusao da n-esima posicao ... INVALIDA=> %d", n);
- // getchar();
- // return false;
- // }
- //
- // int i=1; // UM NO
- // Node * corrente , *anterior;
- // corrente = anterior = (*L);
- //
- // if( n == 1) // caso do 1o. da lista
- // {
- // printf("\n NOH: %d => EXCLUINDO NOME: %s", i, ( corrente -> data));
- // (*L) = corrente -> next;
- // free(corrente);
- // return true;
- // }
- // // OS DEMAIS ... CREIO QUE PODE SER MELHORADO
- // while( i < n ) // avancar ate o no em questao
- // {
- // anterior = corrente;
- // corrente = corrente -> next ; // avanca
- // i++;
- // } ;
- //
- // anterior -> next = corrente -> next ;
- // printf("\n NOH: %d => EXCLUINDO NOME: %s", i, ( corrente -> data));
- //
- // free(corrente);
- // return true;
- //
- // }
- //
- // // REUSO DE METODOS -- BOM EXEMPLO
- // bool exclui_o_ultimo_lista( Node **header_L )
- // {
- // int posicao_ultimo = comprimento_lista( &(*header_L) );
- // return ( exclui_n_esimo_lista( posicao_ultimo , &(*header_L) ));
- // // SINTAXE ESTRANHA ... mas era o endereço do ponteiro do header LISTA original
- // // ilustra o REUSO de METODOS
- // }
- //
- //
- // // Insercao na N-esima posicao ....
- //
- // bool inclui_n_esima_lista( int n, char *pt_data, Node **head_L )
- // {
- //
- // if( n > comprimento_lista( head_L ) || n < 1)
- // {
- // printf("\n Erro na inclusao .... posicao INVALIDA!");
- // getchar();
- // return false;
- // }
- //
- // int i=1; // UM NO
- // Node * new_node;
- // new_node = (Node *) malloc(sizeof(Node));
- //
- // Node * corrente , *anterior;
- // corrente = anterior = (*head_L);
- //
- // if( n == 1) // caso do 1o. da lista
- // {
- // //new_node-> x_nome = val;
- // new_node -> data = pt_data;
- // new_node -> next = (*head_L);
- // // para o primeiro da lista onde head estava apontando
- // (*head_L) = new_node ; // atualiza head
- // return true;
- // }
- // // OS DEMAIS ... CREIO QUE PODE SER MELHORADO
- // while( i < n ) // avancar ate o no em questao
- // {
- // anterior = corrente;
- // corrente = corrente -> next ; // avanca
- // i++;
- // } ;
- //
- // new_node -> data = pt_data;
- // anterior -> next = new_node ;
- // new_node -> next = corrente ;
- //
- // return true;
- //
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement