2016-03-06 75 views
1

我不明白這有什麼錯在這裏我的代碼:插入「否」鏈表節點和打印他們的數據(C++)

//Inserting n nodes, then print their values 
#include <iostream> 
#include <string> 

using namespace std; 

//Defining a node and it's head pointer 
struct node 
{ 
    int data; 
    node *next; 
}; 
node *head=NULL; 
node *link; 
node *tmp; 


int main() 
{ 
    int n; 
    cin>>n; 
    while (n>0) 
    { 
     //Insert n nodes into the list 
     link=new node; 
     if (head==NULL) 
     { 
      head=link; 
     } 
     cin>>link->data; 
     link=link->next; 
     n--; 
    } 
    link=NULL; 

    //print data present in those n nodes 
    tmp=head; 
    while (tmp!=NULL) 
    { 
     cout<<tmp->data; 
     tmp=tmp->next; 
    } 
    return 0; 
} 

代碼的第一部分定義了一個節點。

第二部分(main函數的開始部分)是用於創建鏈接列表的代碼,其中包含n個節點。所以我插入n個節點。

最後,我使用指針tmp輸出它們。然而,我不是獲取數據值,而是獲得無限循環。這裏發生了什麼?

謝謝

+0

'鏈接=鏈路>未來;'你可能要考慮分配的無用的下一次您導航循環體來。 – WhozCraig

+0

您的代碼存在許多問題,例如,一個內存泄漏,因爲你沒有刪除用'new'分配的內存,沒有意義的代碼,比如'link = link-> next'等等。然後再次我想你試圖在這裏重新發明輪子。爲什麼不使用一個std ::列表而不是? –

+0

爲什麼要使用鏈表?家庭作業。在現實生活中,除非你做了大量的插入 - 刪除(並且我的意思是插入和刪除的次數多得多),否則你要爲這個O(1)插入刪除付出很大的代價。他們有他們的用途,但他們沒有像你在學校看到的重點那麼有用。 – user4581301

回答

1
#include <iostream> 
#include <string> 

using namespace std; 

//Defining a node and it's head pointer 
struct node 
{ 
    int data; 
    node *next=NULL; 
}; 
node *head=NULL; 
node *link; 
node *tmp; 


int main() 
{ 
    int n,limit; 
    cin>>n; 
limit=n; 

    while (n>0) 
    { 
tmp=link; 
     link=new node; 

    link->next=NULL; 
     cin>>link->data; 
if (head==NULL) 
     { 
      head=link; 
     } 
if(n!=limit)                //check whether tmp is null initially tmp will be null for first element 
{ 
tmp->next=link; 
} 
     n--; 
    } 

    //print data present in those n nodes 
    tmp=head; 
    while (tmp!=NULL) 
    { 
     cout<<tmp->data<<"\n"; 
     tmp=tmp->next; 
    } 
    return 0; 
} 
1

首先它是C++。我建議使用std::list

#include <iostream> 
#include <list> // list 

int main() 
{ 
    int n; 
    std::cin >> n; 

    std::list<int> l; 
    while (l.size() < n) 
    { 
     int data; 
     std::cin >> data; 
     l.push_back(data); 
    } 

    for (int data : l) 
     std::cout << data << std::endl; 

    return 0; 
} 

但是,如果你想迪它自己,你要記住你的列表的末尾,並添加每一個新的節點作爲尾節點的繼任者。另外,你應該在你的程序結束delete名單:

#include <iostream> 

struct node 
{ 
    int data; 
    node *next; 
}; 
node *head = NULL; 

int main() 
{ 
    int n; 
    std::cin >> n; 

    node *tail = NULL; 
    while (n>0) 
    { 
     node *link = new node; 
     link->next = NULL; // successor of new node is NULL 
     std::cin >> link->data; 

     if (head==NULL) 
      head = link;  // if list is empty new node is head of list 
     else 
      tail->next = link; // successor of tail is new node 
     tail = link; 
     n--; 
    } 

    node *tmp = head; 
    while (tmp != NULL) // delete the list from head to tail 
    { 
     std::cout << tmp->data << std::endl; 
     tmp = tmp->next; 
    } 

    while (head != NULL) 
    { 
     tmp = head->next; 
     delete head; 
     head = tmp; 
    } 
    return 0; 
} 
1

此代碼你想使用std::list什麼。我不明白爲什麼你應該繼續創建自己的原始和不安全鏈接列表。 (我甚至不知道爲什麼你應該使用鏈表;在這個特定的std::vector將是一個更合理的選擇)。

#include <iostream> 
#include <list> 

int main() { 

    std::list<int> linked_list; 

    // fill the list 
    std::cout << "Please enter the number of elements in the list: " << std::endl; 
    int n {}; 
    std::cin>>n; 
    while (n-- > 0) { 
    std::cout << "Please enter the data for this item: " << std::endl; 
    int data {}; 
    std::cin>>data; 
    linked_list.push_back(data); 
    } 

    // print out data the list 

    for (auto& data : linked_list) 
    std::cout << "printing data entry: " << data << std::endl; 

    return 0; 
}