0

Background

Creating a dice game where the dice roll value should be stored in a Linked List.

Question

How should an implementation of Linked List be completed in C++? '

Example (What I have tried using struct instead of class)

   #include <time.h>
   #include <stdlib.h>
   #include <stdio.h>
   #include <iostream>

   struct Score {
     int d1;
     int d2;
     int total;
     int totals[13];
     int value;
     struct Score * next;
   }
   
   score_dice1, score_dice2, score_total, score_totals;

   struct Score * ordered_insert(struct Score * , struct Score * );

   int dice = 2;

   void Randomize() {
     srand((unsigned) time(NULL));
   }

   int Random(int Max) {
     return (rand() % Max) + 1;
   }

   int main(int argc, char * argv[]) {

     struct Score * myList = NULL;

     if (argc == 2) {

       int dice_rolls;
       dice_rolls = atoi(argv[1]);
       Randomize();
       for (dice = 2; dice <= 12; dice++)
         score_totals.totals[dice] = 0;
       for (dice = 0; dice < dice_rolls; dice++) {
         score_dice1.d1 = Random(6);
         score_dice2.d2 = Random(6);
         score_total.total = score_dice1.d1 + score_dice2.d2;

         score_totals.totals[score_total.total]++;
       }
       for (dice = 1; dice <= 13; dice++) {

         printf("%i %i\n\r", dice, score_totals.totals[dice]);
       }
     } else {

       std::cout << "How many times should we roll the dice?" << '\n' <<
         "One number please" << '\n';
     }
     return 0;
   }
wuno
  • 9,547
  • 19
  • 96
  • 180
  • Be very careful, when you declare the array of totals[13] it is only valid to references totals[0] through totals[12]. If you want to reference totals[13], then simply declare the array totals[13+1]. – ChuckCottrill Oct 02 '14 at 20:33
  • atoi(argv[1]) could return a value <= 0, and you probably wanted your loop to allow that many dice rolls (rather than 12). – ChuckCottrill Oct 02 '14 at 20:35
  • You probably want to use the STL, and include the std::list class. You should look at the methods for that class - here is a reference, http://www.cplusplus.com/reference/list/list/ – ChuckCottrill Oct 02 '14 at 20:37
  • Are you going to even *attempt* writing `ordered_insert()` ? And decide whether you're using `printf` or `std::cout` and stick with it (unless its `printf`, in which case stop that and change it to `std::cout`) – WhozCraig Oct 02 '14 at 20:58
  • So update your question with "This is what I've tried" and post what you attempted and felt had the best fighting chance at succeeding, whether it works or not. As it stands now, this isn't even a "Why doesn't this code work?" question; its a "This code doesn't exist; write it for me" request, which is not how SO works. – WhozCraig Oct 02 '14 at 21:03

2 Answers2

0

You probably want to use the STL, and include the std::list class. You should look at the methods for that class. I will show you how using the std::vector class.

Suppose you want to place the following into the list,

struct roll_t {
    int dice1; //value of dice1
    int dice2; //value of dice2
    int total; //total of dice1+dice2
    roll_t() dice1(0), dice2(0), total(0) { }
    roll_t(int d1, int d2) : dice1(d1), dice2(d2), total(d1+d2) { };
};

Here is a C++ list reference

Here is an example with explanation

But, let me also add an example here,

#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
    vector<roll_t> dv;
    roll_t rolls[13+1];

    //suppose you have initialized totals[] here...
    for( ndx=1; ndx<=12; ++ndx ) {
        rolls[ndx] = roll_t.new(random(6),random(6));
    }

    //move totals to vector (convert to list left as exercise for poster)
    int ndx;
    for( ndx=1; ndx<=12; ++ndx ) {
        dv.push_back(rolls[ndx]);
    }

    //traverse vector (convert to list, you will need iterator)
    cout << "Loop by index:" << endl;
    for(ndx=0; ndx < dv.size(); dv++)
    {
        cout << "["<<ndx<<"]"
             << dv[ndx].dice1 <<','<< dv[ndx].dice2 <<','<< dv[ndx].total << endl;
    }

}

The above uses the STL, but maybe this question is for a class? And you need to roll your own linked list? I have linked to some stackoverflow answers,

I built one of those,

Trying to make linkedlist in C

Here is a description of single-linked list,

Singly-list insert to end of list

Here is a basic C++ dice game,

Basic C++ Dice game

Here is an explanation of how to sort a linked list by moving pointers,

Trying to Sort a Linked List only by Manipulating Pointers

Those should help.

Community
  • 1
  • 1
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42
  • Here is an example of what i trying to do http://stackoverflow.com/questions/4825030/c-add-to-linked-list-in-sorted-order – wuno Oct 02 '14 at 20:50
0

Answer

Use a class to create the Linked List logic.

Example

linklist.h

#pragma once

#ifndef LINKLIST_H
#define LINKLIST_H

#include <iostream>

using namespace std;

class linklist {
  private:
    struct node {
      int data;
      node * link;
    }* p;

  public:
    linklist();
    void append(int num);
    void add_as_first(int num);
    void addafter(int c, int num);
    void del(int num);
    void display();
    int count();
    ~linklist();
};

linklist::linklist() {
  p = NULL;
}

void linklist::append(int num) {
  node * q, * t;
  if (p == NULL) {
    p = new node;
    p -> data = num;
    p -> link = NULL;
  } else {
    q = p;
    while (q -> link != NULL)
      q = q -> link;
    t = new node;
    t -> data = num;
    t -> link = NULL;
    q -> link = t;
  }
}

void linklist::add_as_first(int num) {
  node * q;
  q = new node;
  q -> data = num;
  q -> link = p;
  p = q;
}

void linklist::addafter(int c, int num) {

  node * q, * t;
  int i;
  for (i = 0, q = p; i < c; i++) {
    q = q -> link;
    if (q == NULL) {
      cout << "\nThere are less than " << c << " elements.";
      return;
    }
  }
  t = new node;
  t -> data = num;
  t -> link = q -> link;
  q -> link = t;
}

void linklist::del(int num) {
  node * q, * r;
  q = p;
  if (q -> data == num) {
    p = q -> link;
    delete q;

    return;

  }

  r = q;
  while (q != NULL) {
    if (q -> data == num)

    {
      r -> link = q -> link;
      delete q;
      return;
    }
    r = q;
    q = q -> link;
  }
  cout << "\nElement " << num << " not Found.";
}

void linklist::display() {
  node * q;
  cout << endl;
  for (q = p; q != NULL; q = q -> link)
    cout << endl << q -> data;
}

int linklist::count() {
  node * q;
  int c = 0;
  for (q = p; q != NULL; q = q -> link)
    c++;
  return c;
}

linklist::~linklist() {
  node * q;
  if (p == NULL)
    return;

  while (p != NULL) {
    q = p -> link;
    delete p;
    p = q;
  }
}

#endif

main.cc

  #include <time.h>
  #include <stdlib.h>
  #include <stdio.h>
  #include <iostream>
  #include <list>
  #include <vector>
  #include "linklist.h"

  struct score {
    int d1;
    int d2;
    int total;
    int totals[13];
  }
score_dice1, score_dice2, score_total, score_totals;

int dice = 2;

void Randomize() {
  srand((unsigned) time(NULL));
}

int Random(int Max) {
  return (rand() % Max) + 1;
}

int main(int argc, char * argv[]) {

  linklist ll;

  if (argc == 2) {

    int dice_rolls;
    dice_rolls = atoi(argv[1]);
    Randomize();
    for (dice = 2; dice <= 12; dice++)
      score_totals.totals[dice] = 0;
    for (dice = 0; dice < dice_rolls; dice++) {
      score_dice1.d1 = Random(6);
      score_dice2.d2 = Random(6);
      score_total.total = score_dice1.d1 + score_dice2.d2;

      score_totals.totals[score_total.total]++;
    }
    for (dice = 1; dice <= 13; dice++) {

      ll.append(score_totals.totals[dice]);

      std::cout << ll.count() << '\n';

      ll.display();
    }
  } else {

    std::cout << "How many times should we roll the dice?" << '\n' <<
      "One number please" << '\n';
  }
  return 0;
}
wuno
  • 9,547
  • 19
  • 96
  • 180