#include <iostream>
#include <string>
using namespace std;
struct Employee
{
//Data
string name;
int id;
string residance;
int salary;
};
class Node
{
public:
// Data
Employee data;
// The Next Pointer
Node * next;
// Constructor
Node(Employee Object ,Node * ptr = NULL );
};
class List
{
int size;
Node * first;
//Node Obj;
Node * ptr;
int counter;
public:
bool Exist;
public:
List();
/************************************
PreCondition : None
PostCondition : Linked List is constructed , with head and tail pointing
to nothing and a size if zero
************************************/
//Copy Constructor
List(List& );
/************************************
PreCondition : None
PostCondition : Linked List is constructed that is similar ( copy ) of the paramter
Object
************************************/
// Assignment Operator
const List operator = (const List & Obj);
// Destructor
~List();
//Empty: check if the list is empty or not.
/***** empty operation *****/
bool Empty();
/*----------------------------------------------------------------------
Check if a list is empty.
Precondition: None
Postcondition: true is returned if the list is empty,
false if not.
-----------------------------------------------------------------------*/
//Search: take an employee object, search for it; if found return the Node name and Salary.
void Search (Node obj);
//Insert Sorted: takes an object of the Node type, search for it,
// and if found return without insertion else search for its correct position –
// to keep the list ordered by name- then add it.
void InsertSorted (Node obj);
//Insert At Mid: takes an object of Node type, and insert it in the middle of the Node list.
// (The middle is n/2 if odd and n/2+1 if even)
void InsertMid (Node obj);
//Remove: takes an Node object, search for it in the list, and if found delete it.
void Delete (Node obj);
//Remove mid: delete the Node object found in the middle of the list.
void DeleteMid ();
//Change Salary: takes a salary and employee object,
// replace the information for this employee by the information passed as parameter.
List ChangeSalary(Node obj , int sal);
//Print All Nodes: that prints all employees information.
void Print();
};
#include "Header.h"
//Constructor
Node::Node(Employee Object ,Node *p)
{
data=Object;
next=p;
}
List::List()
{
first = NULL;
size = 0;
ptr = NULL;
Exist = false;
}
// Copy Constructor
List::List(List& Orig)
{
if(Orig.size==0)
{
first=NULL ;
return ;
}
ptr = first = new Node (Orig.first->data);
Node * CopyPtr = Orig.first ;
while(CopyPtr->next)
{
CopyPtr=CopyPtr->next;
ptr->next=new Node(CopyPtr->data);
ptr = ptr->next;
}
size = Orig.size;
}
// Empty
bool List::Empty()
{
if (size == 0)
{
cout<<"the List Is Empty."<<endl;
return true;
}
else
{
cout<<"The List Is Not Empty, It Has "<<size<<" Elements."<<endl;
return false;
}
}
//Search
void List::Search(Node obj)
{
{
ptr=first;
for ( int i=0 ; i<=size ;i++)
{
if (ptr->data.name==obj.data.name || ptr->data.id==obj.data.id)
{
cout<<"The Employee's Name : "<<obj.data.name<<" The Employee's Salary : "<<obj.data.salary<<endl;
Exist= true;
}
else if (i==size)
{
ptr=ptr->next;
}
else
{
clog<<"Emloyee Not Found"<<endl;
Exist= false;
}
}
}
}
void List::InsertSorted (Node obj)
//Insert Sorted: takes an object of the Node type, search for it,
// and if found return without insertion else search for its correct position –
// to keep the list ordered by name- then add it.
/* 2wl 2shi mn 3mal if statment w btswi serach 3la 2l object w b3dain 2tha 6l3 mwjood zyo btnsa 2l mwdoo3
w bt6la3 w 2tha kan msh mwjood bt3mal for loop tdqarn feha 2l 2sma2
w b3dain bkoun fe counter kol ma ynt8il la Node gdeed be zeed mshan n7keelo 2nsert at postion size plus 1
w mn thma
*/
{
int pos=0;
Search (obj);
if (Exist)
{
cout<<"The Employee Already Exists."<<endl;
}
else
{
ptr=first;
for (int i=0 ; i<=size ; i++ )
{
if (ptr->data.name<obj.data.name)
{
ptr=ptr->next;
pos=pos+1;
}
else
{
if(pos<1||pos>size+1)
{
cout<<"Invalid Position!"<<endl;
return ;
}
if(pos==1)
{
Node * NFirst = new Node(obj.data,first );
if(NFirst==NULL)
return ;
size++;
first = NFirst;
return ;
}
if(pos==size+1)
{
Node * NEnd = new Node(obj.data);
if (NEnd==NULL)
return ;
size++;
if (first==NULL)
first=NEnd ;
else
{
Node *temp = first ;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=NEnd;
}
return ;
}
ptr = first ;
for(int i=1;i<pos-1;i++)
{
ptr=ptr->next;
}
Node * newNode = new Node(obj.data,ptr->next);
ptr->next=newNode;
size++;
}
}
}
}
//"size" 3la 2 w mnsweelna solafeh 2tha kan frdi 2w zwji b3dain insert 6beee3i
void List::InsertMid(Node obj)
{
int pos = 0;
int x = 0;
if (size>1)
x=0;
else
x=2;
switch (x)
{
case 0:;
case 1:
{
Node * NFirst = new Node(obj.data,first );
if(NFirst==NULL)
return ;
size++;
first = NFirst;
return ;
}
case 2:
{
pos=size/2;
ptr =first ;
for(int i=1;i<pos-1;i++)
{
ptr=ptr->next;
}
Node * newNode = new Node(obj.data,ptr->next);
ptr->next=newNode;
size++;
}
}
}
// Delete
void List::Delete (Node obj)
{
int pos =0;
//search and make pos === smthin
if(size==0)
return ;
if(pos>size||pos<1)
{
cout<<"Invalide position !"<<endl;
return ;
}
if(size==1)
{
delete first;
first=NULL;
size = 0;
return ;
}
if(pos==1)
{
Node *toDelete = first;
first = first->next;
delete toDelete ;
return ;
}
ptr = first;
for(int i=1;i<pos-1;i++)
{
ptr=ptr->next;
}
Node *toDelete = ptr->next;
ptr->next = toDelete->next;
delete toDelete ;
size--;
}
//Delete Mid
void List::DeleteMid()
{
int pos=0;
pos=size/2;
ptr = first;
for(int i=1 ; i<pos-1 ; i++)
{
ptr=ptr->next;
}
Node *toDelete = ptr->next;
ptr->next = toDelete->next;
delete toDelete ;
size--;
}
// Print
void List::Print()
{
for (int i = 0 ; i<=size ;i++)
{
cout<<ptr->data.name<<ptr->data.id<<ptr->data.residance<<ptr->data.salary<<endl;
ptr->next;
}
}
#include "Header.h"
/*
Q2: An Employee information system must contain data about
employees in order to retrive and manupliate data easily and quickly.
Create a structure for Employee that contains:
employee name, employee id, residence and Salary.
Create a class linked List so that each node has an object of Employee type.
The class should support the following operations:
? Empty: check if the list is empty or not.
? Search: take an employee object, search for it;
if found return the employee name and Salary.
? Insert Sorted: takes an object of the employee type, search for it,
and if found return without insertion else search for its correct position –
to keep the list ordered by name- then add it.
? Insert At Mid: takes an object of employee type, and insert it in the middle of the employee list.
(The middle is n/2 if odd and n/2+1 if even)
? Remove: takes an employee object, search for it in the list, and if found delete it.
? Remove mid: delete the employee object found in the middle of the list.
? Change Salary: takes a salary and employee object,
replace the information for this employee by the information passed as parameter.
? Print All Nodes: that prints all employees information.
Notes:
1- The Linked List class must contain the constructor, copy constructor, and assignment operator.
*/
void main ()
{
List HomeWork;
int x;
do
{
cout << endl;
cout << "Select one of the following"<<endl;
cout << "1- Check empty"<<endl;
cout << "2- Search"<<endl;
cout << "3- Insert Sorted"<<endl;
cout << "4- Insert"<<endl;
cout << "5- "<<endl;
cout << "6- "<<endl;
cout << "7- "<<endl;
cout << "8- Print "<<endl;
cout << "9- Exit"<<endl;
cout << "Enter your choice ";
cin >> x;
switch (x)
{
case 1:
{
//HW.Empty();
}
case 2:
{
//HomeWork.Search (HomeWork.Obj);
}
case 3:
{
//emp * node = new emp;
//HomeWork.setme();
// node->Do_insert();
break;
};
case 8:
{
break;
}
case 9: {cout << "Good bye"; break;}
}
}
while (x !=9);
}
it Genrates this Error
------ Build started: Project: LinkList_20080171071, Configuration: Debug Win32 ------
Linking...
Main.obj : error LNK2019: unresolved external symbol "public: __thiscall List::~List(void)" (??1List@@QAE@XZ) referenced in function _main
C:\Documents and Settings\Marji\My Documents\Visual Studio 2005\Projects\LinkList_20080171071\Debug\LinkList_20080171071.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Marji\My Documents\Visual Studio 2005\Projects\LinkList_20080171071\LinkList_20080171071\Debug\BuildLog.htm"
LinkList_20080171071 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========