2013-08-24 66 views
0

我的下面的代碼只打印第一個元素。在print_list()功能中,打印第一個元素後停止。它說第一個元素後,head->next0。不應該指向第二個元素?在C++中打印鏈接列表

我想簡單地打印整個列表。

#include<iostream> 
#include<cstdlib> 
using namespace std;  
struct node { 
    int x; 
    node *next; 
};  
node* add_element(node*); 
bool is_empty(node*); 
void print_list(node*); 
node* search(node*); 

int main() 
{ 
    node *head; 
    head=NULL; 
    node* current=head; 
    for(int i=0;i<5;i=i+1) 
    { 
     if (current==NULL) 
     { 
     current=add_element(current); 
     head=current; 
     } 
     else{ 
      current=add_element(current); 
     } 
    } 
    cout<<head->next<<endl; 

    // DOUBT: head->next gives NULL value. It should give me pointer to 2nd node 
    print_list(head); 
}  
node* add_element(node* current) 
{ 
    node* temp; 
    temp=new node; 
    temp->next=NULL; 
    cout<<"enter element"<<endl; 
    cin>>temp->x; 
    current=temp; 
    return current; 
}  
bool is_empty(node* temp) 
{ 
    return temp==NULL;  
}  
void print_list(node* temp) 
{ 
    if (is_empty(temp)==false) 
    { 
     cout<<"here temp(head)"<<temp->next<<endl; 
     while(temp!=NULL) 
     { 
      cout<<temp->x<<endl; 
      temp = temp->next; 
     } 
    } 
} 
+0

但是您不要調用:'print_list()' –

+0

現在已添加。感謝您指出。但問題仍然存在。 –

回答

1

打印功能打印第一個元素,因爲在鏈接列表中只有一個節點!實際上,錯誤的存在add_element(node*)功能,改寫用新的節點(從而具有內存泄漏)head節點作爲我下面標記的地址:

node* add_element(node* current) 
    { 
    node* temp;   
    temp = new node;  <---" You allocated memory" 
    temp->next = NULL; <---" Set next NULL" 
    cout<< "enter element" << endl; 
    cin>> temp->x;  <---" Assign a value in new node"  

    // Replace below two line with suggested 
    current = temp;  <---"MISTAKE: Overwrite first node"  
          "temp next is NULL so losing address of other nodes" 

    return current;  <--- "return first node" 
    } 

下一頁新節點(所以第一節點)的是NULL因此打印函數僅打印第一個節點的值。

建議:

您應該更正如下添加新節點在鏈表第一個節點:

temp -> next = current; // new nodes next if present first node 
return temp;    // new code becomes first node 

小心current應爲NULL開始。

用我在add_element()功能建議在main()也改變for循環代碼如下:

for(int i=0; i < 5; i = i + 1){ 
    current = add_element(current); 
} 
head = current; 

而在Codepade檢查工作碼(而不是用戶輸入我使用y = 100可變增值)。

編輯要追加新的節點:

您需要檢查新節點是否是不是第一個節點(閱讀評論)。

// returns first node address in linked list = head 
    node* add_element(node* head){ 
    node *temp, *new_nd; 

    // Create new node 
    new_nd = new node; 
    new_nd->next = NULL; 
    cout<<"enter element"<<endl; 
    cin>>new_nd->x; 

    // Is new node is the first node? 
    if(!head) 
     return new_nd; 

    // move to last 
    temp = head; 
    while(temp->next) temp = temp->next; 

    // add new node at last 
    temp->next = new_nd; 

    // return old head 
    return head; 
    } 

也簡單的main()如下:

int main(){ 
    node *head = NULL; 
    for(int i = 0; i < 5; i = i + 1){ 
     head = add_element(head); 
    } 
    print_list(head); 
} 

檢查此working code

+0

中的head = NULL,程序停止工作。我在Windows7 CMD中使用MinGW。 - –

+0

@ user2419974你在做錯誤的閱讀答案我張貼,最初'當前'爲空,你不能做'current-> next = temp' –

+0

我想追加元素在末尾 –

1

你的問題是在這裏:

node* add_element(node* current) 
    { 
    node* temp; //You created a new node 
    temp=new node; //You allocated it here 
    temp->next=NULL; //You set its next property to null 
    cout<<"enter element"<<endl; // 
    cin>>temp->x; 
    current=temp; //This should be current->next = temp. You are overwriting it! 
    return current; //And now you are returning essentially the temp object that 
        //You created and you set its next property to NULL 
    } 

你分配你temp = new node創建中傳遞當前節點的節點,你想要做的是分配給你剛剛創建的節點。當前節點的下一個屬性。它應該是current->next = temp

+0

當我做current-> next = temp時,程序停止工作。我在Windows7 CMD中使用MinGW。 –

+0

當我執行current-> next = temp時,你的main() – arp001

0

head-> next是NULL,因爲您在add_element()中設置了它。要有一個鏈表,你應該設置current-> next = temp。

正如你使用C++,你可能會考慮使用std :: list而不是實現自己的鏈表。

0
if (current==NULL) 
        { current=add_element(current); 
         head=current; 
        } 
        else 
        { current->next=add_element(current); 
         current=current->next; 
        } 

正確的代碼。 你必須在循環中做一個小的修正。 您必須添加一個新節點,然後使其指向當前節點的下一個節點。 所以簡化代碼是current-> next = add_element(current) ,然後將當前點指向新電流。