need help

Discussion in 'C++' started by jw, Dec 6, 2005.

  1. jw

    jw Guest

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


    }
     
    jw, Dec 6, 2005
    #1
    1. Advertising

  2. jw

    mlimber Guest

    jw wrote:
    > 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
     
    mlimber, Dec 6, 2005
    #2
    1. Advertising

  3. "jw" <> writes:

    > 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
     
    Niklas Norrthon, Dec 7, 2005
    #3
  4. jw

    osmium Guest

    "jw" wrote:

    >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.
     
    osmium, Dec 7, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,498
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    779
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    421
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    588
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    525
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page