Filename | Circular |
Permission | rw-r--r-- |
Author | pramana |
Date and Time | 4:06 pm |
Label | SDATA Task |
Action |
#include <cstdlib>
using namespace std;
class circle
{
private:
struct Node
{
Node* ulang;
int nil;
Node *lanjutan;
};
Node *pertama;
public :
circle()
{
Node *t1 = new Node();
t1->ulang = NULL;
t1->nil = 3;
t1->lanjutan = NULL;
pertama = t1;
Node *t2 = new Node();
t2->ulang = t1;
t2->nil = 13;
t2->lanjutan = NULL;
t1->lanjutan = t2;
Node *t3 = new Node();
t3->ulang = t2;
t3->nil = 1;
t3->lanjutan = NULL;
t2->lanjutan = t3;
Node *t4 = new Node();
t4->ulang = t3;
t4->nil = 33;
t4->lanjutan = NULL;
t3->lanjutan = t4;
Node *t5 = new Node();
t5->ulang = t4;
t5->nil = 8;
t5->lanjutan = NULL;
t4->lanjutan = t5;
Node *t6 = new Node();
t6->ulang = t5;
t6->nil = 133;
t6->lanjutan = NULL;
t5->lanjutan = t6;
Node *t7 = new Node();
t7->ulang = t6;
t7->lanjutan = t1;
t7->nil = 0;
t7->lanjutan = NULL;
t6->lanjutan = t7;
}
~circle()
{
Node *temp =pertama, *current =pertama;
while(current != NULL)
{
temp = current->lanjutan;
delete current;
current = temp;
}
}
void Tampil()
{
Node *temp;
for(temp =pertama; temp != NULL; temp = temp->lanjutan)
{
cout<<temp->nil<<" , ";
}
cout<<endl;
}
void Sort()
{
Node *current, *cur;
for(current =pertama; current->lanjutan != NULL; current = current->lanjutan)
{
Node *minimum = current;
for(cur = current ; cur != NULL; cur = cur->lanjutan)
{
if(minimum->nil > cur->nil)
{
minimum = cur;
}
}
if(minimum != current)
{
Node *current_lagi, *current_lanjutan, *min_ulang, *min_lanjutan;
// Initialize them
current_lanjutan = current->lanjutan;
min_ulang = minimum->ulang;
min_lanjutan = minimum->lanjutan;
current_lagi = current->ulang;
if(current_lagi == NULL)
{
// Change thepertama Node
pertama = minimum;
}
if(current->lanjutan == minimum)
{
// Nodes are Adjacent
minimum->ulang = current_lagi;
minimum->lanjutan = current;
current->ulang= minimum;
current->lanjutan = min_lanjutan;
if(min_lanjutan)
{
min_lanjutan->ulang= current;
}
if(current_lagi)
{
current_lagi->lanjutan = minimum;
}
}
else
{
minimum->ulang = current_lagi;
minimum->lanjutan = current_lanjutan;
current->ulang = min_ulang;
current->lanjutan = min_lanjutan;
if(current_lanjutan)
{
current_lanjutan->ulang = minimum;
}
if(min_ulang)
{
min_ulang->lanjutan = current;
}
if(min_lanjutan)
{
min_lanjutan->ulang = current;
}
if(current_lagi)
{
current_lagi->lanjutan = minimum;
}
}
current = minimum;
}
}
}
};
int main()
{
circle list;
cout<<"LinkList = ";
list.Tampil();
cout<<"\n\nSort circular likend list \n\n";
list.Sort();
cout<<"LinkList = ";
list.Tampil();
system("PAUSE");
return EXIT_SUCCESS;
}
0 comments:
Post a Comment