Write a C++ function that starts with a single linked list of items and a specia
ID: 3667880 • Letter: W
Question
Write a C++ function that starts with a single linked list of items and a special value called the splitting value. Two item values can be comparred using the < operator- but the items of the original linked list are in no particular order. The procedure divides the nodes into two linked lists: one containing alll the nodes that contain an item less than the splitting value, and one that contains all the other nodes. If the original linked list had any repeated items integers (i.e, any two or more nodes with the same item in them) then the new linked list that has this item should have the same number of nodes that repeat this item. It does not matter whether you preserve the original linked list or destroy it in the process of building the two new lists, but your comments should documents what happens to the original linked list.
PLEASE ONLY USE THE NODE CLASS WHEN CREATING THE FUNCTION.
DO NOT CREATE ANY OTHER CLASSES AND DO NOT CREATE A STRUCT FOR THE NODE. CREATE THE LINKED LIST FROM THE NODE HEADER FILE.
WRITE THE ENTIRE FUNCTION THE MAIN FILE.
ALSO CREATE A TEST PROGRAM TO MAKE SURE FUNCTION RUNS CORRECTLY.
node1.h: http://pastebin.com/TRp1xXrt
node1.cpp: http://pastebin.com/mYwFk4Ne
Explanation / Answer
// File: newmain.cc
// Author: HARE KRISHNA
// Created on 14 February, 2016, 8:48 PM
#include<stdio.h>
#include<iostream.h>
class node {
int val,x;
node *nxt;
public:
node(int v): val(v), nxt(NULL) {}
node(int v, node *n): val(v), nxt(n) {}
node *next()
{
return nxt;
}
void next(node *n)
{
nxt = n;
}
int & operator <(int v,int x){
if(v <x){
return v
}
}
};
class SplittwoList
{
public:
static node *insert(node **begin, int value)
{
node *me = new node(value);
if ( (me == NULL) || (begin == NULL))
return me;
if ( *begin == NULL )
*begin = me;
else
last(*begin)->next(me);
return me;
}
static bool remove(node **begin, int value)
{
if ( (begin == NULL) || (*begin == NULL))
return false;
node *me = *begin, *prev = NULL;
while(me) {
if ( (*me)() == value )
break;
prev = me;
me = me->next();
}
if ( *begin == me )
*begin = me->next();
if ( prev && me )
prev->next(me->next());
delete me;
return (me != NULL);
}
static node find(node *me, int value)
{
while(me) {
if ( (*me)() == value )
break;
me = me->next();
}
return me;
}
static node *split(node *me)
{
if ( (me == NULL) || (me->next() == NULL))
return NULL;
node *nme = me->next();
node *sec = nme;
while(nme) {
me->next(nme->next());
me = nme;
nme = nme->next();
}
return sec;
}
static void print(node *me)
{
while(me) {
std::cout << (*me)();
me = me->next();
if ( me ) std::cout << " -> ";
}
std::cout << "--|" << std::endl;
}
static node* last(node *begin)
{
node *theone = NULL;
if (begin == NULL) return theone;
theone = begin;
while(theone->next() != NULL)
{
theone = theone->next();
}
return theone;
}
};
int main()
{
node *first = NULL;
// for static intilization
//int input[] = { 1,2,3,4,5,6,7};
//dynnamic intialization
int input[];
cout<<"enter values;"
for(int i=0;i<30;i++)
cin>>input[i];
for(int i = 0 ; i < sizeof(input)/sizeof(input[0]); ++i)
SplittwoList::insert(&first,input[i]);
std::cout << "Input: " << std::endl;
SplittwoList::print(first);
node *second = SplittwoList::split(first);
std::cout << "Output: " << std::endl;
SplittwoList::print(first);
SplittwoList::print(second);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.