Thread: Lists (sorting)

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    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");
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    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");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Sorting two lists into one?
    By Coder527 in forum C++ Programming
    Replies: 8
    Last Post: 10-04-2006, 01:21 AM
  3. sorting in linked lists
    By vice in forum C Programming
    Replies: 2
    Last Post: 05-07-2005, 10:07 PM
  4. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  5. Sorting Linked Lists (code not concept)
    By Newbie Magic in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2004, 08:57 AM