-
Lists (sorting)
Hi there, this is an assignment from school in which i need some help. After struggling a little, i could make my list program in c, using only 1 char, not really that hard, but for some reason i cant do the next assignment
I gotta do it now with a full word, and sorting them. The program i made sort them sometimes, sometimes it doesnt, and throws random ASCII
Any directions would help a lot
note: program is in spanish, first option is instert element, second is delete element.
Code:
#include <stdio.h>
#include <stdlib.h>
struct tiponodo //estructura autoreferencial
{
char *letra;
struct tiponodo *next;
};
typedef tiponodo *tiponodoptr; //tipo de dato nodo apuntador
int IsEmpty(tiponodoptr sptr)
{
if (sptr == NULL)
return 1;
else
return 0;
}
void Insert(tiponodoptr *sptr, char *letra)
//inserta una letra en la lista en orden alfabetico
{
tiponodoptr nuevo, previo, actual;
nuevo = (tiponodo *) malloc(sizeof(tiponodo)); //crear un apuntador
if (nuevo != NULL) //si fue creado
{
nuevo -> letra = letra;
nuevo -> next = NULL;
previo = NULL;
actual = *sptr;
while (actual != NULL && letra > actual -> letra) //buscar la posicion de actual
{
previo = actual; //avanzar al siguiente nodo
actual = actual -> next; //una posición mas adelante
}
if (previo == NULL) //llegue al final de la lista
{
nuevo -> next = *sptr;
*sptr = nuevo;
}
else
{
previo -> next = nuevo;
nuevo -> next = actual;
}
}
else
printf("%s no fue insertado!!!, Memoria no disponible!!!\n", letra);
}
char *Delete(tiponodoptr *sptr, char *letra)
//borra un elemento de la lista y regresa el elemento borrado
{
tiponodoptr previo, actual, temporal;
if (letra == (*sptr)->letra) //primer elemento de la lista
{
temporal = *sptr;
*sptr = (*sptr) -> next; //apuntar al siguiente nodo
free(temporal); //liberar la memoria del nodo
return letra; //regresa la letra borrada
}
else //buscar el elemento a borrar
{
previo = *sptr;
actual = (*sptr) -> next;
while (actual != NULL && actual -> letra != letra)
{
previo=actual; //avanza al siguiente nodo
actual=previo -> next; //una posicion mas adelantada
}
if (actual != NULL) //se encontro la letra a borrar
{
temporal = actual;
previo -> next = actual-> next;
free(temporal);
return letra;
}
}
return " ";
}
void PrintList(tiponodoptr actualptr)
{
if (actualptr == NULL)
printf("La lista es: \n");
else
{
printf("La lista es: \n");
while (actualptr != NULL)
{
printf("%s -->" , &actualptr->letra);
actualptr = actualptr->next;
}
printf("NULL\n\n");
}
}
void operacion()
{
printf("Selecciona una opcion: \n");
printf("1.- Insertar un elemento en la lista\n");
printf("2.- Borrar un elemento en la lista \n");
printf("3.- Terminar\n");
}
int main()
{
tiponodoptr startlista = NULL;
int op;
char *item;
operacion();
printf("Elije una opcion ");
scanf("%d",&op);
fflush(stdin);
while (op != 3)
{
switch (op)
{
case 1:
printf("Dame una letra: ");
fflush(stdin);
//gets(item);
scanf("%s", &item);
Insert(&startlista, item);
PrintList(startlista);
break;
case 2:
if (!IsEmpty(startlista))
{
printf("Dame la palabra a borrar: ");
fflush(stdin);
//gets(item);
scanf("%s",&item);
if (Delete(&startlista, item))
{
printf("[%s] borrado!!! \n", &item);
PrintList(startlista);
}
else
printf("[%s] No esta en la lista \n\n" , &item);
}
else
printf("la lista esta vacia!!!\n\n");
operacion();
break;
}
printf("Elije la opcion ");
scanf("%d", &op);
}
printf("Fin del programa!!!\n\n");
}
-
This: letra > actual -> letra compares the values of the pointers, not the strings stored at those locations. If you want to compare the strings, you should use strcmp. You also never seem to acquire any memory for those strings to live in, so I'm kind of surprised that you don't just have all the pointers pointing at the same place.
-
so i redid the code a bit, still no luck, crashes after i insert the second word
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tiponodo //estructura autoreferencial
{
char *letra;
struct tiponodo *next;
};
typedef tiponodo *tiponodoptr; //tipo de dato nodo apuntador
int IsEmpty(tiponodoptr sptr)
{
if (sptr == NULL)
return 1;
else
return 0;
}
void Insert(tiponodoptr *sptr, char *letra)
//inserta una letra en la lista en orden alfabetico
{
tiponodoptr nuevo, previo, actual;
nuevo = (tiponodo *) malloc(sizeof(tiponodo)); //crear un apuntador
if (nuevo != NULL) //si fue creado
{
nuevo -> letra = letra;
nuevo -> next = NULL;
previo = NULL;
actual = *sptr;
while ((actual != NULL) && (strcmp(letra,actual -> letra)==1)) //buscar la posicion de actual
{
previo = actual; //avanzar al siguiente nodo
actual = actual -> next; //una posición mas adelante
}
if (previo == NULL) //llegue al final de la lista
{
nuevo -> next = *sptr;
*sptr = nuevo;
}
else
{
previo -> next = nuevo;
nuevo -> next = actual;
}
}
else
printf("%s no fue insertado!!!, Memoria no disponible!!!\n", letra);
}
char *Delete(tiponodoptr *sptr, char *letra)
//borra un elemento de la lista y regresa el elemento borrado
{
tiponodoptr previo, actual, temporal;
if (letra == (*sptr)->letra) //primer elemento de la lista
{
temporal = *sptr;
*sptr = (*sptr) -> next; //apuntar al siguiente nodo
free(temporal); //liberar la memoria del nodo
return letra; //regresa la letra borrada
}
else //buscar el elemento a borrar
{
previo = *sptr;
actual = (*sptr) -> next;
while (actual != NULL && strcmp(actual -> letra, letra)==1)
{
previo=actual; //avanza al siguiente nodo
actual=previo -> next; //una posicion mas adelantada
}
if (actual != NULL) //se encontro la letra a borrar
{
temporal = actual;
previo -> next = actual-> next;
free(temporal);
return letra;
}
}
return " ";
}
void PrintList(tiponodoptr actualptr)
{
if (actualptr == NULL)
printf("La lista es: \n");
else
{
printf("La lista es: \n");
while (actualptr != NULL)
{
printf("%s -->" , &actualptr->letra);
actualptr = actualptr->next;
}
printf("NULL\n\n");
}
}
void operacion()
{
printf("Selecciona una opcion: \n");
printf("1.- Insertar un elemento en la lista\n");
printf("2.- Borrar un elemento en la lista \n");
printf("3.- Terminar\n");
}
void main()
{
tiponodoptr startlista = NULL;
int op;
char *item;
operacion();
printf("Elije una opcion ");
scanf("%d",&op);
fflush(stdin);
while (op != 3)
{
switch (op)
{
case 1:
printf("Dame una letra: ");
fflush(stdin);
//gets(item);
scanf("%s", &item);
//strcat(item, "\0");
Insert(&startlista, item);
PrintList(startlista);
break;
case 2:
if (!IsEmpty(startlista))
{
printf("Dame la palabra a borrar: ");
fflush(stdin);
//gets(item);
scanf("%s",&item);
//strcat(item, "\0");
if (Delete(&startlista, item))
{
printf("[%s] borrado!!! \n", &item);
PrintList(startlista);
}
else
printf("[%s] No esta en la lista \n\n" , &item);
}
else
printf("la lista esta vacia!!!\n\n");
operacion();
break;
}
printf("Elije la opcion ");
scanf("%d", &op);
}
printf("Fin del programa!!!\n\n");
}