🔒 Closed c++ linked list (struct,pointer,function) help (SOLVED)

Status
Not open for further replies.

IIZeroHourII

Eternal Poster
patulong sa mga master jan. bakit po same output lahat ng lumalabas sa mga inenter ko. tas hanggang 4 lang po ang kaya nyang node nag sstop working ung command line pag nag enter ng 5 node.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>

using namespace std;

struct employee
{
string name;
string position;
int salary;
employee *next;
};

employee *head;

void callme()
{
employee *temp, *newnode;
string temp_name;
string temp_position;
int temp_salary;
int n;


head=(employee *)malloc(sizeof(employee));
if(head==NULL)
{
cout<<"No size of head"<<endl;
}

cout<<"Enter number of node";
cin>>n;
cout<<"Enter Name : ";
cin>>temp_name;
cout<<"Enter Salary : ";
cin>>temp_salary;
cout<<"Enter Position : ";
cin>>temp_position;

head->name=temp_name;
head->salary=temp_salary;
head->position=temp_position;
head->next= NULL;
temp=head;

for(int i=2 ; i<=n ; i++)
{
newnode=(employee *)malloc(sizeof(employee));
if(newnode == NULL)
{
cout<<"No size for new node"<<endl;
}
cout<<"Enter name : ";
cin>>temp_name;
cout<<"Enter Salary : ";
cin>>temp_salary;
cout<<"Enter Position : ";
cin>>temp_position;

newnode->name=temp_name;
newnode->salary=temp_salary;
newnode->position=temp_position;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}

cout<<"name\t\t\tPosition\t\t\tsalary\t\t\t"<<endl;
while(head != NULL)
{
cout<<temp_name<<"\t\t\t"<<temp_position<<"\t\t\t"<<temp_salary<<endl;
head=head->next;
}
while(newnode != NULL)
{
cout<<temp_name<<"\t\t\t"<<temp_position<<"\t\t\t"<<temp_salary<<endl;
newnode=newnode->next;
}

}


int main()
{
callme();
}
 
I-rewrite mo yung code mo. Mainam na i-practice mo yung proper formatting. Tapos, i-embed mo sa code-blocks yung code mo para maayos tingnan.

To be honest, nahilo ko kababasa ng code mo. Eto mas, comprehensible:

Code:
#include <iostream>

struct Employee {
    std::string name;
    std::string position;
    int salary;
    Employee *next;
};

Employee *pHead = nullptr;
int num_employee{0};

Employee *get_data() {
    std::string name, position;
    int salary;
    std::cout << "\nEnter name: ";
    std::cin >> name;
    std::cout << "Enter position: ";
    std::cin >> position;
    std::cout << "Enter salary: ";
    std::cin >> salary;
    Employee *new_employee = new Employee;
    new_employee->name = name;
    new_employee->position = position;
    new_employee->salary = salary;
    new_employee->next = nullptr;
    return new_employee;
}

void add_employee(Employee *new_employee) {
    new_employee->next = pHead;
    pHead = new_employee;
}

int main(void) {
    std::cout << "\nEnter number of employee: " << std::flush;
    std::cin >> num_employee;
    Employee *new_employee = new Employee;
    for (int i=0; i<num_employee; i++) {
        add_employee(get_data());
        if (i==num_employee) {
            add_employee(nullptr);
        }
    }

    std::cout << std::endl;
    std::cout << "\nNow displaying the contents of Linked List..." << std::endl;
    for(Employee *iter=pHead; iter; iter=iter->next) {
        std::cout << "NAME: " << iter->name << std::endl;
        std::cout << "POSITION: " << iter->position << std::endl;
        std::cout << "SALARY: "  << iter->salary << std::endl << std::endl;
    }

    return 0;
}
 
C++:
for(int i=2 ; i<=n ; i++)
{
    //Statement here...
}

I wonder why did you initialized your int variable 'i' to 2? If you try to simulate that manually, that would only yields 4 loops, which can be illustrate as follows: (2<=5) = yes, (3<=5) = yes, (4<=5) = yes, (5<=5) = yes. I dont want to give you the exact advice but it's almost I am done telling you with the illustration I gave you. Hope it helped.
 
I-rewrite mo yung code mo. Mainam na i-practice mo yung proper formatting. Tapos, i-embed mo sa code-blocks yung code mo para maayos tingnan.

To be honest, nahilo ko kababasa ng code mo. Eto mas, comprehensible:

Code:
#include <iostream>

struct Employee {
    std::string name;
    std::string position;
    int salary;
    Employee *next;
};

Employee *pHead = nullptr;
int num_employee{0};

Employee *get_data() {
    std::string name, position;
    int salary;
    std::cout << "\nEnter name: ";
    std::cin >> name;
    std::cout << "Enter position: ";
    std::cin >> position;
    std::cout << "Enter salary: ";
    std::cin >> salary;
    Employee *new_employee = new Employee;
    new_employee->name = name;
    new_employee->position = position;
    new_employee->salary = salary;
    new_employee->next = nullptr;
    return new_employee;
}

void add_employee(Employee *new_employee) {
    new_employee->next = pHead;
    pHead = new_employee;
}

int main(void) {
    std::cout << "\nEnter number of employee: " << std::flush;
    std::cin >> num_employee;
    Employee *new_employee = new Employee;
    for (int i=0; i<num_employee; i++) {
        add_employee(get_data());
        if (i==num_employee) {
            add_employee(nullptr);
        }
    }

    std::cout << std::endl;
    std::cout << "\nNow displaying the contents of Linked List..." << std::endl;
    for(Employee *iter=pHead; iter; iter=iter->next) {
        std::cout << "NAME: " << iter->name << std::endl;
        std::cout << "POSITION: " << iter->position << std::endl;
        std::cout << "SALARY: "  << iter->salary << std::endl << std::endl;
    }

    return 0;
}

Ang ganda ng code ni sir oh. Simplified and well written with a good coding scheme practice.
 
I-rewrite mo yung code mo. Mainam na i-practice mo yung proper formatting. Tapos, i-embed mo sa code-blocks yung code mo para maayos tingnan.

To be honest, nahilo ko kababasa ng code mo. Eto mas, comprehensible:

Code:
#include <iostream>

struct Employee {
    std::string name;
    std::string position;
    int salary;
    Employee *next;
};

Employee *pHead = nullptr;
int num_employee{0};

Employee *get_data() {
    std::string name, position;
    int salary;
    std::cout << "\nEnter name: ";
    std::cin >> name;
    std::cout << "Enter position: ";
    std::cin >> position;
    std::cout << "Enter salary: ";
    std::cin >> salary;
    Employee *new_employee = new Employee;
    new_employee->name = name;
    new_employee->position = position;
    new_employee->salary = salary;
    new_employee->next = nullptr;
    return new_employee;
}

void add_employee(Employee *new_employee) {
    new_employee->next = pHead;
    pHead = new_employee;
}

int main(void) {
    std::cout << "\nEnter number of employee: " << std::flush;
    std::cin >> num_employee;
    Employee *new_employee = new Employee;
    for (int i=0; i<num_employee; i++) {
        add_employee(get_data());
        if (i==num_employee) {
            add_employee(nullptr);
        }
    }

    std::cout << std::endl;
    std::cout << "\nNow displaying the contents of Linked List..." << std::endl;
    for(Employee *iter=pHead; iter; iter=iter->next) {
        std::cout << "NAME: " << iter->name << std::endl;
        std::cout << "POSITION: " << iter->position << std::endl;
        std::cout << "SALARY: "  << iter->salary << std::endl << std::endl;
    }

    return 0;
}

ganda nang ayus, salamat paps. ngayon lang ako nakakita ng ganto nasanay ako sa structured nung prof namin hahahaha.
 
Status
Not open for further replies.

About this Thread

  • 5
    Replies
  • 479
    Views
  • 4
    Participants
Last reply from:
IIZeroHourII

Trending Topics

Online now

Members online
300
Guests online
985
Total visitors
1,285

Forum statistics

Threads
2,280,327
Posts
28,996,593
Members
1,226,725
Latest member
goomba75
Back
Top