Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:41:03+00:00 2026-06-14T04:41:03+00:00

I have tried to implement Singly Linked List. My addAtLast() function is not getting

  • 0

I have tried to implement Singly Linked List. My addAtLast() function is not getting executed properly. Program crashes while executing this function. Kindly suggest some changes.

class LList
{
public:
    int noOfNodes;
    Node const *start;/*Header Node*/

    LList()
    {
        start=new Node;
        noOfNodes=0;start=0;
    }

    void addAtFront(Node* n)
    {
        /*
        cout<<endl<<"class"<<n;
        cout<<"start"<<start;
        cout<<"data in node";n->print();
        */
        n->next=const_cast<Node*>(start);
        start=n;
        // cout<<" next=";start->print();
        noOfNodes++;
    }

    void addAtLast(Node* n)
    {
        Node *cur=const_cast<Node*>(start);
        if (start==NULL)
        { 
            start=n;
            return;
        }
        while(cur->next!=NULL)
        {
            cur=cur->next;
        }
        cur->next=n;
        noOfNodes++;
    }

    int getPosition(Node data)
    {
        int pos=0;
        Node *cur=const_cast<Node*>(start);
        while(cur!=NULL)
        {
            pos++;
            if(*cur==data)
            {
                return pos;
            }
            cur=cur->next;
        }
        return -1;//not found
    }

    Node getNode(int pos)
    {
        if(pos<1)
            return -1;// not a valid position
        else if(pos>noOfNodes)
            return -1; // not a valid position

        Node *cur=const_cast<Node*>(start);
        int curPos=0;
        while(cur!=NULL)
        {
            if(++curPos==pos)
                return *cur;
            cur=cur->next;
        }
    }

    void traverse()
    {
        Node *cur=const_cast<Node*>(start);
        while(cur!=NULL)
        {
            //   cout<<"start"<<start;        
            cur->print();
            cur=cur->next;
        }
    }  

    ~LList()
    {
        delete start;
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:41:04+00:00Added an answer on June 14, 2026 at 4:41 am

    I mentioned this in-comment, but will address it here as an answer. The caller of this function must ensure two things:

    1. That the passed-in node list (and it is a list, even if only one element long) must be properly terminated with an end-next-pointer set to NULL. The caller must ensure this, as this code cannot assume it and blindly set node->next = NULL;

    2. Make absolutely sure the caller is aware that once this executes, this list now owns the passed-in node and any list it potentially starts and the caller, therefore, must NOT free it, or anything it points to, on the caller side.

    Apart from the node-count management issue, there is nothing wrong with the addAtLast() function, though I would have implemented it a little differently:

    void addAtLast(Node* n)
    {
        // no nulls allowed
        if (n == NULL)
            return;
    
        // set start if first in list
        if (start == NULL)
        {
            start = n;
            noOfNodes = 1;
        }
    
        // else walk list to find end
        else
        {
            Node *cur = const_cast<Node*>(start);
            while(cur->next != NULL)
                cur = cur->next;
            cur->next = n;
            ++noOfNodes;
        }
    
        // adjust count to contain any nodes from 'n'
        while (n->next != NULL)
        {
            ++noOfnodes;
            n = n->next;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Reverse a singly linked list reverse a linked list? I have been
I have tried to implement css sticky footer on my page but it doesn't
I have been looking around for solutions, and tried to implement what is often
I have tried searching over the internet about this problem but not able to
I have a class that implements a linked list. The class has a find()
I have just noticed that a multidimensional array in C# does not implement IEnumerable<T>
Have tried to find solutions for this and can't really come up with anything.
I have tried to go through the jungle (really, PayPal, why don't you weed
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I have tried many permutations but they all don't seems to work well. Am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.