2015-02-10 16 views
0

我需要將鏈表中的第一項移動到列表的末尾。我的問題是我會陷入無限循環。當我刪除無限循環的原因(tail -> link != NULL;在for循環中)時,我得到一個seg故障。因此,尋找關於如何讓這些代碼正常工作的想法。將鏈表中的第一項移到結尾C++

#include <iostream> 
#include <string> 
using namespace std; 

struct Node 
{ 
    string data; 
    Node *link; 
}; 

class Lilist 
{ 
    public: 
    Lilist() {head = NULL;} 
    void add(string item); 
    void show(); 
    void move_front_to_back(); 
    Node* search(string target); 

    private: 
    Node *head; 
}; 

int main() 
{ 
    Lilist L1, L2; 
    string target; 

    L1.add("Charlie"); //add puts a name at the end of the list 
    L1.add("Lisa"); 
    L1.add("Drew"); 
    L1.add("Derrick"); 
    L1.add("AJ"); 
    L1.add("Bojian"); 

    cout << "Now showing list One:\n"; 
    L1.show(); // displays the list (This function displayed the list properly) 
    cout << "\n"; 

    L1.move_front_to_back(); 
    L1.move_front_to_back(); 
    L1.show(); 
    cout << "\n"; 

    return(0); 
} 


void Lilist::add(string item) 
{ 
    Node *temp; 
    if(head == NULL) 
    { 
    head = new Node; 
    head -> data = item; 
    head -> link = NULL; 
    } 
    else 
    { 
    for(temp = head; temp -> link != NULL; temp = temp -> link) 
     ; 
    temp -> link = new Node; 
    temp = temp -> link; 
    temp -> data = item; 
    temp -> link = NULL; 
    } 
} 

void Lilist::show() 
{ 
    for(Node *temp = head; temp != NULL; temp = temp -> link) 
    std::cout << temp -> data << " "; 
} 

void Lilist::move_front_to_back() 
{ 
    Node *temp; 
    Node *tail; 

    temp = head; 

    for(tail = head; tail != NULL; tail = tail -> link) 
    ; 

    head = head -> link; 
    tail -> link = temp; 
    temp -> link = NULL; 
} 
+1

郵報[MCVE](http://stackoverflow.com/help/mcve)。當你開始時,列表是否已經損壞? – Angew 2015-02-10 19:29:15

+0

目前我無法輸入所有內容,但我知道列表並未損壞。這是該計劃的後半部分。前半部分添加6個節點,然後輸出列表。我不記得我在這個部分中的代碼,但我曾經在某個地方「刪除」它移動的節點@Angew – transmini 2015-02-10 19:32:18

+0

如果您沒有必要的信息,那麼無法幫助您沒有理由在這裏保留這個問題。只是爲了記錄:這聞起來也像功課。 – 2015-02-10 19:33:52

回答

2

問題在於你如何計算tail。請注意,這(略去了不相關的線):

for(tail = head; tail != NULL; tail = tail -> link) 
    ; 
tail -> link = temp; 

注意,for循環只會終止一次tailNULL。然後,你解除引用tail ...這是空的。

因此改變for循環條件:

for (tail = head; tail->link != NULL; tail = tail->link) 
    ; 

這將在列表中找到的最後一個元素,而不是流走到底。

[Live example]

+0

呵呵。我可以發誓,我把它,它給了我一個無限循環。有效。謝謝! – transmini 2015-02-10 22:02:43

+1

當然,如果'Lilist'有一個'tail'成員,它會隨時更新它,就像它的'head'成員一樣,那麼它就會容易得多,那麼它就不會浪費時間去搜索'tail'每次都需要。 – 2015-02-10 22:24:06

1

Angew已經解釋了爲什麼你的原代碼失敗。我會建議一種替代方法 - 將Lilist一個tail成員與其head成員一起管理。然後,你不必去尋找,只要你需要它tail,你總是知道到底是哪Node是當前tail,如:

#include <iostream> 
#include <string> 

using namespace std; 

struct Node 
{ 
    string data; 
    Node *next; 

    Node(string s); 
}; 

class Lilist 
{ 
public: 
    Lilist(); 
    ~Lilist(); 
    void add(string item); 
    void show(); 
    void move_front_to_back(); 
    Node* search(string target); 

private: 
    Node *head; 
    Node *tail; 
}; 

Node::Node(string s) 
    : data(s), next(NULL) 
{ 
} 

Lilist::Lilist() 
    : head(NULL), tail(NULL) 
{ 
} 

Lilist::~Lilist() 
{ 
    for(Node *temp = head; temp != NULL; temp = temp->next) 
     delete temp; 
} 

void Lilist::add(string item) 
{ 
    Node *temp = new Node(item); 

    if (head == NULL) 
     head = temp; 

    if (tail != NULL) 
     tail->next = temp; 

    tail = temp; 
} 

void Lilist::show() 
{ 
    for(Node *temp = head; temp != NULL; temp = temp->next) 
     cout << temp->data << " "; 
} 

void Lilist::move_front_to_back() 
{ 
    if (head == tail) 
     return; 

    Node *temp = head; 

    head = temp->next; 
    temp->next = NULL; 

    tail->next = temp; 
    tail = temp; 
} 

Node* Lilist::search(string target) 
{ 
    for(Node *temp = head; temp != NULL; temp = temp->next) 
    { 
     if (temp->data == target) 
      return temp; 
    } 
    return NULL; 
} 
相關問題