[英]C++ linked list insert_end()
#include <iostream>
using namespace std;
class Node {
friend class LinkedList;
private:
int value;
Node *pNext;
public:
Node();
Node(int);
~Node();
};
class LinkedList {
private:
Node *pHead;
Node *pCursor;
public:
LinkedList();
void insert_front(int);
void insert_end(int);
void insert_after(int, int);
void delete_node();
void displayList();
void reverseSLL();
int size();
};
int main(int argc, const char * argv[]) {
LinkedList SLL;
for(int i=1;i<6;i++) SLL.insert_end(i);
return 0;
}
Node::Node() {
pNext = NULL;
}
Node::Node(int val) {
value = val;
pNext = NULL;
}
Node::~Node() {
}
LinkedList::LinkedList() {
pHead = NULL;
pCursor = NULL;
}
void LinkedList::insert_front(int val) {
if(size()==0) pHead = new Node(val);
else if(size()>0) {
pCursor = pHead;
pHead = new Node(val);
pHead->pNext = pCursor;
}
displayList();
return ;
}
void LinkedList::insert_end(int val) {
if(size()==0) pHead = new Node(val);
else if(size()>0) {
pCursor = pHead;
while(pCursor!=NULL) pCursor = pCursor->pNext;
Node *pNewNode = new Node(val);
pCursor->pNext = pNewNode;
}
displayList();
}
void LinkedList::insert_after(int nth, int val) {
pCursor = pHead;
Node *pNewNode = new Node(val);
for(int i=0;i<nth-1;i++) pCursor = pCursor->pNext;
pCursor->pNext = pNewNode;
displayList();
}
void LinkedList::delete_node() {
int val = 0;
cout<<"Enter a node you want to delete: "; cin>>val;
pCursor = pHead;
if(size()==0) return;
else if(size()==1) delete pCursor;
else if(size()>1) {
while(pCursor->value != val) {
cout<<pCursor->value;
pCursor = pCursor->pNext;
}
if(pCursor->pNext== NULL) delete pCursor;
else {
pHead = pCursor->pNext;
delete pCursor;
}
}
displayList();
}
void LinkedList::displayList() {
if(size()==0) return;
else if(size()>0) {
pCursor = pHead;
while(pCursor!=NULL) {
if(pCursor->pNext==NULL) {
cout<<pCursor->value;
break;
}
cout<<pCursor->value<<"->";
pCursor = pCursor->pNext;
}
} cout<<endl;
}
void LinkedList::reverseSLL() {
if(size()==0) return;
else if(size()>0) {
pCursor = pHead;
int reversedArr[size()];
for(int i=0;i<size();i++) {
reversedArr[i] = pCursor->value;
pCursor = pCursor->pNext;
}
for(int i=size()-1;i>=0;i--) {
if(i==0) cout<<reversedArr[i];
cout<<reversedArr[i]<<"->";
} cout<<endl;
}
}
int LinkedList::size() {
int temp = 0;
pCursor = pHead;
while(pCursor != NULL) {
temp ++;
pCursor = pCursor->pNext;
}
return temp;
}
I am trying to make a singly linked list and in "insert_end()" function, I see segmentation fault error.我正在尝试创建一个单链表,在“insert_end()”function 中,我看到了分段错误错误。 This function is supposed to add an element at the end of the linked list and then prints out the result.
这个 function 应该在链表的末尾添加一个元素,然后打印出结果。
void LinkedList::insert_end(int val) {
if(size()==0) pHead = new Node(val);
else if(size()>0) {
pCursor = pHead;
while(pCursor!=NULL) pCursor = pCursor->pNext;
Node *pNewNode = new Node(val);
pCursor->pNext = pNewNode;
}
displayList();
}
I do not know what my problem is.我不知道我的问题是什么。 I saw this segmentation fault error in my other functions.
我在其他函数中看到了这个分段错误错误。 What am I supposed to fix?
我应该解决什么问题?
while(pCursor!=NULL) pCursor = pCursor->pNext;
iterates pCursor untill it's NULL迭代 pCursor 直到它是 NULL
pCursor->pNext = pNewNode;
is now dereferencing the NULL pointer.现在取消引用 NULL 指针。
What should you be doing?你应该做什么? Probably stop one earlier, iterate until
pCursor->pNext
is NULL and then allocate the new one into pCursor->pNext
as you are already doing.可能更早停止一个,迭代直到
pCursor->pNext
为 NULL ,然后将新的分配给pCursor->pNext
,就像你已经在做的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.