need help

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();


}
 
M

mlimber

jw said:
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
[snip]

Bully for you. What's your question, exactly? Note that we won't do
your homework for you:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

Also note that you should use std::list unless you're instructor
requires that you reimplement it for your coursework.

Cheers! --M
 
N

Niklas Norrthon

jw said:
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

Yes... Do you need help with it?

[snip code]

Anyway your problem is on this line:
void main(){

I'll leave as an excercise to correct it.

/Niklas Norrthon
 
O

osmium

jw said:
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

First of all, it is *not* rude to ask a question on this newsgroup. If you
post explicit questions you are somewhat less likely to get wise-ass
answers.

There are sorting facilities built into the language libraries. Is there
some reason you should not use them? You seem to be taking a course of some
kind; we have to know the rules you are subjected to if we are going to
avoid causing you to simply flail about according to the last answer you
looked at.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top