简体   繁体   中英

Inserting Values of Array to Ordered Linked List C++

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;
   }

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM