資料結構作業六:Heap Creating, Adjusting and Sorting. (Minimum)
#include<iostream>
#include<time.h>
using namespace std;
int InputDataIntoHeap(int Number, int *Heap);
int AdjustMinHeap(int Pointer, int *Heap);
int SortingHeap(int Pointer, int *Heap, int *TempHeap);
void SwapHeap(int A, int B, int *Heap);
int MinHeap(int Pointer, int *Heap);
int AddKeyValuetoHeap(int r, int Pointer, int *Heap);
void ShowCurrentHeap(int Input, int *Heap);
int MPointer = 0;
int Parent = 0;
int RChild = 0;
int LChild = 0;
int Child = 0;
int pParent = 0;
int OutPort = 0;
int MAXHEAP = 32;
void main(void)
{
int Input;
int Ptr;
int *Heap;
int *TempHeap;
int NewKV;
char Function;
cout << "Please input the number of node: ";
cin >> Input;
Heap = new int[MAXHEAP];
TempHeap = new int[MAXHEAP];
Ptr = InputDataIntoHeap(Input, Heap);
cout << "\nThe orignal ";
ShowCurrentHeap(Input, Heap);
Ptr = AdjustMinHeap(Ptr, Heap);
cout << "\nAfter minimum adjust";
ShowCurrentHeap(Input, Heap);
Ptr = SortingHeap(Ptr, Heap, TempHeap);
cout << "\nAfter Sorting";
ShowCurrentHeap(Input, Heap);
cout << endl;
system("pause");
while(1)
{
system("cls");
ShowCurrentHeap(Input, Heap);
cout << "Add new data(A/a)\nDelete Minimum Data(D/d)\nExit(X/x)\nInput: ";
cin >> Function;
if(Function == 'x' || Function == 'X')
break;
if(Function == 'A' || Function == 'a')
{
cout << "Pleaset Input New data = ";
cin >> NewKV;
Ptr = AddKeyValuetoHeap(NewKV, Ptr, Heap);
if(Input > 32)
Input = 32;
else
Input++;
if(Input > MAXHEAP)
{
cout << "\nHeap is full!\n\n";
}
ShowCurrentHeap(Input, Heap);
system("pause");
}
if(Function == 'D' || Function =='d')
{
if(Input == 0)
{
Input = 1;
cout << "Heap is empty!\n\n";
} else {
Ptr = MinHeap(Ptr, Heap);
cout << "OutPort = " << OutPort << endl;
Input--;
ShowCurrentHeap(Input, Heap);
system("pause");
}
}
}
if(Input !=0)
{
Ptr = SortingHeap(Ptr, Heap, TempHeap);
cout << "\nSorting again.\n";
ShowCurrentHeap(Input, Heap);
cout << endl;
}
delete Heap;
delete TempHeap;
}
Previous in This Category: 超~精簡的VC6 Debug功能簡介 Next in This Category: 資料結構作業六(補充)

