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

时间:2014-10-02 20:26:25

标签: c++

我有一个程序将所有骰子卷添加到一起,然后将它们存储在一个结构中保存的数组中。

我在数组中的值是1到13。

我试图将该数组的值推送到有序链表但无法完成此操作。

我试图遍历我当前的数组score_totals.totals并将它们添加到列表中,但我认为我对这应该如何工作感到困惑。

请告诉我如何做到这一点。

#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 << "Dont be dumb..." << '\n'
            << "How many times should we roll the dice?" << '\n'
            << "One number please" << '\n';
        }
            return 0;
}

2 个答案:

答案 0 :(得分:0)

您可能想要使用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) { };
};

Here is a C++ list reference

Here is an example with explanation

但是,我还要在这里添加一个例子,

#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,但也许这个问题适用于一个类?你需要推出自己的链表吗?我已链接到一些stackoverflow答案,

我建了其中一个,

Trying to make linkedlist in C

以下是单链表的说明,

Singly-list insert to end of list

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

Basic C++ Dice game

以下是如何通过移动指针对链接列表进行排序的说明

Trying to Sort a Linked List only by Manipulating Pointers

那些应该有所帮助。

答案 1 :(得分:0)

您可以创建一个类来处理这样的事情,对于您的代码,这就是您要做的事情,

创建一个名为linkedlist的类并将此代码应用于它,

#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


/*

 FUNCTION DEFINITIONS

 linklist();                       // CONSTRUCTOR
 void append( int num );           // ADD AT END OF LIST
 void add_as_first( int num );     // ADD TO BEGINNING OF LIST
 void addafter( int c, int num );  // ADD DATA num AFTER POSTION c
 void del( int num );              // DELETE DATA num
 void display();                   // DISPLAY LINKED LIST
 int count();                      // NUMBER OF ITEMS IN LIST
 ~linklist();                      // DESTRUCTOR


 */

然后让你的main.cpp文件看起来像这样,

#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 << "Dont be dumb..." << '\n'
            << "How many times should we roll the dice?" << '\n'
            << "One number please" << '\n';
        }
            return 0;
}