elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


  Mostrar Temas
Páginas: [1]
1  Programación / Programación C/C++ / ¿Como controlar punteros a estructuras y meterlo en una funcion? en: 25 Marzo 2022, 03:55 am
tengo una tarea de estructura de datos, donde debo de poner para cada nodo un siguiente, un anterior y un dato de tipo string.

Código:
struct node {
    string data;
    struct node* prev;
    struct node* next;
};

El problema es que tengo que hacer una lista doblemente ligada circular, que no tengo problema al crearla de la siguiente manera:

Código:
#include <iostream>
#include <stdlib.h>

using namespace std;
int main()
{
    struct node* head = makeNode();
    head->data = "Juan";

    struct node* newnode = makeNode();
    newnode->data = "Carlos";

    struct node* temp = head->next;
    newnode->next = temp;
    newnode->prev = head;
    head->next = newnode;
    newnode->prev = head->next;

    Print(head);
    Print(newnode);

    return 0;
}

Corrida de escritorio:
node->prev: 0x6000009782a0
node->data: Juan
node->next: 0x6000009782d0

node->prev: 0x6000009782d0
node->data: Carlos
node->next: 0x6000009782a0

donde se cumple que el 'siguiente' de el ultimo nodo es la direccion de el 'anterior' del primer nodo.

pero al momento de ponerlo en una funcion, todo truena como siempre en los metodos no soy tan habilidoso.

Código:
struct node* addEnd(struct node* head, string data)
{
    struct node* newnode = makeNode();
    if(head == NULL)
    {
        head = newnode;
        return newnode;
    }
    else
    {
        struct node* temp = head->next;
        newnode->next = head;
        head->next = newnode;
        newnode->prev = head->next;
        return head;
    }
}

algun alma bendita que me pueda ayudar porfavor :( llevo como dos semanas intentado que funcione, pero lo maximo que he logrado es sin usar funciones.

tambien intente lo siguiente en el metodo de addEnd():

Código:
temp = head;
while(temp->next !=head)
{
temp = temp->next;
}
2  Programación / Programación C/C++ / ¿Como guardar literalmente un string en una lista doblemente enlazada? en: 23 Marzo 2022, 04:26 am
Tengo un archivo grupo.dat similar a lo siguiente

|        i        |        Correo                       |  Nombre                            |
|-------------+------------------------------+--------------------------------|
|        0        |       a@mail.mx                |  a                                      |
|        1        |       b@mail.mx                |  b                                      |
|        2        |       c@mail.mx                |  c                                       |

Donde extraigo cada linea y la guardo en un arreglo de strings

Código:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#define path "grupo.dat"

using namespace std;

typedef struct key_value
{
    char index[50];
    char mail[50];
    char firstname[50];
    char lastname1[50];
    char lastname2[50];
} student;

struct node {
    string data;
    struct node *next;
    struct node *prev;
    int size;
};

int main()
{
    FILE *file = fopen(path, "r");
    if(!file) exit(1);

    char buffer[114];
    int row_count = 0;
    int field_count = 0;

    student students[58];
    string toostudents[28];
    int i = 0;
    while(fgets(buffer, sizeof(buffer), file))
    {
        field_count = 0;
        row_count++;
        if(row_count == 1) continue;

        char *field = strtok(buffer, "|");
        while(field)
        {
            if(field_count == 0) strcpy(students[i].index, field);
            if(field_count == 1) strcpy(students[i].mail, field);
            if(field_count == 2) strcpy(students[i].lastname1, field);
            if(field_count == 3) strcpy(students[i].lastname2, field);
            if(field_count == 4) strcpy(students[i].firstname, field);

            field = strtok(NULL, "|");
            field_count++;
        }
        i++;
    }
    fclose(file);

    int j = 0;
    for(int i = 3; i < 58; i++)
    {
        if(i > 2 && i%2!=0)
        {
            if(j > 28) break;
            toostudents[j] = string(students[i].lastname1) + students[i].lastname2 + students[i].firstname;
            j++;
        }
    }

    string aux = toostudents[0];
    cout << aux << endl;
    struct node* list = NULL;
    list->prev = NULL;
    list->data = aux;
    list->next = NULL;
    list->size = 1;

    cout << "Data: " << list->data << endl;

    return 0;
}

Yo me lo imagino como <-[prev, data, next]->, donde para cada data, le pertenece un reglon asociado. Pero al momento de correr mi programa me da un error, alguien que me pueda ayudar a solucionarlo o optimizar lo que ya tengo hecho. Por su ayuda gracias.

O tambien si pudiera guardar la estructura student como :

Código:
struct node {
    student data;
    struct node *next;
    struct node *prev;
    int size;
};
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines