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

–1 vote
asked Sep 11, 2019 by sekhar1398 (110 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;

}

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...