-3

So the logic goes like this: Suppose the link list consists of (6,7,8) as data and I pass insert(1,5),so the list will be as (5,6,7,8). Similarly on insert(3,2) link list is (6,7,2,8).

I tried compiling the below code but it gives me an error stating-

Undefined reference to main by '-start'

I tried debugging,even searching for answers but found no help.Kindly suggest a solution.Any further suggestions and bug fixes shall be welcomed. (I have used codepad for compiling)

#include<iostream> 
using namespace std;
class Link_no
{
    struct node
    {
        int data;
        node *next;
    };

    void insert(int n,int d,node *head)
    {
        node *temp=new node();
        temp->data=d;
        temp->next=NULL;
        node *temp1;

        if(n==1)
        {
            temp->next=head;
            head=temp;
            return;
        }
        else
            temp1=head;
        {
            for(int i=0;i<n-1;i++)
            {
                temp1=temp1->next;
            }
            temp->next=temp1;
            temp1=temp;
       }
    }
    void print(node *start)
    {
        node *temp=start;
        while(temp!=NULL)
        {
            cout<<temp->data<<endl;
            temp=temp->next;
        }
    }
    int main()
    {
        node *head=NULL;
        Link_no o1;
        o1.insert(1,5,head);
        o1.insert(2,7,head);
        o1.insert(1,9,head);
        o1.print(head);
        return 0;
    }
}
Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
Sarthak Mehra
  • 359
  • 1
  • 2
  • 12

3 Answers3

4

C++ isnt java, the main does not belong inside a class. The compiler complains because there is no int main() in your code only a int Link_no::main() but that is not the entry point of the program.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

Take out int main() from class Link_no. Take out struct node from class Link_no. It should compile.

Heto
  • 590
  • 4
  • 9
0

The following compiles

#include<iostream> 
using namespace std;
class Link_no
{
    private:
    struct node
    {
        int data;
        node *next;
    };

    node *head;
    public: 

    Link_no(){
        head = nullptr;
    }

    void insert(int n,int d)
    {
        node *temp=new node();
        temp->data=d;
        temp->next=NULL;
        node *temp1;

        if(n==1)
        {
            temp->next=head;
            head=temp;
            return;
        }
        else
            temp1=head;
        {
            for(int i=0;i<n-1;i++)
            {
                temp1=temp1->next;
            }
            temp->next=temp1;
            temp1=temp;
       }
    }

    void print()
    {
        node *temp=head;
        while(temp!=NULL)
        {
            cout << "data is " << temp->data<<endl;
            temp=temp->next;
        }
    }


};

    int main()
    {
        Link_no o1;
        o1.insert(1,5);
        o1.insert(2,7);
        o1.insert(1,9);
        o1.print();
        return 0;
    }

It does not completely do what you want yet only prints out 5 and 9 as data so you need to debug some more.

Edit: I suggest you take a paper and pen and manually try to do what you're doing in your else since there is something going wrong there.

If you can't find it out on your own the following works for me, I haven't tried testing for extreme cases yet.

#include<iostream> 
using namespace std;
class Link_no
{
    private:
    struct node
    {
        int data;
        node *next;
    };

    node *head;
    public: 

    Link_no(){
        head = nullptr;
    }

    void insert(int n,int d)
    {
        node *temp=new node();
        temp->data=d;
        temp->next=NULL;
        node *temp1;

        if(n==1)
        {
            temp->next=head;
            head=temp;
            return;
        }
        else
        {
            cout << "foo" << endl;
            temp1=head;
            for(int i=1;i<n-1;i++)
            {
                temp1=temp1->next;
            }
            node *temp2 = temp1->next;
            temp1->next = temp;
            temp->next=temp2;
       }
    }

    void print()
    {
        node *temp=head;
        cout << "link" << endl;
        while(temp!=NULL)
        {
            cout << "data is " << temp->data<<endl;
            temp=temp->next;
        }
    }


};

    int main()
    {
        Link_no o1;
        o1.insert(1,5);
        o1.print();
        o1.insert(2,7);
        o1.print();
        o1.insert(1,9);
        o1.insert(2,6);
        o1.print();
        return 0;
    }
turoni
  • 1,345
  • 1
  • 18
  • 37
  • Your code worked fine.Thanks! just one problem...while using nullptr, an undefined scope is shown.But when I replace it back to null,the compilation is successful.Please try looking into this problem too. – Sarthak Mehra May 28 '16 at 16:38
  • Do you understand what you did wrong? Also can you be a little more clear what you mean by undefined scope? Is this a compilation error or is your IDE giving this error? – turoni May 28 '16 at 16:48
  • I had compiled using codeshef online compiler.It showed this error only for nullptr,not for null. Maybe the problem is with IDE itself or something? – Sarthak Mehra May 28 '16 at 18:48
  • [This](http://stackoverflow.com/questions/10033373/c-error-nullptr-was-not-declared-in-this-scope-in-eclipse-ide) discusses the nullptr issue in more detail. Simple said that compiler is not using c++11 which is where you can use nullptr. Null wil do fine but [this](http://stackoverflow.com/questions/1282295/what-exactly-is-nullptr) discusses why you should use nullptr. Is there a reasonyou have removed my answer as best answer? – turoni May 29 '16 at 10:05