1
I’m having trouble removing the first node from my double-stranded list. I created a function that returns the list type (Value) to perform the operation, by showing the list elements within the function it is possible to see that it performs the removal of the first element, however by returning it to main and listing it using the printList() function it seems to show the first element that was supposedly removed.
Follow my complete code below. Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CLEAN "cls"
#define PAUSE "pause"
typedef struct Value{
int value;
struct Value *next;
struct Value *prev;
} Value;
Value *list;
Value *start(){
return NULL;
}
void freeList(Value *list){
Value *v = list;
while(v != NULL){
printf("Liberando o canal: %d\n", v->value);
Value *temp = v->next;
free(v);
v = temp;
}
}
int isEmptyList(Value *list){
return (list == NULL);
}
void menu(){
printf("\t _______________________________________________________________");
printf("\n\t|O que Voce Deseja Fazer?\t\t\t\t\t|\n\t|");
printf("\t\t\t\t\t\t\t\t|\n\t|[01] - Adicionar valor de forma ordenada;\t\t\t|");
printf("\n\t|[02] - Adicionar valor no fim da lista;\t\t\t|");
printf("\n\t|[03] - Listar todos os valores;\t\t\t\t|");
printf("\n\t|[04] - Mostrar o maior valor;\t\t\t\t\t|");
printf("\n\t|[05] - Mostrar o menor valor;\t\t\t\t\t|");
printf("\n\t|[06] - Remover ultimo valor;\t\t\t\t\t|");
printf("\n\t|[07] - Remover primeiro valor;\t\t\t\t\t|");
printf("\n\t|[08] - Sair;\t\t\t\t\t\t\t|");
printf("\n\t|_______________________________________________________________|\n");
printf("\t Opcao: ");
}
Value *insertOrdered(Value *list){
int value;
Value *aux = list;
Value *aux2 = list;
Value *new = (Value*) malloc(sizeof(Value));
printf("Informe um valor: ");
scanf("%d", &value);
new->value = value;
if(aux == NULL){
new->next = aux;
new->prev = NULL;
}else{
while(aux != NULL){
if(new->value < aux->value){
new->next = aux;
if(aux->prev != NULL){
aux2->next = new;
aux->prev = new;
new->prev = aux2;
return list;
}else{
new->prev = NULL;
aux->prev = new;
return new;
}
}else if(aux->next == NULL){
aux->next = new;
new->prev = aux;
new->next = NULL;
return list;
}
aux2 = aux;
aux = aux->next;
}
}
return new;
}
Value *insertEnd(Value *list){
int value;
Value *aux = list;
Value *new = (Value*) malloc(sizeof(Value));
printf("Informe um valor: ");
scanf("%d", &value);
new->value = value;
while(aux != NULL){
if((aux->next == NULL) && (new->value > aux->value)){
aux->next = new;
new->prev = aux->next;
new->next = NULL;
}else if((aux->next == NULL) && (new->value < aux->value)){
printf("O numero informado e menor que o ultimmo valor\n");
printf("A ordem da lista sera comprometida\n");
break;
}
aux = aux->next;
}
return new;
}
void printList(Value *list){
Value *v = list;
if(isEmptyList(v)){
printf("Essa lista está vazia!\n");
}
else{
while(v != NULL){
printf("\nValor: %d\n", v->value);
v = v->next;
}
}
}
void findBigger(Value *list){
Value *v = list;
while(v != NULL){
if(v->next == NULL){
printf("O maior valor da lista e: %d\n", v->value);
}
v = v->next;
}
}
void findSmaller(Value *list){
printf("O maior valor da lista e: %d\n", list->value);
}
Value *removeLast(Value *list){
Value *v = list;
if(isEmptyList(v)){
printf("Essa lista está vazia!\n");
}else{
while(v != NULL){
if(v->next == NULL){
v->prev->next = NULL;
printf("Ultimo elemento removido com sucesso\n");
return list;
}
v = v->next;
}
}
}
Value *removeFirst(Value *list){
Value *v = list;
if(isEmptyList(v)){
printf("Essa lista está vazia!\n");
}else{
if(v->next != NULL){
v->next->prev = NULL;
}
list = v->next;
v->next = NULL;
printf("\nValor: %d\n", list->value);
return list;
}
}
int main(){
int op;
list = start();
do{
system(CLEAN);
menu();
scanf("%d", &op);
switch(op){
case 1:
system(CLEAN);
list = insertOrdered(list);
system(PAUSE);
break;
case 2:
system(CLEAN);
insertEnd(list);
system(PAUSE);
break;
case 3:
system(CLEAN);
printList(list);
system(PAUSE);
break;
case 4:
system(CLEAN);
findBigger(list);
system(PAUSE);
break;
case 5:
system(CLEAN);
findSmaller(list);
system(PAUSE);
break;
case 6:
system(CLEAN);
removeLast(list);
system(PAUSE);
break;
case 7:
system(CLEAN);
removeFirst(list);
system(PAUSE);
break;
case 8:
break;
}
}while(op != 8);
system(CLEAN);
printf("O programa esta liberando a memória\n");
freeList(list);
system(PAUSE);
return 0;
}
Thank you so much your answer was very helpful, also appreciate the advice, I will try to stop using hehe global variables, and had not thought to initialize the list again. I confess that I had to read more than twice a few parts to understand what you meant, but now I understand more about addressing.
– Alexandre Tanaka
I managed to do another exercise that involves removing the function at the beginning, middle or end, without many problems. Thank you very much.
– Alexandre Tanaka