2012-04-04 12 views
0

私の "void union"関数では、 "AUB"がvoid関数内にないので、ユーザがそれぞれ入力したリンクリスト "A"と "B"の両方からデータを挿入する方法がわかりません。私は置いただろう: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()'は関数の悪い名前です。意味のあるものに変更する必要があります。 –

+0

詳しいことはありますか?関数の目的はAとBの両方を組み合わせることです – BuzzSmarter

+0

unionは "共用体"を定義するためのキーワードです。これは、大文字だけでキーワードと異なるfunctinonを定義すると誤った仮定につながる可能性があります – Alex

答えて

0

あなたはそれを定義します:void Union(Sets setA,Sets setB)

何をしていますか?どちらも値渡しで、戻り値はvoidです - 結果はどこに行きますか?

現在のオブジェクト(Unionの機能のthis)はそのユニオンになりますか?もしそうなら、すでにそこにあるデータはどうなりますか?あなたは基本的に3つのセットではなく、2をマージしているので、あなたは、私は2つのパラメータとリターン新しいを取るだろう静的merge関数を作成することをお勧め...、

それを削除していませんその2つのマージであるリスト。

それ以外の場合は、通常のmerge関数を作成します。この関数は1つのパラメータしか取らず、現在のオブジェクトにマージします。

ところで - なぜSets、それは明らかにソートされたリンクリストですか?

+0

1.値はsetAとsetBを関数に追加するには、pとqを使用して、それらのブロックと結果がどこにあるのかを指します。 2. 3番目のセットを無視し、削除しました。 3.Setsはorderedlinkedlistよりも短くなっています! – BuzzSmarter

+0

@SeanFlores#1と#2は分かりませんでした。私が書いたのはあなたが投稿したコードです。 Re#3 - * set *は** not ** a * list *です。これらの用語は非常によく定義されており、他の人の目にはないという意味を与えています。そのa * list * - それを* set *と呼んではいけません。 – littleadv