2015-07-19 101 views
1

因此,在我的鏈表列表程序中,我想要它做的是要求用戶輸入多少個數字,然後輸入數字,然後添加這些數字數字在列表的末尾。然後,它將打印列表。之後,用戶將選擇列表中元素的位置來刪除,列表將再次被打印。在列表末尾插入一個整數並在第n個位置刪除

#include <iostream> 
using namespace std; 

struct Node{ 

int data; 
    Node* link; 
}; 

Node* head; 

void Insert(int data){ //insert an integer at the end of the list 

    Node* temp = new Node(); 
    Node* temp2 = new Node(); 

    temp->data = data; 
    temp->link = NULL; 
    if(head = NULL){ 
    head = temp; 
    return; 
    } 
    temp2 = head; 
    while(temp2->link != NULL){ 
    temp2 = temp2->link; 
    } 

    temp2->link = temp; 

} 

void Delete(int n){ //delete an integer at nth position 

    Node* temp1 = new Node(); 
    temp1 = head; 
    if(n == 1){ //if the first node is to be deleted 
    head = temp1->link; //now head points to second node 
    delete temp1; //delete first node 
    return; 
    } 

    for(int i = 0; i < n-2; i++){ 
    temp1 = temp1->link; //temp1 points to (n-1)th node 
    } 
    Node* temp2 = temp1->link; //temp2 points to nth node 
    temp1->link = temp2->link; // pointing to (n+1)th node 
    delete temp2; //deleting nth node 

} 

void Print(){ //print out the list 
    Node* printNode = head; 
    cout << "List: "; 

    while(printNode != NULL){ 
    cout << printNode->data; 
    cout << " "; 
    printNode = printNode->link; 
    } 

    cout << "\n"; 
} 

int main(){ 

    int x, count, n; 

    head = NULL; //start with an empty list 

    cout << "How many numbers? " << endl; 
    cin >> count; 

    for(int i = 0; i < count; i++){ 

    cout << "Enter number: "; 
    cin >> x; 
    Insert(x); 
    } 

    Print(); 

    cout << "Enter position to delete: "; 
    cin >> n; 

    Delete(n); 
    Print(); 

    return 0; 

} 

接受第一個數字後,程序停止工作。我可以知道我在哪裏做錯了代碼,我能做些什麼來使代碼更有效率?提前致謝。

+1

只有掠過代碼當前'如果(head = NULL)'應該是'if(head == NULL)'。不知道這是否解決了整個問題,但它是一個開始 – tomasbasham

+0

@tomasbasham這是一個很大的facepalm我現在已經修復了。 – nisyedlah

+0

沒問題,我可以指出,保存一個引用到鏈表的尾部會更有效率,而不是有t o每次插入數字時都要穿過它 – tomasbasham

回答

1

您可能需要重新考慮您的插入功能。代碼崩潰的部分是在while循環插入期間。如果你想temp2保存數據,那麼你需要動態地爲它分配空間。但是,您只是將其用作位置指示器(以遍歷列表) - 那麼爲什麼您需要分配空間以指向頭部或列表中的任何其他節點位置?

這裏是我怎麼會插入到表(在課程後面):

void Insert(int data){ //insert an integer at the end of the list 

    Node* temp = new Node(); 

    // This is to ensure that temp was created -> Also called defensive programming. 
    if (!temp) 
    { 
     cout << "We did not have enough space alloted to dynamically allocate a node!" << endl; 
     exit(1); 
    } 

    temp->data = data; // Bad nominclature for program; Don't use the same name twice. 
    temp->link = NULL; 

    if (head == NULL) 
    { 
     head = temp; 
    } 
    else 
    { 
     // This is to help traverse the linked list without having to 
     // manipulate the position of what head points to. 
     Node *Pos_Indicator = head; 

     while (Pos_Indicator->link != NULL) 
     { 
      Pos_Indicator = Pos_Indicator->link; 
     } 

     // We are at the end of the list, it is now safe to add. 
     Pos_Indicator->link = temp; 

     // Should probably have a check here for whether it was successful or not. 
    } 

} 

我能夠編譯並沒有其他問題,運行您的代碼完成。讓我知道如果這有幫助!

編輯:。或者你知道(head = NULL)(head == NULL)也工作:(

+1

這是一個很好的幫助,並且教了很多。而對於我的不好名聲,我應該改變哪一部分? – nisyedlah

+0

使用Pos_Indicator後,說是Pos_Indicator沒有在函數中聲明。跟隨你的信,所以我不知道什麼是錯的。 – nisyedlah

+0

你是如何申報的?我能夠在VS 2013中編譯和運行,沒有任何問題。 – KillaBytes

2
對我而言

大捂臉,只是一個小錯誤代碼已得到糾正

if(head == NULL){ 
    head = temp; 
    return; 
} 
相關問題