问题描述
在您阅读本文之前,我是c ++的新手,感谢您的任何反馈,请不要苛刻:(
所以基本上我是在双向链表中添加元素,而在下面的功能中,我的目的是将其元素添加到单链链表中并返回它,但是我只是返回了指向链表的指针./p>
我只是停留在这个小问题上,
当我返回它时,它给了我一个地址,我在函数中做错了什么,我只能将手指放在上面,其余的代码也可以正常工作.我希望我正确地提出了我的问题,以其他方式询问我.任何帮助都非常感谢.
模板< class Type>SinglyLinkedList< Type>* DoublyLinkedList< Type> :: Function(){SinglyLinkedList< Type> * newList = new SinglyLinkedList< Type>();nodeDLL< Type> * p = head2;//我将双向链表中的值添加到了单链表中而(p!= NULL){newList-> addToHead(p-> value2);p = p-> next2;}//我使用了print函数来确保上面的方法可以按照我想要的方式工作,newList-> print();//但是当我在这里将其退回时,给了我地址吗?返回newList;
作为参考,下面是其余代码:
单链接列表节点的类
模板< class Type>类nodeSLL {上市:nodeSLL *接下来;类型值;nodeSLL(类型v,nodeSLL * n){值= v;下一个= n;}〜nodeSLL(){cout<<节点已损坏"<<恩德尔}};
单链表类
模板< class Type>类SinglyLinkedList {上市:nodeSLL< Type> *头;nodeSLL< Type> *尾部;SingyLinkedList();SinglyLinkedList(SinglyLinkedList< Type>& obj);void addToHead(Type v);void addToTail(Type v);无效print();〜SinglyLinkedList();};
模板< class Type>SinglyLinkedList< Type> :: SinglyLinkedList(const SinglyLinkedList& obj)//复制构造函数{头= obj.head;尾巴= obj.tail;}
template< class Type>SinglyLinkedList< Type> :: SinglyLinkedList(){头=尾= NULL;}
template< class Type>SinglyLinkedList< Type> ::〜SinglyLinkedList(){nodeSLL< Type>* p =头;而(p!= NULL){头=头->下一个;删除p;p =头;}}
打印功能
模板< class Type>void SinglyLinkedList< Type> :: print(){nodeSLL< Type>* t =头;而(t!= NULL){cout<<t->值<<"=>";t = t-> next;}cout<<"NULL"<<恩德尔}
添加到头部功能
模板< class Type>void SinglyLinkedList< Type> ::: addToHead(Type v){nodeLL< Type>* newNode =新的nodeSLL< Type>(v,头);如果(head == NULL){头=尾= newNode;} 别的 {头= newNode;}
双向链表节点
模板< class Type>类nodeDLL {上市:nodeDLL * next2;nodeDLL * prev2;类型value2;nodeDLL< Type>(nodeDLL< Type> * prv,Type v,nodeDLL< Type> * n){值2 = v;next2 = n;prev2 = prv;}〜nodeDLL(){prev2 = next2 = NULL;cout<<节点已损坏"<<恩德尔}};
双向链表类
模板< class Type>类DoublyLinkedList {上市:nodeDLL< Type> * head2;nodeDLL< Type> * tail2;DounlyLinkedList();void addToHead2(Type v);void Clear();〜DoublyLinkedList();};
添加到头双向链接列表
模板< class Type>void DoublyLinkedList< T> ::: addToHead2(Type v){nodeDLL< T> * newNode =新的nodeDLL< Type>(NULL,v,head2);如果(head2 == NULL){head2 = tail2 = newNode;}别的 {head2-> prev2 = newNode;head2 = newNode;}}
主要功能
int main(){SinglyLinkedList< int> * list1;DoublyLinkedList< int>清单;list.addToHead2(55);list.addToHead2(87);list.addToHead2(2);//list1 = list.Function();cout<< lists.Function();返回0;}
它提供了地址,因为那是您在代码中编写的.如果您不需要地址,请不要使用指针.改为这样做
template< class Type>SinglyLinkedList< Type>DoublyLinkedList< Type> :: Function(){SinglyLinkedList< Type>newList;nodeDLL< Type> * p = head2;//我将双向链表中的值添加到了单链表中而(p!= NULL){newList.addToHead(p-> value2);p = p-> next2;}//我使用了print函数来确保上面的方法可以按照我想要的方式工作,newList.print();返回newList;}
但是要实现此目的,您的单链接列表类将需要有效的副本构造函数(因为从函数返回时列表可能会被复制),我看不到有任何证据.
您需要阅读三个规则,然后再进行下一步.
Before you read this, I am new to c++, any feedback is appreciated just don't be harsh please:(
So basically I am adding elements to a doubly linked list and in the function below my aim is to take its elements and add them into my singly linked list and return it, however I am just returning a pointer to the list.
I am just stuck on this little problem,
When I return it, it gives me an address, I am doing something wrong in the function, I just can put my finger on it, the rest of the code works with me fine.I hope I delivered my question correctly, ask me other wise.Any help is very appreciated.
template <class Type>
SinglyLinkedList<Type> *DoublyLinkedList<Type>:: Function() {
SinglyLinkedList<Type>* newList = new SinglyLinkedList<Type>();
nodeDLL<Type>* p = head2;
//I added the values from the doubly linked list into the singly linked list
while (p !=NULL) {
newList->addToHead(p->value2);
p=p->next2;
}
// I used the print function to make sure the above works and it works the way I want,
newList->print();
// but when I return it here it gives me an address?
return newList;
For reference here is the rest of the code:
class for the Singly linked list node
template <class Type>
class nodeSLL {
public:
nodeSLL* next;
Type value;
nodeSLL(Type v, nodeSLL* n) {
value = v;
next = n;
}
~nodeSLL() {
cout << "node destroyed" << endl;
}
};
Singly linked list class
template <class Type>
class SinglyLinkedList {
public:
nodeSLL<Type>* head;
nodeSLL<Type>* tail;
SingyLinkedList();
SinglyLinkedList(SinglyLinkedList<Type> &obj);
void addToHead(Type v);
void addToTail(Type v);
void print();
~SinglyLinkedList();
};
template <class Type>
SinglyLinkedList<Type>::SinglyLinkedList(const SinglyLinkedList &obj) // Copy Constructor
{
head = obj.head;
tail = obj.tail;
}
template <class Type>
SinglyLinkedList<Type>:: SinglyLinkedList() {
head = tail = NULL;
}
template <class Type>
SinglyLinkedList<Type>:: ~SinglyLinkedList() {
nodeSLL<Type> *p = head;
while (p != NULL) {
head = head->next;
delete p;
p = head;
}
}
print function
template <class Type>
void SinglyLinkedList<Type>:: print() {
nodeSLL<Type> * t = head;
while (t != NULL) {
cout << t->value << " => ";
t = t->next;
}
cout << " NULL"<< endl;
}
add to head function
template <class Type>
void SinglyLinkedList<Type>:: addToHead(Type v) {
nodeLL<Type> *newNode = new nodeSLL<Type>(v, head);
if (head == NULL) {
head = tail = newNode;
} else {
head = newNode;
}
Doubly linked list node
template <class Type>
class nodeDLL {
public:
nodeDLL* next2;
nodeDLL* prev2;
Type value2;
nodeDLL<Type>(nodeDLL<Type>* prv, Type v, nodeDLL<Type>* n) {
value2 = v;
next2 = n;
prev2 = prv;
}
~nodeDLL() {
prev2 = next2 = NULL;
cout << "node destroyed" << endl;
}
};
Doubly linked list class
template <class Type>
class DoublyLinkedList {
public:
nodeDLL<Type>*head2;
nodeDLL<Type>*tail2;
DounlyLinkedList();
void addToHead2(Type v);
void Clear();
~DoublyLinkedList();
};
Add to head doubly linked list
template <class Type>
void DoublyLinkedList<T>:: addToHead2(Type v) {
nodeDLL<T>* newNode = new nodeDLL<Type>(NULL, v, head2);
if (head2 == NULL) {
head2 = tail2 = newNode;
}
else {
head2->prev2 = newNode;
head2 = newNode;
}
}
Main function
int main()
{
SinglyLinkedList<int>* list1;
DoublyLinkedList<int> lists;
lists.addToHead2(55);
lists.addToHead2(87);
lists.addToHead2(2);
//list1 = lists.Function();
cout <<lists.Function();
return 0;
}
It gives the address because that's what you wrote in the code. Don't use pointers if you don't want addresses. Do it like this instead
template <class Type>
SinglyLinkedList<Type> DoublyLinkedList<Type>::Function() {
SinglyLinkedList<Type> newList;
nodeDLL<Type>* p = head2;
//I added the values from the doubly linked list into the singly linked list
while (p !=NULL) {
newList.addToHead(p->value2);
p=p->next2;
}
// I used the print function to make sure the above works and it works the way I want,
newList.print();
return newList;
}
But to make this work your singly linked list class will need a valid copy constructor (because the list might be copied when you return from the function) and I see no evidence that you have that.
You need to read up on the rule of three before you go any further.
这篇关于如何从函数返回链表?为什么当我退还给我一个地址时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!