#include <iostream>
#include <string>
using namespace std;
class Node
{
private:
int item;
Node *next;
public:
Node();
Node(int value);
Node *getNext() const;
void setNext(Node * nextPtr);
int getItem() const;
void setItem(int value);
};
Node::Node():item(0),next(nullptr)
{}
Node::Node(int value):item(value),next(nullptr)
{}
Node * Node::getNext() const
{
return next;
}
void Node::setNext(Node * nextPtr)
{
next = nextPtr;
}
int Node::getItem() const
{
return item;
}
void Node::setItem(int value)
{
item = value;
}
Node * partition(Node * headPtr,int x)
{
Node * beforStart = nullptr;
Node * afterStart = nullptr;
while(headPtr != nullptr)
{
Node * nextNodePtr = headPtr->getNext();
if(headPtr->getItem() < x)
{
headPtr->setNext(beforStart);
beforStart = headPtr;
}
else
{
headPtr->setNext(afterStart);
afterStart = headPtr;
}
headPtr = nextNodePtr;
}
if(beforStart == nullptr)
return afterStart;
Node * head = beforStart;
while(head->getNext() != nullptr)
head = head->getNext();
head->setNext(afterStart);
return beforStart;
}
void printNodeChain(Node * headPtr)
{
while(headPtr != nullptr)
{
cout<<headPtr->getItem()<<" ";
headPtr = headPtr->getNext();
}
cout<<endl;
}
int main()
{
int arr[6] = {3,8,9,1,2,4};
Node * headPtr = nullptr;
for(int i = 0; i < 6; i++)
{
Node * nodePtr = new Node();
nodePtr->setItem(arr[i]);
nodePtr->setNext(headPtr);
headPtr = nodePtr;
}
printNodeChain(headPtr);
headPtr = partition(headPtr,5);
printNodeChain(headPtr);
return 0;
}