2017-06-15 94 views
0

我正在學習C++中的數據結構。這是一個使用鏈接和節點插入 的簡單程序。插入發生在節點的開始處。 我不明白代碼的某些部分。C++中的數據結構。插入鏈表中的節點開頭

在函數display()中,指針np指向插入的信息,然後使用下一個節點獲取先前信息的值。下一個指針使用insert_beginning()函數指向以前的信息。 顯示使用while循環完成。下一個指針在每個循環中如何更改其值?

PS:程序運行良好。

#include<iostream> 
#include<process.h> 
#include<cstdlib> 
using namespace std; 

struct node 
{ 
    int info; 
    node *next; 
}*start,*newptr,*save,*ptr; 

node *create_new_node(int); 
void insert_beg(node*); 
void display(node*); 

/*---------------------------------------------------------------------------------------------------------------------------- 

The pointer 'start' points to the beginning of the list. 

Function 'create_new_node()' takes one integer argument , allocates memory to create new node and returns 
the pointer to the new node.(return type: node*) 

Function 'insert_beg()' takes node* type pointer as an argument and inserts this node in the beginning of the list. 

Function display takes node* type pointer as an argument and displays the list from this pointer till the end of the list 

------------------------------------------------------------------------------------------------------------------------------ 
*/ 

int main() 
{ 
    start=NULL; 
    int inf; 
    char ch='y'; 
    while(ch=='y'||ch=='Y') 
    { 
     system("cls"); 

     cout<<"enter information for the new node "; 
     cin>>inf; 
     cout<<"\ncreating new node. Press enter to continue "; 
     system("pause"); 

     newptr = create_new_node(inf); 

     if(newptr!=NULL) 
     { 
      cout<<"\nnew node created successfully. Press enter to 
continue. "; 
      system("pause"); 

     } 

     else 
     { 

      cout<<"\nCannot create new node. ABORTING!! "; 
      exit(1); 

     } 

     cout<<"\nnow inserting this node in the beginning of the list. 
Press enter to continue "; 
     system("pause"); 
     insert_beg(newptr); 
     cout<<"\nNow the list is \n"; 
     display(start); 
     cout<<"\nPress 'Y' to enter more nodes, 'N' to exit\n"; 
     cin>>ch; 

    } 

    return 0; 
} 

node *create_new_node(int n) 
{ 
    ptr=new node; 
    ptr->info=n; 
    ptr->next=NULL; 

} 

void insert_beg(node *np) 
{ 

     if(start==NULL) 
     start=np; 
     else 
    { 
     save=start; 
     start=np; 
     np->next=save; 
    } 

} 

void display(node *np) 
{ 

    while(np!=NULL) 
    { 
     cout<<np->info<<" ->"; 
     np=np->next; 

    } 
    cout<<"!!!\n"; 
} 
+0

函數顯示如何與插入節點相關? –

+0

鏈表中的每個節點都包含下一個節點的地址,所以'np-> next'指向下一個節點,該地址再次被分配給'np'np = np-> next',這就是'np'移動到下一個節點。 –

回答

0

短切長的故事 - 按我的理解,你的基本問題是: -

顯示器使用while循環來完成。下一個指針在每個循環中如何改變其值? ??

這恰恰發生在這一行: -

np=np->next;

你基本上是推進指針node結構到另一個node結構,它的地址是在第一個節點結構的next成員。這是教科書的東西,任何基本的算法書應該包括這個徹底

HTH!

+0

接下來保存前一個節點的地址。 np從該地址獲取值。那麼下一步如何指向另一個節點。 –

+0

接下來保存下一個節點的地址,而不是前一個節點的地址 – Zakir

+0

它如何指向每個循環中的下一個信息? –

0

你的問題有點不清楚。尤其是因爲你聲明:

PS:程序運行良好。

它肯定是不是。有一個簡單的意思是這個程序不起作用。

的問題是,create_new_node沒有返回指針值

node *create_new_node(int n) 
{ 
    ptr=new node; 
    ptr->info=n; 
    ptr->next=NULL; 
    return ptr;  // This line is missing 
} 

除此之外,這是一個非常糟糕的主意,用全局指針變量!

這裏

struct node 
{ 
    int info; 
    node *next; 
}*start,*newptr,*save,*ptr; 

你定義struct node,但你也可以定義4個變量,即4點指向節點。這些變量將是全局的,即在您的所有代碼中都可用。東西你應該從來沒有做。

反而讓局部變量需要 - 例如:

node *create_new_node(int n) 
{ 
    node *ptr; // Local variable instead of global 
    ptr=new node; 
    ptr->info=n; 
    ptr->next=NULL; 
    return ptr; 
} 

那麼對於insert_beg改變它,以便它返回一個新的開始指針 - 樣:

node* insert_beg(node* start, node *np) 
{ 
    np->next=start; 
    return np; 
} 

main使用像:

node* start = NULL; 
... 
... 
start = insert_beg(start, newptr); 

順便說一句 - 在現代的C++中,你會nev呃使用原始指針,你永遠不會寫自己的列表。使用智能指針而不是原始指針。使用標準容器而不是自己寫。

+0

實際上程序運行時沒有建議的編輯。我確實想過從create_new_node()返回指針,但編譯和輸出很好。順便說一句...你有沒有詳細說明智能指針? –

+0

@SohitGore 1)如果沒有'return ptr',程序就不能正確運行2)智能指針......好了,原始指針的問題在於許多程序員無法處理它們。他們忘記釋放/刪除導致很多問題的內存(又稱內存泄漏)。因此,智能指針是將責任委託給特殊類的一種方式,即唯一指針或共享指針 - 智能指針將自動處理內存釋放 - 就像一種垃圾收集。如果你想做現代的C++,你應該閱讀它們。 – 4386427

+0

@SohitGore - 順便說一句:記得總是把警告轉化爲錯誤(即編譯器選項)。你的編譯器尖叫着你沒有在該函數返回:-) – 4386427