Implementing singly linked list in c++ VS 2013

Hi
I need to make singly and doubly linked list classes that can insert elements. Once the lists are created, I need to order the linked list elements according to a certain pattern.

order2 pattern:
input: 0 1 2 3 4 5 6 7
output: 1 0 3 2 5 4 7 6

order3 pattern:
input: 0 1 2 3 4 5 6 7 8 9 10 11
output: 2 1 0 5 4 3 8 7 6 11 10 9

sequence order pattern:
input: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
output: 1 0 4 3 2 8 7 6 5 13 12 11 10 9

reverse pattern:
input: 0 1 2 3 4 5 6 7 8 9
output: 9 8 7 6 5 4 3 2 1 0

My instructor has given the description of the required classes in a file interfaces01.h as:

#pragma once

#include <cstdlib>

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;
virtual void sequenceOrderSort() = 0;
virtual void reverse() = 0;
};

struct IDoubleNode {
IDoubleNode() {}
virtual ~IDoubleNode() {}
virtual void setValue(int value) = 0;
virtual int getValue() = 0;
virtual IDoubleNode * getPrev() = 0;
virtual IDoubleNode * getNext() = 0;
virtual void setPrev(IDoubleNode * prev) = 0;
virtual void setNext(IDoubleNode * next) = 0;
};

struct IDoubleList {
IDoubleList() {}
virtual ~IDoubleList() {}
virtual IDoubleNode * getHead() = 0;
virtual IDoubleNode * getTail() = 0;
virtual void setHead(IDoubleNode * head) = 0;
virtual void setTail(IDoubleNode * tail) = 0;
virtual void addSorted(int value) = 0;
virtual void orderSort2() = 0;
virtual void orderSort3() = 0;
virtual void sequenceOrderSort() = 0;
virtual void reverse() = 0;
};

I have created the files singlenode.h,singlenode.cpp, singlelist.h,singlelist.cpp in which I am inplementing these classes.

singlenode.h

#ifndef SINGLENODE_H
#define SINGLENODE_H
#include <Interfaces01.h>

struct SingleNode : public ISingleNode
{
public:
SingleNode(){ _next = NULL; }
virtual ~SingleNode() { delete _next; }
virtual void setValue(int value);
virtual int getValue();
virtual ISingleNode * getNext();
virtual void setNext(ISingleNode * next);
private:
int _value;
ISingleNode * _next;
};
#endif

singlenode.cpp

#include <SingleNode.h>

void SingleNode::setValue(int value)
{
_value = value;
}

int SingleNode::getValue()
{
return _value;
}

ISingleNode * SingleNode::getNext()
{
return _next;
}

void SingleNode::setNext(ISingleNode * next)
{
_next = next;
}

Singlelist.h

#ifndef Singlelist_H
#define Singlelist_H
#include "Interfaces01.h"
struct SingleList : public ISingleList
{
public:
SingleList(){ _head = NULL; }
virtual ~SingleList() { delete _head; }
virtual ISingleNode * getHead();
virtual void setHead(ISingleNode * head);
virtual void addHead(int value);
virtual void orderSort2();
virtual void orderSort3();
virtual void sequenceOrderSort();
virtual void reverse();
private:
ISingleNode * _head;
};
#endif

Singlelist.cpp

#include <Singlelist.h>
#include <SingleNode.h>
#include <Interfaces01.h>

ISingleNode * SingleList::getHead()
{
return _head;
}

void SingleList::setHead(ISingleNode * head)
{
_head = head;
}

void SingleList::addHead(int value)
{
SingleNode * curr;
curr = new SingleNode();
curr->setValue(value);
curr->setNext(_head);
_head = curr;
}
void SingleList::orderSort2()
{
ISingleNode * curr = _head;
ISingleNode * temp = _head;
ISingleNode * last = new SingleNode();
curr = curr->getNext();
temp->setNext(curr->getNext());
curr->setNext(temp);
_head = curr;
last = temp;
curr = temp->getNext();
int count = 0;
while (curr != NULL)
{
if (count == 1)
{
temp->setNext(curr->getNext());
curr->setNext(temp);
last->setNext(curr);
last = temp;
curr = temp->getNext();
count = 0;
continue;
}
temp = curr;
curr = curr->getNext();
count = 1;
}
}
void SingleList::orderSort3()
{
ISingleNode * curr = _head;
ISingleNode * first = _head;
ISingleNode * second = _head->getNext();
ISingleNode * last = NULL;
curr = second->getNext();
first->setNext(curr->getNext());
curr->setNext(second);
second->setNext(first);
_head = curr;
last = first;
curr = first->getNext();
int count = 1;
ISingleNode * seq_head = curr;
ISingleNode * seq_tail = curr;
curr = curr->getNext();
while (curr != NULL)
{
if (count < 3 && count > 0)
{
seq_tail->setNext(curr->getNext());
last->setNext(curr);
curr->setNext(seq_head);
seq_head = curr;
count++;
curr = seq_tail->getNext();
continue;
}
if (count == 3)
{
last = seq_tail;
seq_head = curr;
seq_tail = curr;
count = 1;
curr = curr->getNext();
continue;
}
}
}
void SingleList::sequenceOrderSort()
{
ISingleNode * seq_head = _head;
ISingleNode * seq_tail = NULL;
ISingleNode * curr = _head;
ISingleNode * last = NULL;
ISingleNode * temp = curr->getNext();
curr->setNext(temp->getNext());
temp->setNext(curr);
_head = temp;
seq_head = curr->getNext();
last = curr;
curr = seq_head;
int seq_size = 3;
int count = 1;
seq_head = curr;
seq_tail = curr;
curr = curr->getNext();
while (curr != NULL)
{
if (count < seq_size && count > 0)
{
seq_tail->setNext(curr->getNext());
last->setNext(curr);
curr->setNext(seq_head);
seq_head = curr;
curr = seq_tail;
count++;
curr = curr->getNext();
continue;
}
if (count == seq_size)
{
last = seq_tail;
seq_head = curr;
seq_tail = curr;
count = 1;
seq_size++;
curr = curr->getNext();
continue;
}
}
}

void SingleList::reverse()
{
ISingleNode * curr = _head;
while (curr->getNext() != NULL)
{
ISingleNode * temp = curr->getNext();
curr->setNext(temp->getNext());
temp->setNext(_head);
_head = temp;
}
}

However when I am compiling these files in vs 2013 I am getting the error: cannot open include file Singlenode.h,Singlelist.h.
Also I am not too sure about the sorting and reverse logic. Kindly explain how can I implement these logics.

Please help me...
Don't know but I can give you advice on how to post and make others that have the potential to help you a lot happier...

Put your code between these here ->> (code][/code) substitute the parenthesis for opening bracket ---> [ and closing ]

or look for the button under Format: looks like this <>
Last edited on
Topic archived. No new replies allowed.