合并排序林链表给出分段错误
我应用了合并排序算法来对链表进行排序,但是当我应用它时,它给出了其中存在分段错误(核心转储)。 帮我调试一下。
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int data){
this->data = data;
next = NULL;
}
};
Node *takeInput(int n){
int data;
Node *head = NULL;
Node *tail = NULL;
while(n>0){
cin >> data;
Node *newNode = new Node(data);
if(head == NULL){
head = newNode;
tail = newNode;
}else{
tail->next = newNode;
tail = newNode;
}
n--;
}
return head;
}
Node *merge(Node *head , Node *head2){
Node *newhead = NULL;
Node *newTail = NULL;
while(head != NULL && head2 != NULL){
int x = head->data;
int y = head2->data;
if(x < y){
if(newhead == NULL){
newhead = head ;
newTail = head ;
}else{
newTail->next = head;
newTail = head;
}
head = head->next;
}else{
if(newhead == NULL){
newhead = head2 ;
newTail = head2 ;
}else{
newTail->next = head2;
newTail = head2;
}
head2 = head2->next;
}
}
if(head == NULL){
newTail->next = head2;
}else{
newTail->next = head;
}
return newhead;
}
Node *mergeSort(Node *head, int n){
if(n == 0){
head->next =NULL;
return head;
}else{
int t = (n-1)/2;
Node *temp = head;
while(t>0){
temp = temp->next;
t--;
}
Node *head2 = temp->next;
temp->next = NULL;
head = mergeSort(head,n/2);
int n2 = n - n/2 ;
head2 = mergeSort(head2,n2);
Node *newHead = merge(head,head2);
return newHead;
}
}
void print(Node *head){
while(head != NULL){
cout << head->data << " ";
head = head->next;
}
}
int main()
{
int n;
cin >> n ;
Node *head = takeInput(n);
Node *newHead = mergeSort(head,n);
print(newHead);
return 0;
}
我尝试调试但无法找出问题所在。 我正在检查注释之一中指定的 nullptr
,temp
,让我们看看它是否有效。
I applied merge sort algorithm to sort a linked list but when I applied it , it's giving there is a segmentation fault(core dumped) in it.
Help me to debug it.
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int data){
this->data = data;
next = NULL;
}
};
Node *takeInput(int n){
int data;
Node *head = NULL;
Node *tail = NULL;
while(n>0){
cin >> data;
Node *newNode = new Node(data);
if(head == NULL){
head = newNode;
tail = newNode;
}else{
tail->next = newNode;
tail = newNode;
}
n--;
}
return head;
}
Node *merge(Node *head , Node *head2){
Node *newhead = NULL;
Node *newTail = NULL;
while(head != NULL && head2 != NULL){
int x = head->data;
int y = head2->data;
if(x < y){
if(newhead == NULL){
newhead = head ;
newTail = head ;
}else{
newTail->next = head;
newTail = head;
}
head = head->next;
}else{
if(newhead == NULL){
newhead = head2 ;
newTail = head2 ;
}else{
newTail->next = head2;
newTail = head2;
}
head2 = head2->next;
}
}
if(head == NULL){
newTail->next = head2;
}else{
newTail->next = head;
}
return newhead;
}
Node *mergeSort(Node *head, int n){
if(n == 0){
head->next =NULL;
return head;
}else{
int t = (n-1)/2;
Node *temp = head;
while(t>0){
temp = temp->next;
t--;
}
Node *head2 = temp->next;
temp->next = NULL;
head = mergeSort(head,n/2);
int n2 = n - n/2 ;
head2 = mergeSort(head2,n2);
Node *newHead = merge(head,head2);
return newHead;
}
}
void print(Node *head){
while(head != NULL){
cout << head->data << " ";
head = head->next;
}
}
int main()
{
int n;
cin >> n ;
Node *head = takeInput(n);
Node *newHead = mergeSort(head,n);
print(newHead);
return 0;
}
I tried to debug but was not able to find out what's the fault.
I am checking for nullptr
,temp
as specified in one of the comment , let's see if it work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我调试了一下。
我用 : 替换了 mergeSort 的基本情况
,它起作用了。
Finally, I debugged it.
I replaced base case of mergeSort by :
and it worked.