-2
class linkedlist{
     class listNode{
          int data;
          listNode* next;
            void listNode(int data,listNode* next){
            this->data=data;
            this->next=next;
        }
};

int main(){
  // in here I want to creat a listnode, but i dont know how to do it

  insert(listNode,5);
}

I need to call the listnode in main() function, I know I can use linkedlist list; and then list.listNode, but I need a node in order to fill in the insert() function.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Rex Huang
  • 1
  • 2
  • 2
    Please make a [mre], including a detailed description of misbehaviour, full verbatim quotes of any error or warning you might get (as text). – Yunnosch Feb 03 '20 at 20:56
  • "_i know i can use linkedlist list; and then list.listNode_" Did you try it? If you did, you would know, that it wouldn't work either. – Algirdas Preidžius Feb 03 '20 at 20:56
  • The nested class is part of the outer class so you need a bit more scope resolution, `linkedlist::listNode`, to correctly identify the nested class. You also need to sort out the naming of the `listNode` function. Is it supposed to be a constructor? And when you call it, you need to provide parameters. I strongly recommend consulting [a good set of reference materials](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is not a good language to try to learn by Internet or trial and error. – user4581301 Feb 03 '20 at 20:57
  • i was asked by my constructor doing like this. a linkedlist class {and a listnode class and a constructLL(..)} , and int the main function i need to creat a listhead, and constructLL(listhead) – Rex Huang Feb 03 '20 at 21:04

3 Answers3

1

You can simply create a listNode variable like below

int main(){
linkedlist::listNode aNode(5, NULL);
// or
linkedlist::listNode *pNode = new linkenlist::listNode(5,NULL);
....

}

idris
  • 488
  • 3
  • 6
  • thanks, that help me a lot, but I have one thing left, i use "linkedlist:: listNode listHead(5,null) in main class, and now ,i want to insert(listHead,10), so what I wrote is void insert(listNode* listHead,int n), but it says insert was not declared, I know now listNode* listHead might be wrong, but i dont know how to fix it – Rex Huang Feb 04 '20 at 14:40
  • You can define insert function like void insert(linkedlist::listNode *listHead, int n) { // find last node of list linkedlist::listNode *lastNode = listHead; while (lastNode->next != null) lastNode = lastNode->next; // append new node to end of list lastNode->next = new linkedlist:: listNode(n, NULL); } – idris Feb 04 '20 at 14:54
  • i tried this way, it still says insert was not declared, the insert function is inside the linkedlist class – Rex Huang Feb 04 '20 at 14:58
  • You can move it to out of class than you can use it. The answer given by @jul3x is more convenient and complete solution. – idris Feb 04 '20 at 15:04
  • well, I cant. My professor required sheet said the insert has to be inside linkedlist class – Rex Huang Feb 04 '20 at 15:06
  • so look at @jul3x solution given below. Isnt it suitable for you? – idris Feb 04 '20 at 15:07
  • I dont think I can do that way, because the insert function has to be like insert(listHead,number) – Rex Huang Feb 04 '20 at 15:11
  • add this method to linkedlist class void insert(listNode* listHead, int number) { linkedList::listNode newNode = new linkedList::listNode(number); newNode->next = listHead->next; listHead->next = newNode; } – idris Feb 04 '20 at 15:23
  • newNode->next won't pass it, and the listhead in main class is linkedlist:: listnode listhead, if we use it to insert(listhead,5), we cannot just set insert(listNode* listhead or linkedlist:: listNode listhead) – Rex Huang Feb 04 '20 at 15:37
  • sorry this should be like that void insert(linkedList::listNode* listHead, int number) – idris Feb 04 '20 at 15:44
  • i just post my code here and my professor reqiurement, the only problem is still the constructLL in main function, keep saying constructLL is could not be resolved – Rex Huang Feb 04 '20 at 15:56
  • You need a linkedlist instance. in main function uncomment linkedlist list; than use list.constructLL(). – idris Feb 05 '20 at 06:45
0

listNode is a class defined inside linkedList class. This means that you need to enter appropriate namespace before usage of described symbol. Also, you need to call constructor of your desired class. To be honest, you better should read some C++ tutorial, because your example has more errors.

Valid very simple implementation would be:

#include <iostream>

class linkedList {
public:
    class listNode {
    public:
        int data;
        listNode* next;

        listNode(int data_) : data(data_), next(nullptr) {}
    };

    linkedList() : head(nullptr), tail(nullptr) {}

    void insert(listNode* new_node) {
        if (head == tail && tail == nullptr) {
            head = tail = new_node;
        }
        else {
            tail->next = new_node;
            tail = new_node;
        }
    }

    listNode* getFront() { return head; };

    listNode* getBack() { return tail; };

    listNode* head;
    listNode* tail;
};

int main(){
    linkedList list;
    list.insert(new linkedList::listNode(5));
    list.insert(new linkedList::listNode(6));
    list.insert(new linkedList::listNode(7));

    std::cout << list.getFront()->data << std::endl; // 5
    std::cout << list.getBack()->data << std::endl;  // 7
    return 0;
}
jul3x
  • 16
  • 4
  • `list.insert(linkedlist::listNode(5));` won't compile because `insert()` is expecting a `listNode*` pointer but you are giving it a `listNode` instance instead. You would need to use `list.insert(new linkedlist::listNode(5));` , but now you have to deal with proper dynamic memory management, the Rule of 3/5/0, etc. `insert()` should take just an `int` as input and create a new node for it, the caller should not be creating nodes directly – Remy Lebeau Feb 04 '20 at 00:02
  • Yeah, I just modified code. But for memory management - if list is created like this - it does not matter - let the OS take care of cleaning up. – jul3x Feb 04 '20 at 00:07
  • well, thanks for correct me, but the point i confused is my instructor ask me to get something like this. A linkedlist class { a listNode class { data,next} and constructLL(listNode* listHead)} which here is ok for me, but in main function, she ask me,get a new listnode which is listHead,and using the function constructLL(listHead); which is confused me – Rex Huang Feb 04 '20 at 03:59
0

enter image description here

enter code here
class linkedlist{
public:
class listNode{
public:
        int data;
        listNode* next;
        listNode(int data,listNode* next){
            this->data=data;
            this->next=next;
        }
        void printNode(listNode* node,ofstream& outFile){
            outFile<<"( "<<node->data<<","<<node<<","<<node->next<<","<<node->next- 
 >data<<")"<<endl;
        }
};
//  listNode* listHead;
//  listNode* listHead(){
//      return new listNode(-9999,NULL);
//  }
listNode* findSpot(listNode* listHead,listNode* newNode){
    listNode* spot=listHead;
    while(spot->next!=NULL && spot->next->data<newNode->data)
        spot=spot->next;
    return spot;
}
void listInsert(listNode* listHead,listNode* newNode){
    listNode* spot=findSpot(listHead,newNode);
    newNode->next=spot->next;
    spot->next=newNode;
}
void printList(listNode* listHead,ofstream& outFile){
    listNode* c=listHead;
    outFile<<"listHead -> ";
    while(c->next!=NULL){
        outFile<<"( "<<c->data<<","<<c<<","<<c->next<<","<<c->next->data<<") -> ";
        c=c->next;
    }
    outFile<<" NULL "<<endl;
}
void constructLL(linkedlist::listNode* listHead,ifstream& inFile,ofstream& outFile2){
    int a;
    cout<<listHead->data<<endl;
    while(inFile>>a){
        listNode* newNode=new listNode(a,NULL);
        listInsert(listHead,newNode);
        printList(listHead,outFile2);
    }
}
listNode* findMiddleNode(listNode* listHead,ofstream& outFile2){
    listNode* walk1=listHead;
    listNode* walk2=listHead;
    listHead->printNode(walk1, outFile2);
    while(walk2!=NULL && walk2->next!=NULL){
        walk1=walk1->next;
        walk2=walk2->next->next;
    }
    return walk1;
}
};
int main(int argc ,char* argv[]) {
   ifstream inFile(argv[1]);
   ofstream outFile1(argv[2]);
    ofstream outFile2(argv[3]);
//  linkedlist list;
//  list.listHead();
cout<<"123";
linkedlist:: listNode listHead(-9999,NULL);
//  list.sethead(-9999);
//  listHead=new listNode(-9999,NULL);
 //     cout<<list.gethead();
constructLL(listHead,inFile,outFile2);
//  printList(listHead,outFile1);
//  listNode* middleNode=findMiddleNode(listHead,outFile2);
//  listHead->printNode(middleNode,outFile1);
inFile.close();
outFile1.close();
outFile2.close();
return 0;
}
Rex Huang
  • 1
  • 2