Okay. This weeks 'problem' is that I need to simulate a game. I have to make two singly linked lists which will contain a name of player, his attack (double/float) number and his defence number and so on for as many players as the user inputs. The point of the game is to try to break the other 'chain'. You do that by running into 'enemies' chain and trying to break it, you break it if your attack is greater then defence of two players that you are trying to separete. If you separate them you can pick one of them to join your chain, but if you don't break it you stay in place where you tried to break chain.

So, I need to program two lists with all those atributes and I don't have an issue with creating a

typedef struct player { 
    int atc,def;
    struct player *next;
} Player;

but how do I add the name of the payers ? Can I so it with

while((string = getchar()) != '\n'){
   name[i++] = string ; 
   }

? And then pass the string -name- to first element of linked list ?
If you don't get something that I wrote, please don't hesitate to ask!(sorry for bad spelling)

Why not just store the name in the player struct itself? Something like:

typedef struct plist_ {
    int atc, def;
    char *name;
    struct plist_ *next;
} PlayerList;

Can I use same structure for 2 linked lists, like for 2 different groups of players ?

Of course. The question you need to ask yourself is if that is a maintenance nightmare (probably, yes).

What you might want to do is maintain two separate lists (one for each type) and just use whichever is necessary at the time. Or, have an array of player lists that contains all the lists of players you will be supporting.

Design is an important part of writing code; you want to make sure you do it correct to avoid having to revisit the same problem many, many times.

I have new issuse now. How do I compare elements from lists ?
I need to compare attack of one and addition of TWO chosen defences.

Comparison is easy: take the items in the node of the list you have and evaluate them. Something like:

if (node->atk < (node2->def1 + node2->def2)) {
   // too small
} else {
   // big enough
}

The trick is knowning when you have the correct nodes in the list to compare. That is probably based on a name or type of something (perhaps an ID?).

This is main

#include "lista.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX 50

void main(){
    Elem *lst1 = NULL,*lst2 = NULL ; 
    int end = 0 ,choise, n; 
    double atk, def;
    char ime[MAX];
    while(!kraj){
        printf("\n1. Add to first list\n"
            "2. Add to second list\n"
            "3. Erase first list\n"
            "4. Erase second list\n"
            "5. Lenght of first list\n"
            "6. Lenght of second list\n"
            "7. Print first list\n"
            "8. Print second list\n"
            "0. End\n"
            );
        scanf("%d" , &choise);
        switch(choise){
        case 1:
            /*printf("Name?\n");
            scanf("%s", ime);*/
            printf("Attack?\n");
            scanf("%lf", &atk);
            printf("Defence?\n");
            scanf("%lf",&def);
            lst1 = na_kraj (lst1,atk,def,ime);
            break;
        case 2:
            /*printf("Name?\n");
            scanf("%s", ime);*/
            printf("Attack?\n");
            scanf("%lf", &atk);
            printf("Defence?\n");
            scanf("%lf",&def);
            lst2 = na_kraj (lst2,atk,def,ime);
            break;
        case 3: brisi (lst1) ; 
            lst1 = NULL;
            break;
        case 4: brisi (lst2) ; 
            lst2 = NULL;
            break;
        case 5: printf("Duzina = %d\n" , duz(lst1));
            break;
        case 6: printf("Duzina = %d\n" , duz(lst2));
            break;
        case 7: printf("First:\n ");
            pisi (lst1);
            putchar('\n');
            break;
        case 8: printf("Second:\n ");
            pisi (lst2);
            putchar('\n');
            break;
        case 0: kraj = 1;
            break;
        default : printf("Nedozvoljen izbor!");
            break;
        }
    }
}

these are all the functions defined(I'm sorry that it's not translated)

#include "lista.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50

// Broj elem

int duz(Elem *lst){
    int n = 0;
    while(lst){
        n++;
        lst = lst->sled;
    }
    return n;
}

// Ispis

void pisi (Elem *lst){
    while(lst){
        printf("\n");
        /*printf("Ime: %s" , lst->ime);
        printf("\n");*/
        printf("Napad: %lf" , lst->atk);
        printf("\n");
        printf("Odbrana: %lf" , lst->def);
        printf("\n");
        lst = lst->sled;
    }
}

// Na kraj

Elem *na_kraj(Elem *lst, double atk, double def, char ime[MAX]){
    Elem *novi = malloc(sizeof(Elem));
    novi->atk = atk;
    novi->def = def;
    //strcpy(novi->ime, ime);
    novi->sled = NULL;
    if(!lst){
        return novi;
    }
    else{
        Elem *tek = lst;
        while(tek->sled){
            tek = tek->sled;
        }
            tek->sled = novi;
            return lst;
        }
}

void brisi(Elem *lst){
    while(lst){
        Elem *stari = lst;
        lst = lst->sled;
        free(stari);
    }
}

and this is my structure dot H

// elements

typedef struct elem{
        char ime[50];
        double atk,def;
        struct elem *sled;
    } Elem;

// number of elements

int duz(Elem *lst);

// Ispis

void pisi(Elem *lst);

// Put elements to  end

Elem *na_kraj(Elem *lst, double atk, double def,char ime[50]);

// delete

void brisi(Elem *lst);

I have issue when I try to put in name of player. As the main problem so I posted the code with name input 'turned off'.

IT WORKS. Now the other part. Comparing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.