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 3672566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:37:29+00:00 2026-05-19T02:37:29+00:00

I was developing a generic linked list. Although the compiler doesn’t give any error

  • 0

I was developing a generic linked list. Although the compiler doesn’t give any error but on running the program, it just crashes. I haven’t been able to figure out what’s wrong but since I am trying insert method of the list in main, the problem is somewhere there itself. Here’s the code in List.h

#include<cstdlib>

enum Error_code
{
    success,
    overflow,
    underflow,
    range_error
};

template<class Node_entry>
struct Node
{
    Node_entry entry;
    Node<Node_entry> *next;

    Node()
    {
        next=NULL;
    }

    Node(Node_entry item, Node<Node_entry> *add_on=NULL)
    {
        entry=item;
        next=add_on;
    }
};

template<class List_entry>
class List
{
  public:
    List()
    {
        count=0;
        head=NULL;
    }

    Error_code insert(int position, const List_entry &x)
    {
        if(position<0 || position>count)
            return range_error;
        Node<List_entry> *previous, *following, *new_node;
        if(position>0) {
            previous=set_position(position-1);
            following=previous->next;
        } else {
            following=head;
        }
        new_node = new Node<List_entry>(x, following);
        if(new_node==NULL)
            return overflow;
        if(position==0)
            head=new_node;
        else
            previous->next=new_node;
        count++;
        return success;
    }

    Error_code remove(int position, List_entry &x)
    {
        if(position<0 || position>count)
            return overflow;
        Node<List_entry> *old_node, *previous;
        if(position==0)
            old_node=head;
        else {
            previous=set_position(position-1);
            old_node=previous->next;
        }
        if(old_node==NULL)
            return underflow;
        if(position==0) {
            head=old_node->next;
            delete old_node;
        } else {
            previous->next=old_node->next;
            delete old_node;
        }
        count--;
        return success;
    }

    bool empty() const
    {
        return count==0;
    }

    ~List()
    {
        Node<List_entry> *temp_node=head->next;
        while(!empty()) {
            delete head;
            head=temp_node;
            temp_node=head->next;
        }
    }

  protected:
    int count;
    Node<List_entry> *head;

    Node<List_entry> *set_position(int position)const
    {
        Node<List_entry> *q=head;
        for(int i=0;i<count;i++)
            q=q->next;
        return q;
    }
};

main.cpp

#include <iostream>
#include"List.h"
using namespace std;
int main()
{
    int i;
    List<int> the_list;
    the_list.insert(1, 2);
}

P.S I am just into learning the basics for now and not working on large scale design modules and practices. At this point, this only needs to work.

  • 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-05-19T02:37:30+00:00Added an answer on May 19, 2026 at 2:37 am

    What happens in your main function is:

    1. Construct list; success.
    2. Try to insert at position 1, which fails with range_error because position>count. If you choose to return error codes, you should always check for them.
    3. Destroy list. This segfaults because head is NULL when you try to dereference it with Node<List_entry> *temp_node=head->next;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C program that I'm developing using Ubuntu 11.10 (Linux version 3.0.0-12-generic-pae
Developing an interface generic I wished to declare a constructor in an interface but
I'm currently developing a program in python and I just noticed that something was
I'm developing a generic app on PHP that is dynamically customized for many clients.
I am developing a cache for generic types and have a doubt, should I
While developing an application using gwt in ecliplse crashed. Now the server is running
I am developing a generic wrapper around TryParse, as follows: public delegate bool ParseDelegate<T>(string
I am developing a generic layer that allows me to perform the basic save,
Does IntelliJ have any special support for developing a MDI (Multiple Document Interface) application?
This question is pretty generic actually, but I'm really having trouble finding a good

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.