943,800 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 374
  • C++ RSS
Nov 29th, 2008
0

i need help pls LINKED List

Expand Post »
this is a linked list assigment and the required from me is to fill the functions: i have one fucntion left that i was unable to do which is void DeleteElement(int ID);

and i have another problem the compiler states that i hve o errors but when i compile the program doesnt work will. so pls can u pls me thnx in advance



#include<iostream>
#include<string>
using namespace std;

void Output(struct Employee *data_ptr);
void Input(struct Employee *data_ptr);
void AddElement_Front(struct Employee E);
void AddElement_End(struct Employee E);
void AddElement_Sorted(struct Employee E);
void DeleteFirst();
void DeleteLast();
void DeleteElement(int ID);
int ListCount();
int ListSize();
void PrintList();
void SortList();
struct Employee MinElement();
struct Employee MaxElement();

struct Employee
{
int id_number;
int age;
float salary;
string name;
struct Employee* next;
};


struct Employee *head = NULL;
struct Employee *current = NULL;

int main()
{
int Operation = 1;

cout<<"Press 1 to add element to the front of the list"<<endl;
cout<<"Press 2 to add element to the end of the list"<<endl;
cout<<"Press 3 to add element in sorting order of the list"<<endl;
cout<<"Press 4 to delete the first element from the list"<<endl;
cout<<"Press 5 to delete the last element from the list"<<endl;
cout<<"Press 6 to delete a given element from the list"<<endl;
cout<<"Press 7 to print the list"<<endl;
cout<<"Press 8 to count the number of elements in the list"<<endl;
cout<<"Press 9 to calculate the size of the list"<<endl;
cout<<"Press 10 to sort the list"<<endl;
cout<<"Press 11 to find the minimum element in the list"<<endl;
cout<<"Press 12 to find the maximum element in the list"<<endl;
cout<<"Press -1 to exit"<<endl;

while(Operation != -1)
{
cout<<"\nChoose Operation : ";
cin>>Operation;

struct Employee Element;
switch(Operation)
{
case1 :
Input(&Element);
AddElement_Front(Element);
break;
case2 :
Input(&Element);
AddElement_End(Element);
break;
case3 :
Input(&Element);
AddElement_Sorted(Element);
break;
case4:
DeleteFirst();
break;
case5:
DeleteLast();
break;
case6:
int ID;
cout<<"Enter the ID of the employee you want to delete : ";
cin>>ID;
DeleteElement(ID);
break;
case7:
PrintList();
break;
case8:
int N;
N=ListCount();
cout<<"The number of elements in the list is : "<<N<<endl;
break;
case9:
int S;
S=ListSize();
cout<<"The size of the list is : "<<S<<" bytes"<<endl;
break;
case10:
SortList();
PrintList();
break;
case11:
Element = MinElement();
cout<<"The Employee with minimum ID : "<<endl;
Output(&Element);
break;
case12:
Element = MaxElement();
cout<<"The Employee with maximum ID : "<<endl;
Output(&Element);
break;
}
}
}

void Output(struct Employee *data_ptr)
{
cout<<"******************************\n";
cout<<"ID = "<<data_ptr->id_number<<endl;
cout<<"Name = "<<data_ptr->name<<endl;
cout<<"Age = "<<data_ptr->age<<endl;
cout<<"Salary = "<<data_ptr->salary<<endl;
cout<<"******************************\n";
}

void Input(struct Employee *data_ptr)
{
cout<<"******************************\n";
cout<<"Enter ID : ";
cin>>data_ptr->id_number;
cout<<"Enter Name : ";
cin>>data_ptr->name;
cout<<"Enter Age : ";
cin>>data_ptr->age;
cout<<"Enter Salary : ";
cin>>data_ptr->salary;
cout<<"******************************\n";
}

void AddElement_Front(struct Employee E)
{
struct Employee *temp;
temp = new Employee;
temp->id_number = E.id_number;
temp->name = E.name;
temp->age = E.age;
temp->salary = E.salary;
temp->next = head;
head = temp;
}


void AddElement_End(struct Employee E)
{
if(head==NULL)
{
struct Employee *temp;

temp = new Employee;

temp->id_number = E.id_number;
temp->name = E.name;
temp->age = E.age;
temp->salary = E.salary;
temp->next = NULL;
head = temp;
}

else{
Employee *temp1;
temp1= new Employee;
temp1 = head;

while(temp1->next!=NULL)
temp1 = temp1->next;
Employee *temp;
temp = new Employee;
temp->id_number = E.id_number;
temp->name = E.name;
temp->age = E.age;
temp->salary = E.salary;
temp->next = NULL;
temp1->next = temp;
}
}
void AddElement_Sorted(struct Employee E)
{
struct Employee *temp;
temp = new Employee;
temp->id_number = E.id_number;
temp->name = E.name;
temp->age = E.age;
temp->salary = E.salary;

temp->next = head;
head = temp;

SortList();

}
void DeleteFirst()
{

if(head==NULL)
{
cout<<"List empty"<<endl;
}
else
if(head->next==NULL)
{
head = NULL;
cout<<" first element deleted"<<endl;
cout<<"empty list"<<endl;
}

Employee *temp;
temp = new Employee;
temp = head;
head = temp->next;
free(temp);
cout<<"first element deleted"<<endl;


}
void DeleteLast()
{

if(head==NULL)
{
cout<<"List empty"<<endl;
}
else
if(head->next==NULL)
{
head = NULL;
cout<<"last element deleted"<<endl;
}


Employee *temp1;
temp1 = new Employee;
temp1 = head;

Employee *old;
old = new Employee;
while(temp1->next!=NULL)
{
old = temp1;
temp1 = temp1->next;
}

old->next = NULL;
free(temp1);
cout<<"Last element deleted"<<endl;


}

void DeleteElement(int ID)
{

//fill code
}

void PrintList()
{
Employee *temp1;
temp1 = head;
while( temp1 != NULL )
{
cout<<"********************"<<endl;
cout<<temp1->id_number<<endl;
cout<<temp1->name<<endl;
cout<<temp1->age<<endl;
cout<<temp1->salary<<endl;
cout<<"********************"<<endl;
temp1 = temp1->next;
}
}

int ListCount()
{
int N;
N = 0;
Employee *temp;
temp = new Employee;
temp = head;

while (temp->next != NULL)
{
temp = temp->next;
N++;
}
return N + 1;
}

int ListSize()
{
int S;
int Count = ListCount();
S=Count * sizeof(struct Employee);
return S;
}
void SortList()
{
Employee *temp;
Employee *temp1;
Employee *temp2;
temp = new Employee;
temp1 = new Employee;
temp2 = new Employee;

for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next )
{
for( temp2 = temp1->next ; temp2!=NULL ; temp2 = temp2->next )
{
if( temp1->id_number>temp2->id_number )
{
temp->id_number = temp1->id_number;
temp->name = temp1->name;
temp->age = temp1->age;
temp->salary = temp1->salary;
temp1->id_number = temp2->id_number;
temp1->name = temp2->name;
temp1->age = temp2->age;
temp1->salary = temp2->salary;
temp2->id_number = temp->id_number;
temp2->name = temp->name;
temp2->age = temp->age;
temp2->salary = temp->salary;

}
}
}

}
struct Employee MinElement()
{
struct Employee min;

Employee *temp;
Employee *temp1;
Employee *temp2;

temp = new Employee;
temp1 = new Employee;
temp2 = new Employee;

int tid = head->id_number;

for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next )
{
if( temp1->id_number<tid )
{
tid = temp1->id_number;
}
if (temp1->id_number == tid)
{
min.id_number = temp1->id_number;
min.name = temp1->name;
min.age = temp1->age;
min.salary = temp1->salary;
}
}

return min;
}
struct Employee MaxElement()
{
struct Employee max;

Employee *temp,*temp1,*temp2;
temp = new Employee;
temp1 = new Employee;
temp2 = new Employee;
int tid = 0;

for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next )
{
if( temp1->id_number>tid )
{
tid = temp1->id_number;
}
if (temp1->id_number == tid)
{
max.id_number = temp1->id_number;
max.name = temp1->name;
max.age = temp1->age;
max.salary = temp1->salary;
}
}


return max;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
solimanmuttawa is offline Offline
3 posts
since Nov 2008
Nov 29th, 2008
0

Re: i need help pls LINKED List

Code tags and formatting please.


[code=cplusplus]
// paste code here.
[/code]


You also need to give us a much more detailed description of what the problem is.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Nov 29th, 2008
0

Re: i need help pls LINKED List

first, you need to enclose the body of a case statement in curly braces if you want to delare a variable within the body of the case statement. The best answer why I can give you is "because".

The algorithm for deleting an arbitrary node from a singly linked list is to find the desired node and the node before the desired node (unless of course the desired node is the first node). Then assign desired->next to previous->next and delete the desired node.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Rapid XOR'ed encryption cracking.
Next Thread in C++ Forum Timeline: Sizeof operator help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC