2012-04-04 70 views
0

在我的「void union」函數中,我不確定如何從用戶分別輸入的鏈接列表「A」和「B」插入數據,因爲「AUB」不在void函數內。我會把:linked list union

AUB.insert 

但我不確定。有什麼建議麼?

#include "stdafx.h" 
#include <iostream> 

using namespace std; 

class Sets 
{ 
private:struct NODE 
     { 
      int info; 
      NODE *next; 
     }; 
     NODE *list; 
public:Sets() 
     { 
      list=NULL; 
     } 
     void Insert(int x) 
     { 
      NODE *p=list, *q=list, *r; 
      //create a new node 
      r = new (NODE); 
      r->info = x; 
      r->next = NULL; 
      //find the insertion place 
      while(p != NULL && p->info < x) 
      { 
       q=p; 
       p=p->next; 
      } 
      if(p==list)//x is the first info 
      { 
       list=r; 
       r->next=p; 
      } 
      else if(p==NULL)//x is the last info 
      { 
       q->next=r; 
      } 
      else //x is neither forst nor last info 
      { 
       r->next=p; 
       q->next=r; 
      } 
     } 
     void display() 
     { 
      NODE *p=list; 
      while(p != NULL) 
      { 
       cout << p->info << "-->"; 
       p=p->next; 
      } 
      cout << "NULL\n"; 
     } 
     void Union(Sets setA,Sets setB) 
     { 
      NODE *p=setA.list, *q=setB.list; 
      while(p != NULL && q != NULL) 
      { 
       if(p->info > q-> info) 
       { 
        (q->info) 
        q=q->next; 
       } 
       else if(p->info == q->info) 
       { 
        insert(p->info) 
        p=p->next; 
        q=q->next; 
       } 
       else//P->info < q->info 
       { 
        insert(p->info); 
        p=p->next; 
       } 
      } 
      while(p !=NULL) 
      { 
       insert(p->info); 
       p=p->next; 
      } 
      while(q != NULL) 
      { 
       insert(q->info); 
       q=q->next; 
      } 
     } 
}; 


int main() 
{ 
    //create a set of integers 
    int x; 
    Sets A, B, setAUB; 
    cout << "Enter data for setA:\n"; 
    cout << "Enter a group of positive integer numbers with -1 at the end end: "; 
    cin >> x; 
    while(x != -1) 
    { 
     A.Insert(x); 
     cin >> x; 
    }; 
    //display setA 
    cout << endl << "setA="; 
    A.display(); 

    cout << "Enter data for setB:\n"; 
    cout << "Enter a group of positive integer numbers with -1 at the end end: "; 
    cin >> x; 
    while(x != -1) 
    { 
     B.Insert(x); 
     cin >> x; 
    }; 
    //display setB 
    cout << endl << "setB="; 
    B.display(); 

    setAUB.Union(A, B); 
    //display setAUB 
    cout << endl << "setAUB="; 
    setAUB.display(); 

    system ("pause"); 

    //terminate program 
    return 0; 
}; 
+2

'void Union()'是一個函數的壞名字。您應該將它改爲有意義的。 – 2012-04-04 09:32:58

+0

你能否詳細說明一下?功能的目的是結合A和B – BuzzSmarter 2012-04-04 09:37:45

+0

聯合是定義「聯合」的關鍵字,當您定義與關鍵字僅用大寫字母相區別的函數時,這可能會導致錯誤的假設 – Alex 2012-04-04 09:46:57

回答

0

您可以定義它:void Union(Sets setA,Sets setB)

你在做什麼?兩者都通過值傳遞,返回值爲void - 結果在哪裏?

您當前的對象(中的Union函數)是否成爲該聯合?如果是這樣,那麼已經存在的數據會發生什麼?你不刪除它,所以你基本上合併三套,而不是兩個......

我建議建立一個靜態merge功能,將採取兩個參數和一個這將是兩者合併的清單。

否則,請創建一個只需要1個參數的常規merge函數,並將其合併到當前對象中。

順便說一句 - 爲什麼Sets,當它顯然是一個排序的鏈表?

+0

1.我已經通過了setA和setB進入函數,我用p和q指向那些塊,這就是結果去的地方。 2.忽略第三組,並刪除它。 3.Sets僅比orderedlinkedlist短! – BuzzSmarter 2012-04-04 09:50:24

+0

@SeanFlores我不明白#1和#2,我寫的是關於你發佈的代碼。 Re#3 - * set *是**不是** a *列表*。這些術語是非常明確的,你給他們意味着他們沒有(在別人眼中)。如果它的a *列表* - 不叫它* set *。 – littleadv 2012-04-04 09:58:58