双指针链表实现与指针C ++ [英] Doubly Linked List Implementation with Pointers C++

查看:137
本文介绍了双指针链表实现与指针C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在教自己的C ++,并试图在C ++中使用部分完成的指针实现一个双向链表。我知道,代码当前未能处理悬挂节点或输出错误,我将在下面实施。但是,代码应该至少能够构造一个列表对象并向其添加元素。目前,我得到一个错误,当我试图调用列表的构造函数,这说明我要求从LinkedList *转换为非标量类型LinkedList。为什么我的列表被声明为一个指针?任何帮助将非常感谢,谢谢!

  LinkedList.h 
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

struct dataElement {
int key;
int id;
};

struct Node
{
dataElement data;
Node * next;
Node * prev;
};


class LinkedList
{
public:
/ **默认构造函数* /
LinkedList();
/ **默认析构函数* /
virtual〜LinkedList();
void addAtFront(int newElement);
void addAtBack(int newElement);
int removeTop();
int removeBottom();
int getTop();
int getBottom();
int findKey(int keyToFind);
protected:
private:
Node * head;
Node * tail;
int size;
};

#endif // LINKEDLIST_H


LinkedList.cpp
#includeLinkedList.h
#include< iostream>
#include< stdlib.h>


LinkedList :: LinkedList()
{
size = 0;
}

LinkedList ::〜LinkedList()
{
// dtor
}

void LinkedList :: addAtFront (int newElement)
{
if(size == 0)
{
节点temp;
temp.data.id = newElement;
temp.data.key = 0;
head =& temp;
tail =& temp;
++ size;
}
else
{
节点temp;
temp.data.id = newElement;
temp.data.key = size;
temp.next = head;
head-> prev =& temp;
head =& temp;
++ size;
}
}

void LinkedList :: addAtBack(int newElement)
{
if(size == 0)
{
Node temp;
temp.data.id = newElement;
temp.data.key = 0;
head =& temp;
tail =& temp;
++ size;
}
else
{
节点temp;
temp.data.id = newElement;
temp.data.key = 0;
tail-> next =& temp;
temp.prev = tail;
tail =& temp;
++ size;
}
}

LinkedListTest.cpp
#includeLinkedListTest.h
#includeLinkedList.h

int main()
{
LinkedList list = new LinkedList();
list.addAtFront(0);
}


解决方案

有一个LinkedList列表声明不作为一个指针,你分配一个新的LinkedList()类型 LinkedList * (而不是 LinkedList )。它应该是:

  LinkedList * list = new LinkedList(); //我声明一个指向列表的指针
list-> addAtFront(0); //我调用指向对象的指针的方法

  LinkedList列表; 
list.addAtFront(0);

它们是两种不同的类型,分配在两个不同的存储器中,这很重要,请继续阅读。 / p>

我看到的更重要的是,当你使用动态分配的内存时,你应该考虑实际分配在堆对象上,这些对象应该持续声明它们的范围。 p>

更具体地说,这是:

  {
Node temp;
..
head =& temp;
..
}

这会导致问题,因为 temp 声明为堆栈上的自动存储,这意味着一旦你获得了它的地址并将其分配给 head 或任何内容,一旦退出范围,该地址将不再有效。您应该在堆上分配:

 节点temp = new Node(value,id); 
head = temp;
tail = temp;
++ size;请注意,这需要你自己从堆中清除内存,当

> Node
不再需要了。


I am currently teaching myself C++ and am attempting to implement a doubly-linked list in C++ using pointers which is partially complete. I am aware that the code currently fails to deal with dangling nodes or output errors, both of which I will implement next. However, the code should atleast be able to construct a list object and add elements to it. Currently, I am getting an error when I attempt to call a constructor for the list, which says that I am requesting a conversion from LinkedList* to non scalar type LinkedList. Why is my list being declared as a pointer? Any help would be much appreciated, thank you!

LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

struct dataElement {
  int key;
  int id;
};

struct Node
{
    dataElement data;
    Node* next;
    Node* prev;
};


class LinkedList
{
public:
    /** Default constructor */
    LinkedList();
    /** Default destructor */
    virtual ~LinkedList();
    void addAtFront(int newElement);
    void addAtBack(int newElement);
    int removeTop();
    int removeBottom();
    int getTop();
    int getBottom();
    int findKey(int keyToFind);
protected:
private:
    Node* head;
    Node* tail;
    int size;
};

#endif // LINKEDLIST_H


LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
#include <stdlib.h>


LinkedList::LinkedList()
{
size = 0;
}

LinkedList::~LinkedList()
{
//dtor
}

void LinkedList::addAtFront(int newElement)
{
if (size == 0)
{
    Node temp;
    temp.data.id = newElement;
    temp.data.key = 0;
    head = &temp;
    tail = &temp;
    ++size;
}
else
{
    Node temp;
    temp.data.id = newElement;
    temp.data.key = size;
    temp.next = head;
    head->prev = &temp;
    head = &temp;
    ++size;
}
}

void LinkedList::addAtBack(int newElement)
{
if (size == 0)
{
    Node temp;
    temp.data.id = newElement;
    temp.data.key = 0;
    head = &temp;
    tail = &temp;
    ++size;
}
else
{
    Node temp;
    temp.data.id = newElement;
    temp.data.key = 0;
    tail->next = &temp;
    temp.prev = tail;
    tail = &temp;
    ++size;
}
}

LinkedListTest.cpp
#include "LinkedListTest.h"
#include "LinkedList.h"

int main()
{
LinkedList list = new LinkedList();
list.addAtFront(0);
}

解决方案

The error means that somewhere you have a LinkedList list declared not as a pointer, to which you assign a new LinkedList() which is of type LinkedList* (and not LinkedList). It should be:

LinkedList* list = new LinkedList(); // I declare a pointer to a list
list->addAtFront(0); // I call a method on a pointer to an object

or

LinkedList list;
list.addAtFront(0);

They are two different types which are allocated in two different storages and this is important, keep reading.

What I see more importantly is that when you use dynamically allocated memory you should take case of actually allocate on heap objects that should persist the scope in which they are declared.

More specifically, this:

{
  Node temp;
  ..
  head = &temp;
  ..
}

This will cause problems because temp is declared as automatic storage on stack, which means that once you obtained its address and assign it to head or tail or whatever, that address won't be valid anymore once the scope exited. You should allocate it on heap:

Node temp = new Node(value, id);
head = temp;
tail = temp;
++size;

Mind that this requires that you clean up memory by yourself from the heap when the Node is not needed anymore.

这篇关于双指针链表实现与指针C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆