2012-12-13 55 views
0

它只返回一個地址,沒有任何調試錯誤,儘管我的DEV C++和Code :: Blocks編譯器都顯示send不發送窗口錯誤,但它們僅初始化類對象,I已經包括了代碼,誰能告訴我,爲什麼它的發生初學者:圓形單鏈表的第一個程序,

#include <iostream> 
#include <conio.h> 
using namespace std; 
struct Node 
{ 
    int data; 
    Node *nextptr; 
}; 


class CLLIST{ 

private: 

    Node*firstptr; 
    Node*lastptr; 

public: 
    CLLIST(){ 

    cout << "Constructor Called !"; 
     firstptr=lastptr=NULL; 
} 

void insert_at_back(int val){ 

     if(firstptr==NULL) //it means start of C.LIST 
     { 
      Node*temptr = new Node; //means firstptr = temptr 
      firstptr->data=val; 
      firstptr=temptr; 
      firstptr->nextptr=firstptr; 
     } else{ 

      Node*temp1 = new Node; 
      Node*temp2 = new Node; 

      temp1 = firstptr; 
      while(temp1->nextptr!=firstptr) //traversing 
      { 
       temp2 = temp1->nextptr; 
       temp2->data = val; //inserted at back 
       temp2->nextptr=firstptr; //circle completed 
      } 
     } 
} 

void printit(){ 

      // functiont o print all the circular link lists data 
      Node*temp3ptr= new Node; 
      temp3ptr = firstptr; 

      while(temp3ptr->nextptr!=firstptr)//traversing 
      { 
       cout << temp3ptr->data; 
       cout << endl; 
      } 
} 
}; 


    int main() 
    { 
    CLLIST obj1; 

    obj1.insert_at_back(10); 
    obj1.insert_at_back(20); 
    obj1.insert_at_back(30); 

    obj1.printit(); 

    cout << "Done !"; 

    getch(); 
    } 
+0

Node * temptr = new Node; //表示firstptr = temptr firstptr-> data = val; –

+0

firstptr仍然= NULL在那一點 –

回答

3

的幾個問題與您現有的代碼(可能還有更多,但你應該專注於解決這些第一)


問題1

  if(firstptr==NULL) //it means start of C.LIST 
     { 
      Node*temptr = new Node; //means firstptr = temptr 
      firstptr->data=val; 
      firstptr=temptr; 
      firstptr->nextptr=firstptr; 
     } else{ 

^隨着firstptr->data=val;你提領firstptr即使它仍然NULL。 SWAP它和下一行,所以它讀取:

  if(firstptr==NULL) //it means start of C.LIST 
     { 
      Node*temptr = new Node; //means firstptr = temptr 
      firstptr=temptr; 
      firstptr->data=val; 
      firstptr->nextptr=firstptr; 
     } else{ 

或更好,但只是做:firstptr = new Node;直接跳過temptr


問題2

 Node*temp1 = new Node; 
    Node*temp2 = new Node; 

    temp1 = firstptr; 
    while(temp1->nextptr!=firstptr) //traversing 
    { 
     temp2 = temp1->nextptr; 
     temp2->data = val; //inserted at back 
     temp2->nextptr=firstptr; //circle completed 
    } 

^這是內存泄漏; new爲堆分配內存,當您將臨時節點指向firstptr時,其地址將丟失。剛剛宣佈temp1temp2代替:

 Node*temp1; 
    Node*temp2; 

問題3

while(temp1->nextptr!=firstptr) 

^這個while循環將永遠不會因爲運行:

  • 你開始firstptr爲null
  • 然後你添加一個節點,並且firstptr->next指向firstptr
  • 然後,當您嘗試添加第二個節點,它分配temp1 = firstptr;的工作,但while循環無法運行,因爲firstptr->next == firstptr

問題4

正如@ aleguna指出:

 Node*temp3ptr= new Node; 
     temp3ptr = firstptr; 

^這是另一個內存泄漏,對於sa我問題2中說明的原因。剛剛宣佈temp3ptr代替:

 Node*temp3ptr; 

問題5

 while(temp3ptr->nextptr!=firstptr)//traversing 
     { 
      cout << temp3ptr->data; 
      cout << endl; 
     } 

^在這裏,你需要一種方法來實際上是通過你的循環鏈表結束,現在它只是打印第一個節點反覆地(好吧,從技術上說,代碼的其他部分不會讓您將第二個節點添加到鏈接列表中)

類似於:

 while(temp3ptr->nextptr!=firstptr)//traversing 
     { 
      cout << temp3ptr->data; 
      cout << endl; 
      // Can technically do it without the if-check since this 
      // is circular, but better to code defensively. 
      if (temp3ptr->next != NULL) { 
       temp3ptr = temp3ptr->next; 
      } 
     } 
+0

在'printit'中有另一個泄漏:'temp3ptr = firstptr;' – 2012-12-13 16:35:01

+0

嗯..這意味着,如果我想分配firstptr的地址,我必須使用firstptr = new Node; 同樣,這意味着我也在insert_at_back函數中犯了同樣的錯誤? –

+0

非常感謝,我編輯的代碼,使其運行,,,如果我將面臨任何問題,我會再次問。 –