2014-12-03 41 views
-3

我已經寫了一些代碼在「第n」的位置插入一個單鏈表中的節點,但是當我運行該項目,我收到分段錯誤插入節點:11段錯誤,在單鏈表

當n == sizeM時,我會調用push_back,它會在最後插入節點。當n ==爲0時,我會調用push_font,它會在開始時插入節點。這兩個函數都起作用,因爲我已經打電話給他們在我的主文件中檢查。之前,我撥打任何電話插入我的列表包含

與headM指向440這是在列表中的第0位置。

然後我讓3個電話插入

插入(40.3)

插入(10,0)

插入(33,2)

和列表現在應該be 10 440 33 330 110 40

林不知道什麼是錯,但我很肯定這是我插入33時要做的事情。

這是包含類

​​

這是一個包含成員定義我的文件,該文件是不工作的一個是插入功能

#include <iostream> 
using namespace std; 

#include <stdlib.h> 
#include "SimpleList.h" 


SimpleList& SimpleList::operator =(const SimpleList& rhs) 
{ 
if (this != &rhs) { 
    destroy(); 
    copy(rhs); 
} 

sizeM = rhs.sizeM; 
return *this; 
} 

ListItem& SimpleList::at(int n) 
{ 
if(n < 0 || n >= sizeM) 
{ 
    cout << "\n Illegal Access. Program Terminates..."; 
    exit(1); 
} 

Node * p = headM; 
for(int i= 0; i < n; i++) 
    p = p -> next; 

// point four 

return p -> item; 
} 

const ListItem& SimpleList::at(int n)const 
{ 
if(n < 0 || n >= sizeM){ 
    cout << "\n Illegal Access. Program Terminates..."; 
    exit(1); 
} 

Node * p = headM; 
for(int i= 0; i < n; i++) 
    p = p -> next; 

// point three - when reached for the first time 
return p -> item; 
} 

void SimpleList::push_back(const ListItem& item) 
{ 
Node *new_node = new Node; 
if(new_node == NULL) 
{ 
    cout << "\nNo memory available to create a node" << endl; 
    exit(1); 
} 

new_node->item = item; 

if (headM == 0) { 
    new_node->next = headM; 
    headM = new_node; 
} 
else 
{ 
    Node* p = headM; 
    while (p ->next != NULL) 
     p = p ->next; 

    p -> next = new_node; 
    new_node -> next = NULL; 
} 
sizeM++; 

// point five - when reached for the third time 
} 


void SimpleList::push_front(const ListItem& item) 
{ 
Node *new_node = new Node; 
new_node->item = item; 
new_node->next = headM; 
headM = new_node; 
sizeM++; 

// point two 
} 

void SimpleList::destroy() 
{ 

// This function is not properly designed. 
cout << "\nSimpleList::destroy was called but didn't do the right job."; 
headM = 0; 
} 


void SimpleList::copy(const SimpleList& source) 
{ 

// this function is incomplete and is not properly designed. It doesnt do 
// its job, makeing 'this' SimpleList object a copy of the scoure. 

// The only effect of the next line is to tell the compiler 
// not to generate an "unused argument" warning. If you are going to complete 
// this funciton, don't leave it in your solution. 
(void) source; 

cout << "\nSimpleList::copy was called but didn't do the right job." 
<< "--program is terminated.\n"; 
exit(1); 
} 

void SimpleList::insert(const ListItem& theItem, int n) 
{ 
if (n < 0 || n > sizeM) 
    return; 
else if (n == sizeM) 
    push_back(theItem); 
else if (n == 0) 
    push_front(theItem); 
else 
{ 
    Node* new_node = new Node; 
    new_node->item = theItem; 
    Node* temp = headM; 
    for(int i = 0; i < (n - 1) ; i++) 
    { 
     temp = temp->next; 
    } 
    new_node->next = temp->next; 
    temp->next = new_node; 
    sizeM++; 
} 
} 

void SimpleList::remove(int n) 
{ 
if(n < 0 || n > sizeM) { 
    return; 
} 

Node* p = headM; 

if(n == 0 && headM != NULL) 
{ 
    Node* const p_doomed = headM; 
    headM = p_doomed->next; 
    delete p_doomed; 
    --sizeM; 
} 
else{ 
    for(int c = 0; c < n - 1; c++) 
    { 
     p = p->next; 
    } 
    Node* const p_doomed = p->next; 
    p->next = p_doomed->next; 
    delete p_doomed; 
    --sizeM; 
} 
} 

和我下面的simpleList.h是我的主要文件

#include <iostream> 
#include <iomanip> 
using namespace std; 
#include "SimpleList.h" 
#define EXERCISE_B 

void print(const SimpleList& list); 
// PROMISES: prints values in the list from first node (node number 0) to 
//   the last node. 

int main() 
{  
SimpleList list; 

cout << "\nList just after creation -- is empty."; 

list.push_front(50); 
cout << "\nAfter calling push_front. list must have: 50\n"; 
print(list); 

list.push_back(440); 

list.at(0) = 770; 
cout << "\nAfter calling push_back and at functions, list must have: 770 440\n"; 
print(list); 

list.push_back(330); 
list.push_back(220); 
list.push_back(110); 

cout << "\nAfter three more calls to push_back, list must have:" 
"770, 440, 330, 220, 110\n"; 
print(list); 

#if defined (EXERCISE_B) 
list.remove(0); 
list.remove(2); 
cout << "\nAfter removing two nodes. list must have: 440, 330, 110\n"; 
print(list); 
list.insert(40, 3); //insert node with the value of 40 at the 4th position 
list.insert(20, -1); // do nothing 
list.insert(30, 30000); // do nothing 
list.insert(10, 0); //insert node with the value of 10 at the 1st position 
list.insert(33, 2); // insert node with the value 33 at the 3rd position 

cout << "\nTwo more nodes inserted, must have: 10, 440, 33, 330, 110, 40\n"; 
print(list); 

list.remove(0); 
list.remove(1); 
list.remove(2); 
list.remove(3); 
list.remove(4); 
list.remove(5); 
cout << "\nAfter 6 removes, list must have: 440, 330, 40: \n"; 
print(list); 

#endif 
return 0; 

} 


void print(const SimpleList& list) 
{ 
for(int i = 0; i < list.size(); i++) 
    cout << list.at(i) << " "; 
} 

,這是我收到

輸出
Haydns-MacBook-Pro:desktop Haydn$ g++ lab9_EXE_A.cpp simpleList.cpp 
Haydns-MacBook-Pro:desktop Haydn$ ./a.out 

List just after creation -- is empty. 
After calling push_front. list must have: 50 
50 
After calling push_back and at functions, list must have: 770 440 
770 440 
After three more calls to push_back, list must have:770, 440, 330, 220, 110  
770 440 330 220 110 
After removing two nodes. list must have: 440, 330, 110 
440 330 110 
Two more nodes inserted, must have: 10, 440, 33, 330, 110, 40 
Segmentation fault: 11 
+5

處理縮進和一般格式(也用於純文本)。另外,你確定這是一個*最小*的例子嗎?請參閱:[MCVE](https:// stackoverflow。com/help/mcve) – Deduplicator 2014-12-03 18:32:56

+1

借調縮進。儘管就MCVE而言,如果他能找到並提供MCVE,他很可能不會有任何問題:)。 – ChrisCM 2014-12-03 18:35:58

+0

@Deduplicator我之前問過這個問題,大家都說提供了一個完整的例子,所以我有 – 2014-12-03 18:40:02

回答

1

的問題是在第一線void SimpleList::remove(int n)

if (n < 0 || n >= sizeM) { // CHange to >= and not > !!! 

說明:

如果函數調用n0,如果列表已經爲空(意思是sizeM==0headM==nullptr),你的原核心,n<0 || n>sizeM將是錯誤的。

這意味着該功能將繼續,電子執行流程之中:如果函數調用n是是exacly你的棧的大小,這發生在你main()

... 
p=headM; // meaning that p is now nullptr 
if (n == 0 && headM != NULL) // false because `n==0 && headM==nullptr` 
// if block ignored 
else{ // here: (n!=0 || headM==NULL) 
    for (int c = 0; c < n - 1; c++) // the condition is false 
    // the loop body is ignored 
    Node* const p_doomed = p->next; // ouch !!!! p is nullptr => segfault !!! 

當你remove(3) ,情況類似。當p已經是nullptr時,您嘗試訪問一次太多到p->next

+0

- 改變> =工作!!我認爲這是我的插入有問題,但它與我的刪除,而不是謝謝你 – 2014-12-03 23:08:07