-1
,所以我寫在那裏用戶填寫與整數兩個相連列出了PROGRAMM,現在我需要創建一個用於創建與第一和第二值,第三鏈接列表的功能列表沒有重複。 這裏是代碼,我現在:功能合併兩個單鏈表,形成第三個
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int info;
node *next;
node (int i, node *n=NULL)
{
info = i;
next = n;
}
~node()
{
cout <<"NODE CONTAINING \"" << info << "\" WAS DELETED!" << endl;
}
};
struct list
{
node* startList1, *lastList1, *startList2, *lastList2;
int menuOption;
int nodeCount1=0, nodeCount2=0;
list() {
startList1 = NULL;
startList2 = NULL;
}
void addList1(node *p)
{
int n;
cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE:";
cin >> n;
p = new node(n);
nodeCount1++;
if(startList1==NULL)
{
startList1 = lastList1 = p;
}
else
{
lastList1->next = p;
lastList1 = p;
}
}
void printList1(node *pr)
{
node *pr;
for (pr=startList1; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
}
void addList2(node *q)
{
int n;
cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE:";
cin >> n;
q = new node(n);
nodeCount2++;
if(startList2==NULL)
{
startList2 = lastList2 = q;
}
else
{
lastList2->next = q;
lastList2 = q;
}
}
void printList2(node *pr)
{
for (pr=startList2; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
}
// this just prints first and second lists to show what is inside..
void printBoth(node *pr, node *qr)
{
cout << "Elements of the first list:" << endl;
for (pr=startList1; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
cout << "Elements of the second list:" << endl;
for (pr=startList2; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
}
void printMenu()
{
cout << "MENU" << endl;
cout << "(1) ADD ELEMENT LIST1." << endl;
cout << "(2) PRINT LIST1" << endl;
cout << "(3) ADD ELEMENT LIST2" << endl;
cout << "(4) PRINT LIST2" << endl;
cout << "(5) PRINT BOTH LISTS" << endl;
cout << "(6) USE MERGE FUNCTION" << endl;
cout << "(7) TO EXIT" << endl;
cin >> menuOption;
system ("cls");
};
void dragons()
{
node *temp1 = startList1;
node *temp2 = startList2;
while(temp1)
{
temp1 = startList1->next;
delete startList1;
startList1=temp1;
}
while(temp2)
{
temp2 = startList2->next;
delete startList2;
startList2=temp2;
}
};
};
int main()
{
struct node *p = NULL, *q = NULL;
list s;
s.printMenu();
node* list1;
node* list2;
node* sorting;
while(s.menuOption!=7)
{
switch (s.menuOption)
{
case 1: s.addList1(list1);
break;
case 2: s.printList1(list1);
break;
case 3: s.addList2(list2);
break;
case 4: s.printList2(list2);
break;
case 5:s.printBoth(list1, list2);
break;
case 6:s.merge();
break;
default: cout << "SOMETHING WENT WRONG!!!!" << endl;
break;
}
system ("pause");
system ("cls");
s.printMenu();
}
s.dragons();
return 0;
}
所以現在用戶可以輸入元素兩份名單,看看他們,但我怎樣才能創建一個函數,將合併這些名單沒有重複的值?例如: List1 = 1,2,3,6,8; List2 = 2,4,5,7,8;
合併列表= 1,2,3,4,5,7,8; (可以是任何實際的順序,沒關係)
任何意見將不勝感激!謝謝!
如果您無法從中檢索數據,鏈接列表有什麼用處?請注意,您的整個'struct list'無法從'main'中添加數據,或者無法從'main'開始迭代。這將是獲得「merge()」正常工作的第一步。 – PaulMcKenzie
對不起,我是新來的C++語言,我不太明白你在說什麼。我應該怎麼做才能「從主要數據中添加數據或從主要數據開始迭代」? –
您構建了一個鏈表類,並且沒有辦法從'list'結構之外的列表中讀取數據。其次,你的'addList1'和'addList2'函數不會改變傳入的參數。你發送的未初始化指針指向'addList1'和'addList2',並且在返回這些函數時它們將保持未初始化狀態。如果你想證明,在調用你的'add ...()'函數之前,將'list1'和'list2'設置爲'nullptr'。這些函數被調用後,你會看到它們仍然是'nullptr'。所以,當你使用'list1'和'list2'時,你的整個'main'都會有bug。 – PaulMcKenzie