0% found this document useful (0 votes)
24 views4 pages

(Public: Int Value Node Next Node (Value Next NULL ) )

The document defines a Node class with an integer value and pointer to the next node. It also defines a LinkedList class to implement a linked list using these Node objects. The LinkedList class contains methods to add nodes, display the list, get the first node, and remove nodes by value. The main function creates a linked list, calls QuickSort to sort it, and displays the list before and after sorting.

Uploaded by

marryam nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

(Public: Int Value Node Next Node (Value Next NULL ) )

The document defines a Node class with an integer value and pointer to the next node. It also defines a LinkedList class to implement a linked list using these Node objects. The LinkedList class contains methods to add nodes, display the list, get the first node, and remove nodes by value. The main function creates a linked list, calls QuickSort to sort it, and displays the list before and after sorting.

Uploaded by

marryam nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy