Sunteți pe pagina 1din 4

class Node

{
public:
int Value;
Node *Next;
Node()
{
Value=0;
Next=NULL;
}
};

1 of 1
#include <iostream>
#include "Node.h"

using namespace std;

class LinkedList
{
private:
Node *Head;
public:
LinkedList()
{
Head = NULL;
}

bool empty()
{
return (Head==NULL);
}

void add(int item)


{
Node *n = new Node();
n->Value = item;
if (empty())
Head = n;
else
{
Node *c=Head;
while (c->Next) c = c->Next;
c->Next = n;
}
}

void display()
{
Node *c=Head;
cout << "\n{";
while (c)
{
cout << c->Value << ", ";
c = c->Next;
}
cout << "}\n";
}

int first()
{
if (Head!=NULL)
return Head->Value;
else
return -1;
}
void remove(int item)
{
Node *p,*d;
p=d=Head;
while (d->Next)
{
if (d->Value == item) break;
p = d;
d = d->Next;
}

1 of 2
if (d != NULL)
{
if (d==Head)
Head = d->Next;
else
p->Next = d->Next;

delete d;
}
}

};

2 of 2
#include <cstdlib>
#include <iostream>
#include "linkedlist.h"

void QuickSort(LinkedList*);

using namespace std;

int main(int argc, char *argv[])


{
LinkedList list;
int A[]={5,2,6,1,7,2,5,7,2,9,8,7,1,2,4,5,7,1};
int N = sizeof(A) / sizeof(int);
for (int i=0;i<N;i++)
list.add(A[i]);

list.display();
QuickSort(&list);

list.display();
system("PAUSE");
return EXIT_SUCCESS;
}

void QuickSort(LinkedList* List)


{
//List->display();
if (List->empty()) return;
int P = List->first();
int n;
List->remove(P);
LinkedList* S=new LinkedList();
LinkedList* G=new LinkedList();
while (!List->empty())
{
n=List->first();
if (n<0) break;
List->remove(n);
if (n<P)
S->add(n);
else
G->add(n);
}
QuickSort(S);
QuickSort(G);
while (!S->empty())
{
n = S->first();
S->remove(n);
List->add(n);
}
List->add(P);
while (!G->empty())
{
n = G->first();
G->remove(n);
List->add(n);
}
delete S;
delete G;
}

1 of 1

S-ar putea să vă placă și