我希望输出只是排序后的结果而不是显示所有步骤并在选择2和选择3中得到答案。 这是我的代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node* next;
};
typedef struct node* nodePtr;
nodePtr curr;
nodePtr temp;
nodePtr head;
nodePtr Node;
nodePtr counter;
void addNode(int data);
bool Empty();
struct node* SortedMerge(node* a, node* b);
void display_m(struct node* counter);
void MergeSort(struct node** Head);
void Split(node* givennumber, node** Front, node** Back);
bool Empty() {
return head == NULL;
}
void addNode(int data ){
nodePtr n = new node;
n->next = NULL;
n->data = data;
if(!Empty()){
curr = head;
while(curr->next !=NULL){
curr = curr->next;
}
curr->next = n;
}
else{
head = n;
}
}
void display(){
counter = head;
while(counter != NULL)
{
cout<<counter->data<<" ";
counter = counter-> next;
}
cout<<endl;
}
void display_m(struct node* counter){
while(counter != NULL)
{
cout<<counter->data<<" ";
counter = counter-> next;
}
cout<<endl;
}
void MergeSort(struct node** Head)
{
node* head = *Head;
node* a;
node* b;
if ((head == NULL) || (head->next == NULL))
{
return;
}
Split(head, &a, &b);
MergeSort(&a);
MergeSort(&b);
*Head = SortedMerge(a, b);
}
struct node* SortedMerge(struct node* a, struct node* b)
{
node* result = NULL;
if (a == NULL)
return b;
else if (b==NULL)
return a;
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
display_m(a);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
display_m(b);
}
return result;
}
void Split(node* givennumber, node** Front, node** Back)
{
node* fast;
node* slow;
if (givennumber==NULL || givennumber->next==NULL)
{
*Front = givennumber;
*Back = NULL;
}
else
{
slow = givennumber;
fast = givennumber->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*Front = givennumber;
*Back = slow->next;
slow->next = NULL;
display_m(*Front);
display_m(*Back);
}
}
int main()
{
int i;
char choice;
cout << "Merge Sort Selection: " << endl;
cout << "[1] 20 random numbers" << endl;
cout << "[2] 200 random numbers" << endl;
cout << "[3] 2000 random numbers" << endl;
cin >> choice;
cout << endl;
switch(choice){
case '1':
{
for(int i=0 ; i <20 ; i++){
int random = rand() % 99 ;
addNode(random);
}
cout << "20 Random Numbers: " ;
display();
cout<<"Sorting data: \n";
MergeSort(&head);
cout<< "\nResult: \n";
display_m(head);
system ("pause");
return 0;
}
break;
case '2':
{
for(int i=0 ; i <200 ; i++){
int random = rand() % 99 ;
addNode(random);
}
cout << "200 Random Numbers: " ;
display();
cout<<"Sorting data: \n";
MergeSort(&head);
cout<< "\nResult: \n";
display_m(head);
system ("pause");
return 0;
}
break;
case '3':
{
for(int i=0 ; i <2000 ; i++){
int random = rand() % 99 ;
addNode(random);
}
cout << "2000 Random Numbers: " ;
display();
cout<<"Sorting data: \n";
MergeSort(&head);
cout<< "\nResult: \n";
display_m(head);
system ("pause");
return 0;
}
break;
}
}
我期望选择2和选择3的输出如下: 随机200个数字:“未分类的200个数字” 结果:“排序了200个数字而没有显示步骤”