Having a problem with the link list program . have failed to resolve.worked over 12 hours but the solution looks pretty disorganised an incoplete, please advise
thanksInline Code Example Here

        `        #include <iostream>`
                #include <stdlib.h>
                #include<list>
                using namespace std;


        class Node
        {
        public:
        int get() { return object; };
        void set(int object) { this->object = object; };
        Node * getNext() { return nextNode; };
        void setNext(Node * nextNode) { this->nextNode = nextNode; };


    int object;
    int number;
    Node * nextNode;
    };


    class List
    {
    public:
    List();
    void add (int addObject);
    int get();
    bool next();
    friend void traverse(List list);
    void create();
    friend List addNodes(int number);
     void split(int c, List list, List largeList, List smallList);// this the prime issue
    void differenceList(List listA, List listB,int Num);//this is the prime issue



    private:
        int number;
    int size;
    Node * headNode;
    Node * currentNode;
    Node * lastCurrentNode;
    };
    /* Constructor */
    List::List()
    {
    headNode = new Node();
    headNode->setNext(NULL);
    currentNode = NULL;
    lastCurrentNode = NULL;
    int number=0;
    size = 0;
    }





    void List::create()// this is the bad way out to get some output, and some manipulation of loop to get result of spliting
    //which does not allow user to select a piviot point it just cuts it in half and if list is 21 then there is a problem
    //more over the input to split is not from the size of list but just users visual observation
    {
        List mylist;

        int Num;
     cout<<"Please enter the value for this list";
     cin>>Num;
        for(int i=1; i<= Num; i++){  int j;  for(j=0; j< i; j++)  {   if(i%2!=0)  {mylist.add(i); break;  }}
        // Check if loop ended in normal way or by break statement
        // Print No
        if (i == j) {   cout <<"["<< i << "] ";     }}
        traverse(mylist);

    cout<<"Please select the piviot point of your list";
    int piv;
    cin>>piv;
    if(Num>Num/2||Num<Num/2)  {cout<<"the value of the piviot shoule be exactly half of the List Node size";

     cout<<"Please select the piviot point of your list";
     cin>>Num; }
    List hislist;
    List ob;


        for(int i=1; i<=Num/2; i++) {    int j;  for(j=0; j< i; j++)  {       if(i%2!=0)  {       hislist.add(i);     break;      }   }
        // Check if loop ended in normal way or by break statement
        if (i == j)     {   cout <<"["<< i << "] ";     }}   traverse(hislist);

        List emlist;
        for(int i=1; i<=Num/2 ; i++)
            {
        int j;
        for(j=0; j< i; j++)
        {
            if(i%2!=0)      // If not prime break loop
            {
                emlist.add(i);
                break;  
            }
        }
        if (i == j)         // Check if loop ended in normal way or by break statement
        {
        cout <<"["<< i << "] ";   // Print No
       }}
     traverse(emlist);      
    }
    /* add() class method */
    void List::add (int addObject)
    {
    Node * newNode = new Node();
    newNode->set(addObject);
    if( currentNode != NULL )
    {
    newNode->setNext(currentNode->getNext());
    currentNode->setNext( newNode );
    lastCurrentNode = currentNode;
    currentNode = newNode;
    }
    else
    {
    newNode->setNext(NULL);
    headNode->setNext(newNode);
    lastCurrentNode = headNode;
    currentNode = newNode;
    }
    size ++;
    }
    /* get() class method */
    int List::get()
    {
    if (currentNode != NULL)
    return currentNode->get();
    }
    /* next() class method */
    bool List::next()
    {
    if (currentNode == NULL) return false;
    lastCurrentNode = currentNode;
    currentNode = currentNode->getNext();
    if (currentNode == NULL || size == 0)
    return false;
    else
    return true;
    }
    /* Friend function to traverse linked list */
    void traverse(List list)
    {
    Node* savedCurrentNode = list.currentNode;
    list.currentNode = list.headNode;
     cout << "\n\n Elements of list are given below ";
    for(int i = 1; list.next(); i++)
    {
    cout << "\n Element " << i << " " << list.get();
    }
    cout << "\n List size = " << list.size <<'\n';
    int *ptr;
    ptr=&list.size;
    list.currentNode = savedCurrentNode;
    }

    void List:: split(int c, List list, List largeList, List smallList)  {   }


     void List:: differenceList(List listA, List listB, int Num){

        cout<<"Please enter the data in ListA";//not working at all
        cin>>Num;

            for(int i=1; i<=Num/2; i++)
            {int j;
            for(j=0; j< i; j++)
            {
            if(i%2!=0)      // If not prime break loop
            {
                listA.add(i);
                break;  
            }
            }
            if (i == j)         // Check if loop ended in normal way or by break statement
            {
            cout <<"["<< i << "] ";   // Print No
            }}
            traverse(listA);

            cout<<"Please eneter the data for List B";

            listB;
            for(int i=1; i<=Num/2 ; i++)
            {
            int j;
            for(j=0; j< i; j++)
            {
            if(i%2!=0)      // If not prime break loop
            {
                listB.add(i);
                break;  
            }
            }
            if (i == j)         // Check if loop ended in normal way or by break statement
            {
            cout <<"["<< i << "] ";   // Print No
           }}
           traverse(listB);





     }



   int  main()
    {



            cout<<"\n \n********this program is about functions*********\n of singly linked list\n \n";
        cout<<"Manu of the program:\n(1) for creating a list of Odd numbers";
        cout<<"\n(2) for spliting the List \n(3 for finding the set difference\n \n";


                List mylist;
        List hislist;
        int choice;
        cin>>choice;
        switch (choice)//switch failed
        {
            case 1:
            mylist. create();
            break;
            case 2:
                    mylist.create();
                    hislist.create();
                break;

                case 3:

                cout<<"case 3";
                    List ob ;
                    List oc;
                    ob.create();
                    ob.create();
                    //oc.create();





                    break;










        }
        }



//   

Recommended Answers

All 4 Replies

You post a chunk of code over 250 lines long, with inconsistent indentations and bracing... :-( Good luck with that! You'd get an 'F' in my class just for appearance and readability.

I'd have to echo Rubberman!

The first thing I'd suggest is clean up your indentation and your general code-formatting, it's all over the place. As your code currently stands, it's pretty unreadable. Nobody is going to try and read it in its current state.

Secondly, I recommend putting your classes into separate .h/.cpp files and then including them in your main program. Having the program broken up into smaller, logical units is far better than having a monolithic block of code like the one you posted above. It will make your code and your logic much easier to read and understand!

Thanks for your advice.
It is an eye opener for me.
Will repost it again after correcting it
Thanks.

Sir,
i am new in programming and this is the best that I could do
regards.

#include <iostream>
#include <stdlib.h>
#include<conio.h>
#include <list>
using namespace std;
/////////////////////////////////////////////Node.h//////////////////////////////////////////////////////    
    class Node
    {
    public:
    int get() { return object; };
    void set(int object) { this->object = object; };
    Node * getNext() { return nextNode; };
    void setNext(Node * nextNode) { this->nextNode = nextNode; };
    private:
    int object;
    Node * nextNode;
    };



//////////List.h/////////////////////////////////////////////////////// 
    class List
    {
    public:
    List();
    void add (int addObject);
    int get();
    bool next();
    friend void traverse(List list);
   // friend List addNodes(int number);
    friend void split(int c, List list, List largeList, List smallList);// this function is written with flukes
    friend void differenceList(List listA, List listB);

    private:
    int size;
    Node * headNode;
    Node * currentNode;
    Node * lastCurrentNode;
    };
////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////List.cpp/////////////////////////////////////////////////    
    /* Constructor */
    List::List()
    {
    headNode = new Node();
    headNode->setNext(NULL);
    currentNode = NULL;
    lastCurrentNode = NULL;
    size = 0;
    }
    /* add() class method */
    void List::add (int addObject)
    {
    Node * newNode = new Node();
    newNode->set(addObject);
    if( currentNode != NULL )
    {
    newNode->setNext(currentNode->getNext());
    currentNode->setNext( newNode );
    lastCurrentNode = currentNode;
    currentNode = newNode;
    }
    else
    {
    newNode->setNext(NULL);
    headNode->setNext(newNode);
    lastCurrentNode = headNode;
    currentNode = newNode;
    }
    size ++;
    }
    /* get() class method */
    int List::get()
    {
    if (currentNode != NULL)
    return currentNode->get();
    }
    /* next() class method */
    bool List::next()
    {
    if (currentNode == NULL) return false;
    lastCurrentNode = currentNode;
    currentNode = currentNode->getNext();
    if (currentNode == NULL || size == 0)
    return false;
    else
    return true;
    }
    /* Friend function to traverse linked list */
    void traverse(List list)
    {
    Node* savedCurrentNode = list.currentNode;
    list.currentNode = list.headNode;
     cout << "\n\n Elements of list are given below ";
    for(int i = 1; list.next(); i++)
    {
    cout << "\n Element at node [" << i << "] " << list.get();
    }
    cout << "\n List size =[ " << list.size<<"]" <<'\n';
    list.currentNode = savedCurrentNode;
    }

    void split(int c, List list, List largeList, List smallList){

    int pivot = 0; 
    int pivotFlag = 0;

    for (int i = 1; i < c; i++){
        if (i % 2 == 1)
            list.add(i);
    }
    traverse(list);

    while (pivotFlag == 0) {
        cout << "Noe Enter The Split Point (pivot)--->[ ";
        cin >> pivot;
        list.currentNode = list.headNode;

        while (list.next()) {
            if (list.get() < pivot) {
                pivotFlag =1;
            }
        }
        if (pivotFlag ==0) {
            cout << "Note: Pivot element should be from the list \n" ;
            cout << "and should be less than the maximum number of list, \n \n" << endl;
            cout << " so please choose a correct pivot element. " << endl;
        }
    }

    list.currentNode = list.headNode;

    while (list.next()) {
        if (list.get() <= pivot) {
            smallList.add(list.get());
        } else {
            largeList.add(list.get());
        }
    }


    traverse(largeList);
    traverse(smallList);

    cout << "[[[[[[[[[[[[[<>]]]]]]]]]]]]]\n";
    // Write a Call to the Main Menu.
}


    List addNodes(int number)
    {
         //not used
    }   

void differenceList(List listA, List listB){

List setDifference;
int ListASize, ListBSize;
setDifference;

cout << "Please enter the data fro the Nodes of LIST A : ";
cin >> ListASize;
cout<<"Thanks\n";

for (int i = 1; i < ListASize; i++){
if (i % 2 == 1)
listA.add(i);
}
cout << "Please enter the data fro the Nodes of LIST B ";
cin >> ListBSize;

if(ListBSize>ListASize)
{
cout<<"List B has to be smaller than Lsit A (Program Exception)\n Please renter Lit B input  ";
cin >> ListBSize;
}
cout<<"Thanks\n";
for (int j = 1; j < ListBSize; j++){
if (j % 2 == 1)
listB.add(j);
}
traverse(listA);
traverse(listB);
listA.currentNode = listA.headNode;
listB.currentNode = listB.headNode;

for (int k = 1; k <= listB.size; k++){
listA.next();    
}
while (listA.next()/listB.next()==listB.next()||listB.next()/listA.next()==listA.next()) {
setDifference.add(listA.get());
setDifference.add(listB.get());

f(listA.get()-listB.get()!=0)
{
listA.get()==listB.get();
cout<<"May be there is a difference["<<listA.get()<<"]";
//cout<<"May be there is a difference["<<listB.get()<<"]";
//setDifference.add(listB.get());
}
cout<<endl;
}
}


main () {

List list, largeList, smallList;
List listA, listB;
int maximumListNumber = 0;

int choice = 0;
cout << "[MANU] " << endl;
cout << "[1] To Split List: " << endl;
cout << "[2] To Evaluate Differenve Of List 1 List 2: " << endl;
cout << "[3] Exit Program "; 
cout<<"Please Enter Your Choice -->[";
cin >> choice;


switch (choice)
{
    case 1:
    cout << "\n This Linked List is composed of ODD numbers " << endl;
    cout << "Please Enter the number that you want to transform --->[ ";      
    cin >> maximumListNumber;
    split(maximumListNumber, list, largeList, smallList);
    break;

    case 2:
    differenceList(listA, listB);
    break;
    default:
    cout<<"Invalid Input please consult the manu";

}
///////////////////////////main//////////////////////////////////
// the while loop was not working so duplicate Switch statement was used to repeat program once
cout << "[MANU] " << endl;
    cout << "[1] To Split List: " << endl;
    cout << "[2] To Evaluate Differenve Of List 1 List 2: " << endl;
    cout << "[3] Exit Program "; 
    cout<<"Please Enter Your Choice -->[";
    cin >> choice;


switch (choice)
{



    case 1:
    cout << "\n This Linked List is composed of ODD numbers " << endl;
    cout << "Please Enter the number that you want to transform --->[ ";      
    cin >> maximumListNumber;
    split(maximumListNumber, list, largeList, smallList);
    break;

    case 2:

    differenceList(listA, listB);
    break;
    default:
    cout<<"Invalid Input please consult the manu";
    break;
}
system("pause");
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.