1

我有这个模板链接列表类。当我添加五个元素时,“大小”数据成员说有五个元素,但是当我使用“getAt()”函数时,我似乎无法访问第 5 个节点。它给了我 R6010 错误(要求中止)。我似乎找不到 getAt() 函数中的逻辑问题。也许它实际上并没有添加第五个?不过,不要这么想。以前从未见过此错误。

//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");

它是基于 0 而不是基于 1。

更合适的是:

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 回答