Here is my function which convert linked list in the following order:
Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.
Code:
void SingleList::orderSort3()
{
// If linked list is empty or there is only one node or two nodes in list
if(getHead() == NULL || getHead()->getNext() == NULL || getHead()->getNext()->getNext() == NULL)
return;
// Initialize previous and current pointers
ISingleNode *prev = getHead();
ISingleNode *current = getHead()->getNext()->getNext();
// Change head before proceeeding, because this head will be useful at last.
setHead(current);
// Traverse the list and swap
while(true)
{
ISingleNode *tempNext = current->getNext();
current->setNext(prev->getNext());
prev->getNext()->setNext(prev);
// check remaining node is not empty or single node.
if(tempNext == NULL || tempNext->getNext() == NULL)
{
prev->setNext(tempNext);
break;
}
// check remaining nodes are not only 2.
if(tempNext->getNext()->getNext() == NULL)
{
prev->setNext(tempNext->getNext());
prev = tempNext;
current = tempNext->getNext();
}
else // 3 nodes are available.
{
prev->setNext(tempNext->getNext()->getNext());
prev = tempNext;
current = tempNext->getNext()->getNext();
}
}
}
This is my number of elements in the linked list:
8388607
14348907
Please help me to optimize this code. If you require full code then let me know and I will add more. But I guess this is the main()
function, which reverses the linked list. It takes only a head pointer as an argument and it will sort without returning. This is what I want, but it takes too much time.
Does it depend on our laptop configuration and process as well?
Full source :
#pragma once
#include <cstdlib>
#include <iostream>
struct ISingleNode
{
ISingleNode() {}
virtual ~ISingleNode() {}
virtual void setValue(int value) = 0;
virtual int getValue() = 0;
virtual ISingleNode * getNext() = 0;
virtual void setNext(ISingleNode * next) = 0;
};
struct ISingleList
{
ISingleList() {};
virtual ~ISingleList() {};
virtual ISingleNode * getHead() = 0;
virtual void setHead(ISingleNode * head) = 0;
virtual void addHead(int value) = 0;
virtual void orderSort2() = 0;
virtual void orderSort3() = 0;
};
class SingleList : public ISingleList
{
public:
SingleList() {};
~SingleList() {};
ISingleNode *getHead();
void setHead(ISingleNode * head);
void addHead(int value);
void orderSort2();
void orderSort3();
ISingleNode* SingleList::reverse(ISingleNode *head);
private:
ISingleNode *head_;
};
class SingleNode : public ISingleNode
{
public:
SingleNode() : next_(NULL), value_(0) {}
~SingleNode() {}
void setValue(int value);
int getValue();
ISingleNode * getNext();
void setNext(ISingleNode * next);
private:
ISingleNode* next_;
int value_;
};
void SingleNode::setValue(int value)
{
value_ = value;
}
int SingleNode::getValue()
{
return value_;
}
ISingleNode * SingleNode::getNext()
{
return next_;
}
void SingleNode::setNext(ISingleNode * next)
{
next_ = next;
}
ISingleNode *SingleList::getHead()
{
return head_;
}
void SingleList::setHead(ISingleNode * head)
{
head_ = head;
}
void SingleList::addHead(int value)
{
ISingleNode * currentNode = new SingleNode();
currentNode->setValue(value);
currentNode->setNext(head_);
head_ = currentNode;
}
void SingleList::orderSort2()
{
// If linked list is empty or there is only one node in list
if(getHead() == NULL || getHead()->getNext() == NULL)
return;
// Initialize previous and current pointers
ISingleNode *prev = getHead();
ISingleNode *current = getHead()->getNext();
ISingleNode *tempNext;
// Change head before proceeeding, because this head will be useful at last.
setHead(current);
while(true)
{
tempNext = current->getNext();
current->setNext(prev);// Change next of current as previous node
// If next NULL or next is the last node
if(tempNext == NULL || tempNext->getNext() == NULL)
{
prev->setNext(tempNext);
break;
}
// Change next of previous to next next
prev->setNext(tempNext->getNext());
// Update previous and curr
prev = tempNext;
current = prev->getNext();
}
}
void SingleList::orderSort3()
{
// If linked list is empty or there is only one node or two nodes in list
if(getHead() == NULL || getHead()->getNext() == NULL || getHead()->getNext()->getNext() == NULL)
return;
// Initialize previous and current pointers
ISingleNode *prev = getHead();
ISingleNode *current = getHead()->getNext()->getNext();
// Change head before proceeeding, because this head will be useful at last.
setHead(current);
// Traverse the list and swap
while(true)
{
ISingleNode *tempNext = current->getNext();
current->setNext(prev->getNext());
prev->getNext()->setNext(prev);
if(tempNext == NULL || tempNext->getNext() == NULL)
{
prev->setNext(tempNext);
break;
}
if(tempNext->getNext()->getNext() == NULL)
{
prev->setNext(tempNext->getNext());
prev = tempNext;
current = tempNext->getNext();
}
else
{
prev->setNext(tempNext->getNext()->getNext());
prev = tempNext;
current = tempNext->getNext()->getNext();
}
}
}
int main()
{
SingleList single_list;
for(int i = 8388607 - 1; i >= 0; --i)
{
int value = i;
single_list.addHead(i);
}
single_list.orderSort3();
}