Jesuino (4) Mar 17, 2010 at 6:39am UTC
Hi guys.
I'm having a Strange problem. I can't make a pointer NULL, look at the function "remover". I'm trying to delete the first element in the list, but i can not.
I'm using DevCpp, and my plataform is Windows.
Could Someone Help-me?
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#include <stdio.h>
struct _lista{
int info;
struct _lista *prox;
struct _lista *prev;
};
void inserirNoInicio(struct _lista *lista, int info){
struct _lista *temp;
while (lista->prev != NULL){
lista = lista->prev;
}
temp = malloc(sizeof (struct _lista));
temp->info = info;
temp->prox = lista;
temp->prev = NULL;
lista->prev = temp;
}
void inserirNoFinal(struct _lista *lista, int info){
struct _lista *temp;
while (lista->prox != NULL){
lista = lista->prox;
}
temp = malloc(sizeof (struct _lista));
temp->info = info;
temp->prev = lista;
temp->prox = NULL;
lista->prox = temp;
}
void inserirNoMeio(struct _lista *lista, int info){
int cont = 0, posMeio;
struct _lista *temp;
while (lista->prev != NULL){
lista = lista->prev;
}
while (lista->prox != NULL){
lista = lista->prox;
cont++;
}
posMeio = cont/2;
cont = 0;
while (lista->prev != NULL && cont++ != posMeio){
lista = lista->prev;
}
temp = malloc(sizeof (struct _lista));
temp->info = info;
temp->prox = lista;
temp->prev = lista->prev;
lista->prev->prox = temp;
lista->prev = temp;
}
void listar(struct _lista *lista){
while (lista->prev != NULL){
lista = lista->prev;
}
while (lista){
printf("%d\t" , lista->info);
lista = lista->prox;
}
printf("\n" );
}
void remover(struct _lista *lista, int pos){
pos--;
int atualPos = 0;
while (lista->prev != NULL){
lista = lista->prev;
}
while (atualPos++ != pos && lista != NULL ){
lista = lista->prox;
}
if (lista == NULL){
printf("Naum ha elemento a remover\n" );
return ;
}
//Não é o último
if (lista->prox){
//Não é o primeiro elemento
if (lista->prev){
lista->prev->prox = lista->prox;
lista->prox->prev = lista->prev;
}
//Primeiro elemento
else {
printf("%p\n" , lista);
lista = lista->prox;
printf("%p\n" , lista);
lista->prev = NULL;
}
}
//Último elemento
else if (lista->prev){
lista = lista->prev;
lista->prox = NULL;
}
else {
printf("este eh o elemento unico da lista..." );
}
}
int main(){
struct _lista *teste;
int i, eliminado;
teste = malloc(sizeof (struct _lista));
teste->info = 1;
teste->prev = NULL;
teste->prox = NULL;
for (i=2;i<7;i++){
inserirNoFinal(teste, i);
}
inserirNoMeio(teste, 555);
listar(teste);
printf("Quem voce deseja remover?\t" ); scanf("%d" , &eliminado);
printf("%p\n" , teste);
remover(teste, eliminado);
printf("%p\n" , teste);
printf("%d\n" , teste->info);
listar(teste);
system("pause" );
}
/*
a)inserirNoInicio
b)inserirNoFinal
c)inserirNoMeio
d)listar
e)remover*/
Best Regards!
hamsterman (358) Mar 17, 2010 at 6:39am UTC
That's becouse inside remover you only modify local pointers, but teste keeps pointing to the same memory location.
You can change _lista* lista to _lista*& lista . It might be a wrong way to do this, but it works.
Hope this helps
Last edited on Mar 17, 2010 at 6:39am UTC
Jesuino (4) Mar 17, 2010 at 6:39am UTC
Man!! Its Work, Sorry for my noob question....
I made a pointer to pointer. I do not know how I could forgot it, Java made me a dummy LoLz...
The solution that I adopted:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
void remover(struct _lista **lista, int pos){
// struct _lista *lista = *pLista;
pos--;
int atualPos = 0;
while ((*lista)->prev != NULL){
(*lista) = (*lista)->prev;
}
while (atualPos++ != pos && lista != NULL ){
(*lista) = (*lista)->prox;
}
if (lista == NULL){
printf("Naum ha elemento a remover\n" );
return ;
}
//Não é o último
if ((*lista)->prox){
//Não é o primeiro elemento
if ((*lista)->prev){
(*lista)->prev->prox = (*lista)->prox;
(*lista)->prox->prev = (*lista)->prev;
}
//Primeiro elemento
else {
(*lista) = (*lista)->prox;
(*lista)->prev = NULL;
}
}
//Último elemento
else if ((*lista)->prev){
(*lista) = (*lista)->prev;
(*lista)->prox = NULL;
}
else {
printf("este eh o elemento unico da lista..." );
}
}
Thanks a lot!
This topic is archived - New replies not allowed.