链表:无法访问最后一个节点,获得R6010(C ++,VS2010)

时间:2013-11-15 02:20:43

标签: c++ visual-studio-2010

我有这个模板化的Linked List类。当我添加五个元素时,'size'数据成员说有五个元素但是当我使用'getAt()'函数时,我似乎无法访问第五个节点。它给了我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;
    }

}

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

C / C ++使用零索引。因此for循环应该是

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