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

  • SEARCH
  • Home
  • 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 8230773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:14:42+00:00 2026-06-07T17:14:42+00:00

I was writing a C++ program to implement a linked list. On compilation it’s

  • 0

I was writing a C++ program to implement a linked list. On compilation it’s not giving any error but in the output windows it goes blank and program ended with

list1.exe has
encountered a problem and needs to close.

Debugger response: Program received signal SIGSEGV, Segmentation fault.

Maybe it’s because of memory leakage, but I’m not able to figure out the exact bug and how can we fix that. Please what’s wrong in the prog and what should be fixed?

Below is the code

  //Program to implement linked list

  #include <iostream>
  #include <cstdlib>

  using namespace std;

  class Node
  {
      int data;
      Node * next;

   public:
      Node (){}
      int getdata(){return data ;}
      void setdata(int a){data=a;}
      void setnext(Node* c){next=c;}
      Node* getnext(){return next;}
  };

  class linkedlist
  {
      Node* head;

  public:
      linkedlist(){head=NULL;}
      void print ();
      void push_back(int data);
  };

  void linkedlist::push_back(int data)
  {
      Node* newnode= new Node();
      if(newnode!=NULL)
      {
          newnode->setdata(data);
          newnode->setnext(NULL);
      }
      Node* ptr= head;

      if(ptr==NULL) 
          {head=newnode;}
      while ((ptr->getnext())!=NULL)
      {
          ptr=ptr->getnext();
      }
      ptr->setnext(newnode);
  }

  void linkedlist::print()
  {
      Node* ptr=head;
      if(ptr==NULL)
          {cout<<"null"; return;}

      while(ptr!=NULL)
      {
          cout<<(ptr->getdata())<<" ";
          ptr=ptr->getnext();
      }
  }

  int main()
  {
     linkedlist list;
      list.push_back(30);
      list.push_back(35);
      list.print();
      return 0;
  }
  • 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-07T17:14:44+00:00Added an answer on June 7, 2026 at 5:14 pm

    The main issue is here:

    if(ptr==NULL) {head=newnode;}
    while ((ptr->getnext())!=NULL)
    {
        ptr=ptr->getnext();
    }
    ptr->setnext(newnode);
    

    There’s probably meant to be a return; in the if (ptr == NULL) part; as it stands, it sets head = newnode, but then continues to try to access ptr->getnext(), which causes the segfault.

    Some answers have suggested setting ptr = head = newnode, but note that the bottom line is ptr->setnext(newnode)—this would cause head->getnext() == head. Infinite list!

    For your interest, here’s your code:

    • Formatted nice;
    • Cleaned up not to use using namespace std; (see the C++ FAQ on this);
    • Takes advantage of const-correctness;
    • etc.

    Enjoy!

    #include <iostream>
    #include <stdexcept>
    
    class Node {
        int data;
        Node *next;
    
    public:
        Node(): next(NULL) {}
    
        int getdata() const {
            return data;
        }
    
        void setdata(int a) {
            data = a;
        }
    
        Node *getnext() const {
            return next;
        }
    
        void setnext(Node *c) {
            next = c;
        }
    };
    
    class linkedlist {
        Node* head;
    
    public:
        linkedlist(): head(NULL) {} 
    
        void print() const {
            Node *ptr = head;
    
            if (ptr == NULL) {
                std::cout << "null";
                return;
            }
    
            while (ptr != NULL) {
                std::cout << ptr->getdata() << " ";
                ptr = ptr->getnext();
            }
        }
    
        void push_back(int data) {
            Node *newnode = new Node();
    
            if (newnode == NULL) {
                throw std::runtime_error("out of memory!");
            }
    
            newnode->setdata(data);
    
            Node *ptr = head;
    
            if (ptr == NULL) {
                head = newnode;
                return;
            }
    
            while ((ptr->getnext()) != NULL) {
                ptr = ptr->getnext();
            }
    
            ptr->setnext(newnode);
        }
    };
    
    int main() {
        linkedlist list;
        list.push_back(30);
        list.push_back(35);
        list.print();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program in c++ which implements a doubly-linked list that holds
In a Python program I'm writing, I've built up a linked list using a
I'm writing a program that handles DBs and writes any changes into ListView for
I am writing a program that permutes a list of names based on a
I am needing to implement regular expressions in a C++ program I am writing,
I'm writing a program trying to implement a toy XML processor. Right now the
I'm writing a program for an assignment which has to implement LZW compression/decompression. I'm
I'm writing a drawing program and one feature I would like to implement is
I am writing a program that and like to implement data verification system. It
I'm having a problem designing part of my program (not writing it, for once!).

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.