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

吉姆·麦克马斯特

我有这个模板化的链接列表类。当我添加五个元素时,“大小”数据成员说有五个元素,但是当我使用“ 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;
    }

}
大狼

您正在这里访问越界索引:

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将VS2010与Assimp链接

来自分类Dev

将VS2010与Assimp链接

来自分类Dev

无法从VS2010进行调试

来自分类Dev

VS2010“框架和参考”与“链接器>输入”

来自分类Dev

VS2010中无法解析的外部符号

来自分类Dev

C ++ VS2010中“每个”循环的编译错误

来自分类Dev

VS2010 C ++ MFC隐藏工具栏

来自分类Dev

C#Windows VS2010形式的微积分

来自分类Dev

将VS2013和VS2010与TFS2010一起使用

来自分类Dev

从VS2010升级到VS2013现在我无法发布

来自分类Dev

无法在vs2012中打开vs2010 csproj文件

来自分类Dev

无法在Win7 / VS2010上构建WebKit-r161259

来自分类Dev

VS2010和NuGet,为什么要使用v2.8?如何获得3.5?

来自分类Dev

无法在VS2010中添加新团队项目。错误TF249063

来自分类Dev

npm无法说“找不到VS2010的构建工具”

来自分类Dev

VS2010源文件“ Project \ SharedAssemblyInfo.cs”无法打开(未指定错误)

来自分类Dev

“未找到类型'DatePicker'”,似乎无法解决VS2010中的此错误

来自分类Dev

Mersenne Twister随机生成器-两个并行运行的VS2010

来自分类Dev

C ++ VS2010调试器在超出循环范围的循环变量上表现异常

来自分类Dev

为什么在VS2010 C#中不能使用ManagementObjectSearcher

来自分类Dev

是否可以在所有平台上使用vs2010 c ++并使用Qt?

来自分类Dev

为什么在VS2010 C#中不能使用ManagementObjectSearcher

来自分类Dev

如何在VS2010 C#项目上使用最旧的DLL

来自分类Dev

VS2013与VS2010不同,我无法从Windows应用程序访问网络驱动器

来自分类Dev

VS2010:C#:无法从VS工具箱中找到输入文本组件(以键入文本)

来自分类Dev

是否可以将 VS2019 中内置的共享静态库与 VS2010 项目链接起来?

来自分类Dev

dll在VS2010项目中工作正常,但在VS6应用程序中提供访问冲突

来自分类Dev

VS2010 C ++ / CLI项目始终认为VS2012 C#项目已过时

来自分类Dev

VS2010和VS2012之间的二进制C ++库兼容性?

Related 相关文章

  1. 1

    将VS2010与Assimp链接

  2. 2

    将VS2010与Assimp链接

  3. 3

    无法从VS2010进行调试

  4. 4

    VS2010“框架和参考”与“链接器>输入”

  5. 5

    VS2010中无法解析的外部符号

  6. 6

    C ++ VS2010中“每个”循环的编译错误

  7. 7

    VS2010 C ++ MFC隐藏工具栏

  8. 8

    C#Windows VS2010形式的微积分

  9. 9

    将VS2013和VS2010与TFS2010一起使用

  10. 10

    从VS2010升级到VS2013现在我无法发布

  11. 11

    无法在vs2012中打开vs2010 csproj文件

  12. 12

    无法在Win7 / VS2010上构建WebKit-r161259

  13. 13

    VS2010和NuGet,为什么要使用v2.8?如何获得3.5?

  14. 14

    无法在VS2010中添加新团队项目。错误TF249063

  15. 15

    npm无法说“找不到VS2010的构建工具”

  16. 16

    VS2010源文件“ Project \ SharedAssemblyInfo.cs”无法打开(未指定错误)

  17. 17

    “未找到类型'DatePicker'”,似乎无法解决VS2010中的此错误

  18. 18

    Mersenne Twister随机生成器-两个并行运行的VS2010

  19. 19

    C ++ VS2010调试器在超出循环范围的循环变量上表现异常

  20. 20

    为什么在VS2010 C#中不能使用ManagementObjectSearcher

  21. 21

    是否可以在所有平台上使用vs2010 c ++并使用Qt?

  22. 22

    为什么在VS2010 C#中不能使用ManagementObjectSearcher

  23. 23

    如何在VS2010 C#项目上使用最旧的DLL

  24. 24

    VS2013与VS2010不同,我无法从Windows应用程序访问网络驱动器

  25. 25

    VS2010:C#:无法从VS工具箱中找到输入文本组件(以键入文本)

  26. 26

    是否可以将 VS2019 中内置的共享静态库与 VS2010 项目链接起来?

  27. 27

    dll在VS2010项目中工作正常,但在VS6应用程序中提供访问冲突

  28. 28

    VS2010 C ++ / CLI项目始终认为VS2012 C#项目已过时

  29. 29

    VS2010和VS2012之间的二进制C ++库兼容性?

热门标签

归档