#Simple list

39 messages · Page 1 of 1 (latest)

plain cradleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

hot hull
#

!f

jaunty zealotBOT
#

@ripe saddle's code (missing deletion permissions), requested by @hot hull:
Hello, sorry, could someone help me to know my error in this code to insert in a simple list please


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct nodo {
    int id;
    char tipoTaco[20];
    int cantidadTacos;
    char saborAgua[20];
    int cuenta;
    struct nodo* sig;
} lista;

void insertar(lista** ptr,
              int id,
              char* tipoTaco,
              int cantidadTacos,
              char* saborAgua,
              int cuenta) {
    lista *pt1, *pt2;
    pt1 = *ptr;

    if (pt1 == NULL) {
        pt1 = (lista*)malloc(sizeof(lista));

        if (pt1 != NULL) {
            pt1->id = id;
            strcpy(pt1->tipoTaco, tipoTaco);
            pt1->cantidadTacos = cantidadTacos;
            strcpy(pt1->saborAgua, saborAgua);
            pt1->cuenta = cuenta;
            pt1->sig = NULL;
            *ptr = pt1;
        } else {
            printf("No se pudo crear el nodo\n");
        }
    } else {
        while (pt1->sig != NULL)
            pt1 = pt1->sig;

        pt2 = (lista*)malloc(sizeof(lista));

        if (pt2 != NULL) {
            pt2->id = id;
            strcpy(pt1->tipoTaco, tipoTaco);
            pt1->cantidadTacos = cantidadTacos;
            strcpy(pt1->saborAgua, saborAgua);
            pt1->cuenta = cuenta;
            pt2->sig = NULL;
            pt1->sig = pt2;
        } else {
            printf("No se pudo crear el nodo\n");
        }
    }
}

void ver_lista(lista* ptr) {
    while (ptr != NULL) {
        printf(" %d\t", ptr->id);
        printf(" %s\t", ptr->tipoTaco);
        printf(" %d\t", ptr->cantidadTacos);
        printf(" %s\t", ptr->saborAgua);
        printf(" %d\t", ptr->cuenta);
        ptr = ptr->sig;
    }
}
}```
hot hull
#

You have an extra }

ripe saddle
#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct nodo{
   int id;
   char tipoTaco[20];
   int cantidadTacos;
   char saborAgua[20];
   int cuenta;
   struct nodo *sig;
} lista;

void insertar(lista **ptr, int id, char *tipoTaco, int cantidadTacos, char *saborAgua, int cuenta){
   lista *pt1, *pt2;
   pt1 = *ptr;

   if (pt1 == NULL){

      pt1 = (lista *)malloc(sizeof(lista));

      if (pt1 != NULL){
         pt1->id = id;
         strcpy(pt1->tipoTaco, tipoTaco);
         pt1->cantidadTacos = cantidadTacos;
         strcpy(pt1->saborAgua, saborAgua);
         pt1->cuenta = cuenta;
         pt1->sig = NULL;
         *ptr = pt1;
      }
      else{
         printf("No se pudo crear el nodo\n");
      }
   }
   else{

      while (pt1->sig != NULL)
         pt1 = pt1->sig;

      pt2 = (lista *)malloc(sizeof(lista));

      if (pt2 != NULL){
         pt2->id = id;
         strcpy(pt1->tipoTaco, tipoTaco);
         pt1->cantidadTacos = cantidadTacos;
         strcpy(pt1->saborAgua, saborAgua);
         pt1->cuenta = cuenta;
         pt2->sig = NULL;
         pt1->sig = pt2;
      }
      else{
         printf("No se pudo crear el nodo\n");
      }
   }
}

void ver_lista(lista *ptr){

   while (ptr != NULL){
      printf(" %d\t", ptr->id);
      printf(" %s\t", ptr->tipoTaco);
      printf(" %d\t", ptr->cantidadTacos);
      printf(" %s\t", ptr->saborAgua);
      printf(" %d\t", ptr->cuenta);
      ptr = ptr->sig;
   }
}

//TODO: Por hacer
void ver_lista_id(lista *ptr, int id){

   while( ptr != NULL ){

      if( ptr->id == id ){
         printf(" %d\t", ptr->id);
         printf(" %s\t", ptr->tipoTaco);
         printf(" %d\t", ptr->cantidadTacos);
         printf(" %s\t", ptr->saborAgua);
         printf(" %d\t", ptr->cuenta);
      }
      else{
         printf( "\nValor no encontrado..." );
      }
   }
}
hot hull
#

What errors are you running into

ripe saddle
#
int main(void){

   lista list;
   //struct lista ordenes[3];

   int id;
   char tipoTaco[20];
   int cantidadTacos;
   char saborAgua[30];
   int cuenta;

   printf( "Bienvenido a The Taco :D\n" );
   printf( "Ingrese el id del taco: " );
   scanf( "%d", &id );
   printf( "Ingrese el tipo de taco: " );
   scanf( "%s", tipoTaco );
   printf( "Ingrese la cantidad de datos: " );
   scanf( "%d", &cantidadTacos );
   printf( "Ingrese el sabor del agua: " );
   scanf( "%s", saborAgua );
   printf( "Ingrese la cuenta: " );
   scanf( "%d", &cuenta );

   insertar( &list, id, tipoTaco, cantidadTacos, saborAgua, cuenta );
   ver_lista( &list );   

   return 0;
}```
hot hull
#

Kind of hard to help with code written in another language

#
ripe saddle
hot hull
#

Pay attention to compiler warnings

#

Also there's a tool called address sanitizer which will identify memory errors in your program

ripe saddle
#

Insertar is Insert

hot hull
ripe saddle
ripe saddle
hot hull
#

@ripe saddle You should probably have lista* list; in main not lista list;

#

keep in mind lista is a node itself

ripe saddle
hot hull
#

lista** and lista* are very different types

#

Trying to treat a lista* as a lista** will certainly result in errors

#

Hence the warning from the compiler

ripe saddle
#

Ok, I understand that they are different, but I don't know how I could treat them to make my program work.

#

@hot hull By the way, sorry if you don't understand anything I write, I'm using the Google translator

#

Is this the solution?

#
int main(void){

   lista *list;

   int id;
   char tipoTaco[20];
   int cantidadTacos;
   char saborAgua[30];
   int cuenta;

   printf( "Bienvenido a The Taco :D\n" );
   printf( "Ingrese el id del taco: " );
   scanf( "%d", &id );
   printf( "Ingrese el tipo de taco: " );
   scanf( "%s", tipoTaco );
   printf( "Ingrese la cantidad de datos: " );
   scanf( "%d", &cantidadTacos );
   printf( "Ingrese el sabor del agua: " );
   scanf( "%s", saborAgua );
   printf( "Ingrese la cuenta: " );
   scanf( "%d", &cuenta );

   insertar( &list, id, tipoTaco, cantidadTacos, saborAgua, cuenta );
   ver_lista( list );   

   return 0;
}```
hot hull
#

yep

#

Typically you never put linked list nodes on the stack, which is what lista list; would be

ripe saddle
#

I already tested the code and it doesn't throw any warning, but it doesn't print the ver_lista function 😦

hot hull
#

insertar isn't quite right

ripe saddle
plain cradleBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

ripe saddle
#

!solved