1

このテンプレート化されたリンク リスト クラスがあります。5 つの要素を追加すると、「size」データ メンバーは 5 つの要素があると表示しますが、「getAt()」関数を使用すると、5 番目のノードにアクセスできないようです。R6010 エラー (中止を要求) が表示されます。getAt() 関数のロジックに問題があるようには見えません。たぶん、実際には5番目のものを追加していませんか?しかし、そうは思わないでください。このエラーはこれまでに見たことがありません。

//LinkedList.h
#include <iostream>
using namespace std;


/*
class: Node
description: holds an T in 'info' and a Node pointer in
             'next'. the building block of a linked list.
*/
template <typename T>
class Node{
public:
    T info;//holds the info value
    Node<T>* next;//holds a pointer to the next node in the list

    Node(T val);//constructor
};

/*  
function: constructor
param(s): T (value)
pre:
post: instantiates node, sets "next" pointer to NULL
exception(s):
return:
*/
template <typename T>
Node<T>::Node(T val){//constructor, accepts a value to hold the info
    info = val;
    next = NULL;
}//end Node class


///////////////////////////////////////////////
///////////////////////////////////////////////


/*
class: LinkedList
description: a list of linked T-type nodes. provides methods to add
             node and retrieve a node at a given location in the list. 
*/
template <typename T>
class LinkedList{
public:
    Node<T>* list;//points to the first node of the list
    int size;//the number of nodes in the list

    LinkedList();//default constructor
    ~LinkedList();//destructor
    void add(T addArg);//add a node
    T getAt(int getArg);//get a node at a position 'getArg'
    void updateAt(int getArg, T newData);
};//end LinkedList class

/*  
function: linked list default constructor
param(s): 
pre: 
post: list is instantiated, size is set to 0
exception(s): 
return: 
*/
template <typename T>
LinkedList<T>::LinkedList(){
    list = NULL;
    size = 0;
}

/*  
function: linked list destructor
param(s):
pre:
post: all nodes and pointer to the first node deleted
exception(s):
return:
*/
template <typename T>
LinkedList<T>::~LinkedList(){
    while(list != NULL){
        Node<T>* temp = list->next;
        delete list;
        list = temp;
    }
}

/*  
function: add
param(s): T (addArg)
pre: list is instantiated
post: new node has been added to the node, link of previous node
      has been set to the new node. if no nodes in list before
      adding, link of the new node is NULL
exception(s): 
return: void
*/
template <typename T>
void LinkedList<T>::add(T addArg){
    if(size == 0){//if the list is empty
        Node<T>* next = new Node<T>(addArg);//create a new node
        list = next;//and set the 'list' pointer to it
        size++;//increment size of list
    }
    else if(size > 0){//if there's at least one node in the list
        Node<T>* temp = list;//create new node 
        while(temp->next != NULL){//traverse list to last node
            temp = temp->next;
        }
        temp->next = new Node<T>(addArg);//set the link of the last
                                      //node to a new node of value
                                      //addArg
        size++;//increment size of the list
    }
    else//throw exception, the list has a negative size value
        throw string("Size is negative for some reason...\n");
}

/*  
function: getAt
param(s): getArg(int, the position of the node to retrieve)
pre: list isn't empty
post: 
exception(s): throw out of bounds exception of getArg is negative 
              or out of bounds
return: value of the node at position 'getArg'
*/
template <typename T>
T LinkedList<T>::getAt(int getArg){
    if((getArg>=size)||(getArg<0))//getArg out of bounds
        throw string("Out of bounds 'get' argument");
    else{//getArg is acceptable
        Node<T>* temp = list;//create a temp pointer so as to not lose
                          // the pointer to the list
        for(int i = 1; i < getArg; i++){//traverse list until the
            temp = temp->next;          //sought-after node is found
        }
        return temp->info;//return the value of the sought-after node
    }
}

/*  
function: updateAt
param(s): getArg(int, the position of the node to retrieve)
          newData(T, new data)
pre: list isn't empty
post: info at node getArg is changed to 
exception(s): throw out of bounds exception of getArg is negative 
              or out of bounds
return: value of the node at position 'getArg'
*/
template <typename T>
void LinkedList<T>::updateAt(int getArg, T newData){
    if((getArg>=size)||(getArg<0))//getArg out of bounds
        throw string("Out of bounds 'get' argument");
    else{//getArg is acceptable
        Node<T>* temp = list;//create a temp pointer so as to not lose
                          // the pointer to the list
        for(int j = 1; j < getArg; j++){//traverse list until the
            temp = temp->next;          //sought-after node is found
        }
        temp->info = newData;//return the value of the sought-after node
    }
}

メインはこちら

//main.cpp

void main(void)
{
    LinkedList<int> mine;
    mine.add(1);
    mine.add(2);
    mine.add(3);
    mine.add(4);
    mine.add(5);
    cout << "Size: " << mine.size << endl;

    int dem;
    for(int i = 1; i<= 5; i++)
    {
        dem = mine.getAt(i);
        cout << "i = " << i << endl << "val = " << dem << endl;
    }

    mine.updateAt(3, 10);
    for(int i = 1; i<= 5; i++)
    {
        dem = mine.getAt(i);
        cout << "i = " << i << endl << "val = " << dem << endl;
    }

}
4

2 に答える 2

2

ここで範囲外のインデックスにアクセスしています。

for(int i = 1; i <= 5; i++)

あなたがあなたを見ればgetAt

T LinkedList<T>::getAt(int getArg){
    if((getArg>=size)||(getArg<0))//getArg out of bounds
        throw string("Out of bounds 'get' argument");

1ベースではなく0ベースです。

より適切なのは次のとおりです。

for(int i = 0; i < 5; ++i)
于 2013-11-15T02:30:12.773 に答える
0

C/C++ はゼロインデックスを使用します。したがって、for ループは

for(int i = 0; i< 5; ++i)
于 2013-11-15T02:32:00.297 に答える