This is a homework assignment for class, note that this is suppose to be a introduction class... hence our teacher is crazy.
Anyways, we are suppose to utilize the doublelinked list so that you input employee data to first name and ID.
You are should be able to search the linked list using the next and previous nodes and find the matching ID you search for.
He gives us the function prototypes for all of the functions we need...
He also gave us the entire first function EmployeeDLLNode * Makedoublylinkedlist(int N) of the definition file.
I've got it to call the first function in main and I am able to enter employee data, but after that it does not display it.
I also have a bunch of compile errors with the display functions in main saying that start is undeclared. Do i need to create a pointer in main?
Will that lose all the data that I stored from the first function?? little confused... also... any other tips would be great..
I am lost right now, and my intro book for the class does not go over double linked lists well... found this site to be helpful so far, thought i'd try the forum peoples.
thanks for time & help!!
- Paul
Code:
//******************* employee.h ************
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>
using namespace std;
typedef struct Employee
{
int ID;
string firstname;
} Employee;
typedef struct EmployeeDLLNode
{
EmployeeDLLNode * start;
Employee data;
struct EmployeeDLLNode * previous;
struct EmployeeDLLNode * next;
} EmployeeDLLNode;
Employee Collectdata();
EmployeeDLLNode * Makedoublylinkedlist(int N);
void DisplayDLLofEmployees(Employee * start);
EmployeeDLLNode * FindEmployeeById(EmployeeDLLNode * start, int id);
void DisplayEmployee(Employee e);
#endif
// ************** DEFINITION FILE employee.cpp *****************
#include "employee.h"
// Step 1: Define pointers, Cycle through array and store data inputed
EmployeeDLLNode * Makedoublylinkedlist(int N)
{
EmployeeDLLNode * start = new EmployeeDLLNode;
start -> data = Collectdata();
start -> previous = start = 0;
EmployeeDLLNode * last = start;
for (int i = 1; i < N; i++)
{
last = new EmployeeDLLNode;
last -> next -> data = Collectdata();
last -> next -> previous = last;
last -> next -> next = 0;
last = last -> next;
}
return start;
}
// Step 2: Input employee data
Employee Collectdata()
{
Employee emp;
cout << "Enter ID: ";
cin >> emp.ID;
cin.ignore();
cout << "First name: ";
cin >> emp.firstname;
return emp;
}
// Step 3: Cycle through list, display employee data
void DisplayDLLofEmployees(EmployeeDLLNode * start)
{
for(EmployeeDLLNode * i = start ; i != 0; i = i -> next)
{
DisplayEmployee (i -> data);
}
}
// Step 4: Display employee data
void DisplayEmployee(Employee e)
{
cout << e.ID << " " << e.firstname << endl;
}
// Step 5: Search linked list by ID (?????)
EmployeeDLLNode * FindEmployeeById(EmployeeDLLNode * start, int id)
{
for(EmployeeDLLNode * i = start ; i != 0; i = i -> next)
{
// ????
}
return 0;
}
// ******************* main.cpp **************
#include "employee.h"
int main()
{
for(;;)
{
// Input data (Size of array)
cout << "How many employees? ";
int N;
cin >> N;
// Creates a double linked list using previous and next pointers
Makedoublylinkedlist(N);
// Cycle through array to display employees (error)
DisplayDLLofEmployees(start);
// Input data (ID search)
cout << "Search ID: ";
int id;
cin >> id;
// Searches double linked list for specified ID (????)
FindEmployeeById(start,id);
/*
// DELETE LIST (RESET LIST)
*/
// PROMPT REPEAT
cout << endl << endl
<< "Enter a 0 to end or a non-negative integer to repeat: ";
cout << endl << endl;
int flag;
cin >> flag;
if(!flag)
break;
}
return 0;
}