将数组的值插入到有序链表C++中

将数组的值插入到有序链表C++中,c++,C++,背景 创建一个骰子游戏,其中骰子卷值应存储在链接列表中 问题: 如何在C++中完成链表的实现?p> 示例我尝试使用struct而不是class 您可能希望使用STL,并包括std::list类。您应该查看该类的方法。我将向您展示如何使用std::vector类 假设要将以下内容放入列表中 struct roll_t { int dice1; //value of dice1 int dice2; //value of dice2 int total; //total of

背景

创建一个骰子游戏,其中骰子卷值应存储在链接列表中

问题:

如何在C++中完成链表的实现?p> 示例我尝试使用struct而不是class


您可能希望使用STL,并包括std::list类。您应该查看该类的方法。我将向您展示如何使用std::vector类

假设要将以下内容放入列表中

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) { };
};
但是,让我在这里再加一个例子

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

}
上面使用了STL,但这个问题可能是针对一个类的?你需要滚动你自己的链表吗?我已经链接到一些答案

我造了一个

下面是对单链表的描述

这是一个基本的C++骰子游戏,

下面解释如何通过移动指针对链表进行排序

这些应该会有帮助。

答案

使用类创建链表逻辑

范例

链接列表.h

main.cc


非常小心,当您声明总计[13]数组时,它仅对从总计[0]到总计[12]的引用有效。如果要引用总计[13],则只需声明数组总计[13+1]。atoiargv[1]可能会返回一个您可能希望使用STL的值,并包含std::list类。您应该查看该类的方法-这里是一个引用,您是否打算尝试编写有序的\u insert?决定你是使用printf还是std::cout,并坚持使用它,除非它是printf,在这种情况下,停止使用它并将其更改为std::coutSo,以更新你的问题,这是我尝试过的,并发布你尝试过并认为成功的最佳机会,无论它是否有效。就目前而言,这甚至不是一个问题,为什么这段代码不起作用?问题这是一个不存在的代码;为我的请求写下它,这不是它的工作原理。下面是我试图做的一个例子
#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;
    }

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