英文:
Linked list in c++ adding node at the end of the list
问题
以下是您提供的代码的翻译:
我正在编写一个将元素存储在链表中的代码,但是当我在列表末尾添加一个元素时,输出始终如下...
[在此输入图像描述](https://i.stack.imgur.com/ApWie.png)
我已经从许多教程中搜索并复制了它们的代码,以查看它是否在我的代码上运行,但是它不起作用...
#include <iostream>
#define tab '\t'
using std::endl;
struct node {
int data;
node* next;
};
class Linkedlist {
private:
node* head = new node;
public:
Linkedlist(){
head->next = NULL;
}
void addStart(int val){
node* temp = new node;
temp->data = val;
temp->next = head;
head = temp;
}
void addEnd(int val){
node* temp = new node;
temp->data = val;
temp->next = NULL;
node* curr = new node;
curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = temp;
}
void display(){
if (head == NULL){
std::cout << endl << "Linked List is EMPTY.";
} else {
int i = 1;
node* curr = new node;
curr = head;
std::cout << endl << "The linked list contains..." << endl;
while(curr->next != NULL){
std::cout << "Elements " << i << ':'
<< tab << curr->data << endl;
curr = curr->next;
i++;
}
}
}
};
输出应该是...
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
英文:
I am writing a code which will store elements in linked list, however when i'm adding an element at the end of the list the output is always like this...
enter image description here
i've already search from many tutorials and copying their code to see if it works on mine but it doesn't..
#include <iostream>
#define tab '\t'
using std::endl;
struct node {
int data;
node* next;
};
class Linkedlist {
private:
node* head = new node;
public:
Linkedlist(){
head->next = NULL;
}
void addStart(int val){
node* temp = new node;
temp->data = val;
temp->next = head;
head = temp;
}
void addEnd(int val){
node* temp = new node;
temp->data = val;
temp->next = NULL;
node* curr = new node;
curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = temp;
}
void display(){
if (head == NULL){
std::cout << endl << "Linked List is EMPTY.";
} else {
int i = 1;
node* curr = new node;
curr = head;
std::cout << endl << "The linked list contains..." << endl;
while(curr->next != NULL){
std::cout << "Elements " << i << ':'
<< tab << curr->data << endl;
curr = curr->next;
i++;
}
}
}
};
The Output should be..
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
答案1
得分: 2
如果您在空列表上使用 addEnd
,head
将指向 NULL
。然后,当您执行 curr = head
时,curr
也是 NULL
。接下来,您执行 while (curr->next != NULL)
,这是非法的,因为 curr
是 NULL
,所以 curr->next
指向未知的内存。
您可以添加一个条件来检查 head
是否为 NULL
:
void addEnd(int val) {
if (head == NULL) {
this->addStart(val);
return;
}
//其余的代码
}
上述代码检查了 head
是否为 NULL
。如果是,表示列表为空,将元素添加到列表末尾在功能上等同于将元素添加到列表开头。在这种情况下,您可以只使用 addStart
。
英文:
If you use addEnd
on an empty list, head
will point to NULL
. Then, when you do curr = head
, curr
is also NULL
. Then, you do while (curr->next != NULL)
, which is illegal since curr
is NULL
, so curr->next
points to unknown memory.
You can add a condition checking if head
is NULL
:
void addEnd(int val) {
if (head == NULL) {
this->addStart(val);
return;
}
//the rest of the code
}
The above code checks if head
is NULL
. If it is, the list is empty, and adding an element to the end of the list is functionally equivalent to adding an element to its start. You can just use addStart
in that case.
答案2
得分: 1
Bugs:
- 在addStart()函数中:
问题:这段代码从不将最后一个节点的next设置为NULL
解决方案:
a) 将head声明为null
b) 为两种不同的情况编写代码(当head为NULL和不为NULL时):
Linkedlist(){
head = NULL;
}
void addStart(int val){
node* temp = new node;
temp->data = val;
if(head == NULL){
head = temp;
head->next = NULL;
return;
} else {
temp->next = head;
head = temp;
return;
}
}
- 您没有显示最后一个节点。
解决方案:将display函数中的while循环条件从
curr->next != NULL
更改为
curr != NULL
整体代码:
#include <iostream>
#define tab '\t'
using std::endl;
struct node {
int data;
node* next;
};
class Linkedlist {
private:
node* head = new node;
public:
Linkedlist(){
head = NULL;
}
void addStart(int val){
node* temp = new node;
temp->data = val;
if(head == NULL){
head = temp;
head->next = NULL;
return;
} else {
temp->next = head;
head = temp;
return;
}
}
void addEnd(int val){
node* temp = new node;
temp->data = val;
temp->next = NULL;
node* curr = new node;
curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = temp;
}
void display(){
if (head == NULL){
std::cout << endl << "Linked List is EMPTY.";
} else {
int i = 1;
node* curr = new node;
curr = head;
std::cout << endl << "The linked list contains..." << endl;
while(curr != NULL){
std::cout << "Elements " << i << ':'
<< tab << curr->data << endl;
curr = curr->next;
i++;
}
}
}
};
英文:
Bugs:
- at addStart() function:
> temp->next = head;
> head = temp;
Problem: This code never kept the last nodes' next as NULL
Solution:
a) declared head as null
b) writing code for 2 different conditions (when the head is NULL and not NULL):
Linkedlist(){
head= NULL;
}
void addStart(int val){
node* temp = new node;
temp->data = val;
if(head == NULL){
head = temp;
head->next = NULL;
return;
} else {
temp->next = head;
head = temp;
return;
}
}
- You didn't display the last node.
Soln: change while loop condition in display function from
> curr->next != NULL
to
> curr != NULL.
OVERALL CODE:
#include <iostream>
#define tab '\t'
using std::endl;
struct node {
int data;
node* next;
};
class Linkedlist {
private:
node* head = new node ;
public:
Linkedlist(){
head= NULL;
}
void addStart(int val){
node* temp = new node;
temp->data = val;
if(head == NULL){
head = temp;
head->next = NULL;
return;
} else {
temp->next = head;
head = temp;
return;
}
}
void addEnd(int val){
node* temp = new node;
temp->data = val;
temp->next = NULL;
node* curr = new node;
curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = temp;
}
void display(){
if (head == NULL){
std::cout << endl << "Linked List is EMPTY.";
} else {
int i = 1;
node* curr = new node;
curr = head;
std::cout << endl << "The linked list contains..." << endl;
while(curr != NULL){
std::cout << "Elements " << i << ':'
<< tab << curr->data << endl;
curr = curr->next;
i++;
}
}
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论