Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

segmenation error

+6 votes
asked Sep 11, 2019 by sekhar1398 (180 points)
#include<iostream>

#include<string>

using namespace std;

struct employee

{

int age,year;

string name;

employee *next;

};

class List

{

private:

employee *head,*temp;

public:

List()

{

head = NULL;

}

/* void insert()

{

string name_Array[7]= {"Javeria","Nageeta","Karishma","Bakhtawar","Malook","Shakeel","Nadir"};

int age_Array[7] = {105,95,115,19,15,10,18};

int year_Array[7] = {1987,1990,2011,2015,2017,2019,2019};

employee *temp = head;

for(int i=0; i<7; i++)

{

employee *newNode = new employee;

newNode->name = name_Array[i];

newNode->age = age_Array[i];

newNode->year = year_Array[i];

newNode->next = NULL;

temp->next = newNode;

temp = temp->next;

    if(i==0)

    {

    temp = newNode;

head = newNode;

}

else

{

temp->next = newNode;

temp = temp->next;

}   

}

}*/

void insert(string name, int age, int year)

{

employee *newNode,*current = head, *previous = NULL;

newNode = new employee;

newNode->name = name;

newNode->age = age;

newNode->year = year;

newNode->next = NULL;

if(head == NULL)

{

head = newNode;

current = newNode;

previous = NULL;

}

else

{

current = head;

while(current!=NULL)

{

previous = current;

current = current->next;

if(current->year >= previous->year)

{

previous->next = current->next;

//current = current->next;

}

else

{

}

}

}

}

//=======================================================

/* void display()

    {

    for(employee* p = head; p != NULL; p = p->next)

    {

        cout<<p->name<<endl;

        cout<<p->age<<endl;

        cout<<p->year<<endl;

    }

    

*/ }

//=======================================================

void display_N()

{

temp = head;

int i=0;

while(temp!=NULL)

{

i++;

cout<<temp->name<<" ";

        cout<<temp->age<<" ";

        cout<<temp->year<<endl;

        temp = temp->next;

cout<<"in while loop "<<i<<endl;

}

}

//===================================================

};

int main()

{

string name;

int age,year;

List *obj = new List;

for(int i=0; i<3; i++)

{

cout<<"Enter Name of employee : ";

getline(cin,name);

cout<<"Enter age of employee : ";

cin>>age;

cout<<"Enter year of employee : ";

cin>>year;

cin.ignore();

obj->insert(name,age,year);

}

obj->display_N();

/*obj->display();*/

return 0;

}

2 Answers

–1 vote
answered Dec 1, 2024 by Oliver 66 (290 points)
go on google and search! easy!
0 votes
answered Dec 2, 2024 by Peter Minarik (101,420 points)

Your insert function has current incremented in the loop and current could become nullptr and dereferenced (previous->next = current->next;), causing the segmentation fault.

I've fixed up your code and ended up with this. You should write a destructor that frees up all the allocated memory though!

#include <iostream>
#include <string>

using namespace std;

struct employee
{
    int age, year;
    string name;
    employee * next;
};

class List
{
private:
    employee * head;

public:
    List()
    {
        head = nullptr;
    }
    
    void insert(string name, int age, int year)
    {
        // Create the new node
        employee * newNode = new employee { age, year, name, nullptr };
        
        // Find the where to insert the new node
        employee * insertAfter = nullptr;
        for (employee * current = head; current != nullptr; current = current->next)
        {
            if (current->year <= newNode->year)
            {
                insertAfter = current;
            }
            else
            {
                break;
            }
        }
        
        // Insert the new node
        if (insertAfter == nullptr) // The node goes to the head of the list
        {
            newNode->next = head;
            head = newNode;
        }
        else // The node goes inside the list
        {
            newNode->next = insertAfter->next;
            insertAfter->next = newNode;
        }
    }

    void display()
    {
        for (employee * temp = head; temp != nullptr; temp = temp->next)
        {
            cout << temp->name << " ";
            cout << temp->age << " ";
            cout << temp->year << endl;
        }
    }
};

int main()
{
    string name;
    int age, year;
    List *obj = new List;
    for (int i = 0; i < 3; i++)
    {
        cout << "Enter name of employee : ";
        getline(cin, name);
        cout << "Enter age of employee : ";
        cin >> age;
        cout << "Enter year of employee : ";
        cin >> year;
        cin.ignore();
        obj->insert(name, age, year);
    }
    obj->display();
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...