J
jw
i have a program it has a list of random numbers and my aim is to add
the nodes in another list
the nodes will be in an order at the new link list
#include<iostream>
#include<cmath>
using namespace std;
class Node{
private:
int value;
Node *next;
public:
Node()
{
next=NULL;
}
friend class List;
friend class List1;
};
class List
{
private:
Node *head,*tail,*TOP;
void append(Node *adding);
public:
List *anotherlist;
void sortAll();
void takeElements();
void displayAll();
~List()
{
Node *tmp1,*tmp2;
tmp1=tmp2=head;
while(tmp1)
{
tmp2=tmp1;
tmp1=tmp1->next;
delete tmp2;
}
}
List()
{
TOP=new Node;
head=tail=TOP;
}
};
void List::takeElements()
{
int number;
number=rand()%200;
Node *newnode;
newnode=new Node;
newnode->value=number;
append(newnode);
}
void List::append(Node *adding)
{
if(TOP->next==NULL)
{
TOP->next=adding;
head=tail=TOP->next;
}
else{
tail->next=adding;
tail=tail->next;
}
}
void List::displayAll()
{
Node *starter;
starter=head;
while(starter){
cout<<starter->value<<endl;
starter=starter->next;
}
}
void List::sortAll()
{
int min=head->value;
Node *start=head;
Node *previous;//will be used in else
Node *nodePtr;//will point to head in else
while(tail!=NULL)
{
for(;start!=NULL;start=start->next)
{
if(min>start->value)
{
min=start->value;
}//took minimum
}
for(Node *sec=head;sec!=NULL;sec=sec->next)
{
if(min==sec->value)
{
if(sec==head) {
Node *silincekhead=head;
Node *eklencekNode=new Node;//will be added to second list
eklencekNode->value=head->value;
anotherlist->append(eklencekNode);
head=head->next;
delete silincekhead;
}
if(sec==tail)
{
Node *silincektail=tail;
Node *eklencektail=new Node;
silincektail->value=tail->value;
anotherlist->append(eklencektail);
delete silincektail;//lost the tail
for(Node
*tailgerial=head;tailgerial->next!=NULL;tailgerial=tailgerial->next)
{
tail=tailgerial;//tail is back)
}
}
else/
{
nodePtr=head;
Node *silincekNode;
while(nodePtr!=NULL && nodePtr->value!=min)
{
previous=nodePtr;
nodePtr=nodePtr->next;
}
silincekNode=new Node;
silincekNode->value=nodePtr->value;
anotherlist->append(silincekNode);
previous->next=nodePtr->next;
delete nodePtr;
}
}
}
}
}
void main(){
List thelist;
char yes_no='y';
while(yes_no=='y')
{
thelist.takeElements();
cout<<"add a rand again y/n"<<endl;
cin>>yes_no;
}
thelist.displayAll();
thelist.sortAll();
thelist.anotherlist->displayAll();
}
the nodes in another list
the nodes will be in an order at the new link list
#include<iostream>
#include<cmath>
using namespace std;
class Node{
private:
int value;
Node *next;
public:
Node()
{
next=NULL;
}
friend class List;
friend class List1;
};
class List
{
private:
Node *head,*tail,*TOP;
void append(Node *adding);
public:
List *anotherlist;
void sortAll();
void takeElements();
void displayAll();
~List()
{
Node *tmp1,*tmp2;
tmp1=tmp2=head;
while(tmp1)
{
tmp2=tmp1;
tmp1=tmp1->next;
delete tmp2;
}
}
List()
{
TOP=new Node;
head=tail=TOP;
}
};
void List::takeElements()
{
int number;
number=rand()%200;
Node *newnode;
newnode=new Node;
newnode->value=number;
append(newnode);
}
void List::append(Node *adding)
{
if(TOP->next==NULL)
{
TOP->next=adding;
head=tail=TOP->next;
}
else{
tail->next=adding;
tail=tail->next;
}
}
void List::displayAll()
{
Node *starter;
starter=head;
while(starter){
cout<<starter->value<<endl;
starter=starter->next;
}
}
void List::sortAll()
{
int min=head->value;
Node *start=head;
Node *previous;//will be used in else
Node *nodePtr;//will point to head in else
while(tail!=NULL)
{
for(;start!=NULL;start=start->next)
{
if(min>start->value)
{
min=start->value;
}//took minimum
}
for(Node *sec=head;sec!=NULL;sec=sec->next)
{
if(min==sec->value)
{
if(sec==head) {
Node *silincekhead=head;
Node *eklencekNode=new Node;//will be added to second list
eklencekNode->value=head->value;
anotherlist->append(eklencekNode);
head=head->next;
delete silincekhead;
}
if(sec==tail)
{
Node *silincektail=tail;
Node *eklencektail=new Node;
silincektail->value=tail->value;
anotherlist->append(eklencektail);
delete silincektail;//lost the tail
for(Node
*tailgerial=head;tailgerial->next!=NULL;tailgerial=tailgerial->next)
{
tail=tailgerial;//tail is back)
}
}
else/
{
nodePtr=head;
Node *silincekNode;
while(nodePtr!=NULL && nodePtr->value!=min)
{
previous=nodePtr;
nodePtr=nodePtr->next;
}
silincekNode=new Node;
silincekNode->value=nodePtr->value;
anotherlist->append(silincekNode);
previous->next=nodePtr->next;
delete nodePtr;
}
}
}
}
}
void main(){
List thelist;
char yes_no='y';
while(yes_no=='y')
{
thelist.takeElements();
cout<<"add a rand again y/n"<<endl;
cin>>yes_no;
}
thelist.displayAll();
thelist.sortAll();
thelist.anotherlist->displayAll();
}