[ Foro de C++ ]

Como enlazar nodos de una lista enlazada??

16-Jun-2019 23:27
Pablo Alarcon
1 Respuestas

Hola y de ante mano muchas gracias por la ayuda.
Tengo una lista de trabajadores, cada uno con sus datos y cada trabajador puede trabajar horas extras.
Aqui hice un dibujo de como es el esquema para los punteros
https://i.imgur.com/LUPZfhC.jpg

Como puedo modificar el codigo para que las horas extras puedan ser agregadas correctamente como en el dibujo a cada empleado??

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

struct worker {
int num_worker;
char name[20];
char job[12];
int base_salary;
struct worker *next, *he_next;
};

struct extras_hours{
int Code;
int num_worker;
int day;
int num_exh;
int pay_per_hour;
struct extras_hours *he_next
};

struct worker *first, *last;
struct extra_hours *first_hour, *last_hour

void add_new_worker() {
struct worker *new;

new = (struct worker *) malloc (sizeof(struct worker));
printf("num_worker: ");
scanf("%d",&new->num_worker); //must verify  if worker exist
printf("Name: ");
gets(new->name);
printf("job: ");
gets(new->job);
printf("Base Salary: ");
scanf("%d",&new->base_salary);

new->next = NULL;

if (first==NULL) {
first = new;
last = new;
}
else {
last->next = new;
last = new;
}
}
int Code;
int num_worker;
int day;
int num_exh;
int pay_per_hour;
void add_extra_hours(){
struct extra_hours *new_h;

new_h = (struct extras_hours *) malloc (sizeof(struct extras_hours));

printf("New Extra Hours");
printf("\n new extra hours for worker number:\n");
printf("num_worker: ");
scanf("%d",&new_h->num_worker); //must verify that worker exist
printf("Code: ");
scanf("%d",&new_h->code);
printf("day: "); //must verify is worker already worked extra this day
scanf("%d",&new_h->day);  // must be number between 1 and 30
printf("Number of extra hours: ");
scanf("%d",&new_h->num_exh);
printf("Extra Payment per hour: ");
// (base_salary/180)*1.5, its the base salary of worker. each worker can have different base salary
scanf("%d",&new_h->pay_per_hour);

new_h->he_next = NULL;

if (first_hour==NULL) {
printf( "Primer elemento\n");
first_hour = new_h;
last_hour = new_h;
}
else {
last->he_next = new_h;
last_hour = new_h;
}
}


17-Jun-2019 00:04
Pablo Alarcon

new es una palabra reservada en C, cambio por new_h
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

struct worker {
   int num_worker;
   char name[20];
   char job[12];
   int base_salary;
   struct worker *next, *he_next;
};

struct extras_hours{
   int code;
   int num_worker;
   int day;
   int num_exh;
   int pay_per_hour;
   struct extras_hours *he_next
};

struct worker *first, *last;
struct extra_hours *first_hour, *last_hour

void add_new_worker() {
   struct worker *new_w;

   new_w = (struct worker *) malloc (sizeof(struct worker));
   printf("num_worker: ");
   scanf("%d",&new->num_worker); //must verify  if worker exist
   printf("Name: ");
   gets(new_w->name);
   printf("job: ");
   gets(new_w->job);
   printf("Base Salary: ");
   scanf("%d",&new_w->base_salary);

   new_w->next = NULL;

   if (first==NULL) {
       first = new_w;
       last = new_w;
   }
   else {
       last->next = new_w;
       last = new_w;
   }
}

void add_extra_hours(){
   struct extra_hours *new_h;

   new_h = (struct extras_hours *) malloc (sizeof(struct extras_hours));

   printf("New Extra Hours");
   printf("\n new extra hours for worker number:\n");
   printf("num_worker: ");
   scanf("%d",&new_h->num_worker); //must verify that worker exist
   printf("Code: ");
   scanf("%d",&new_h->code);
   printf("day: "); //must verify is worker already worked extra this day
   scanf("%d",&new_h->day);  // must be number between 1 and 30
   printf("Number of extra hours: ");
   scanf("%d",&new_h->num_exh);
   printf("Extra Payment per hour: ");
   // (base_salary/180)*1.5, its the base salary of worker. each worker can have different base salary
   scanf("%d",&new_h->pay_per_hour);

   new_h->he_next = NULL;

   if (first_hour==NULL) {
       first_hour = new_h;
       last_hour = new_h;
   }
   else {
       last_hour->he_next = new_h;
       last_hour = new_h;
   }
}






(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)