i need help pls LINKED List

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 3
Reputation: solimanmuttawa is an unknown quantity at this point 
Solved Threads: 0
solimanmuttawa solimanmuttawa is offline Offline
Newbie Poster

i need help pls LINKED List

 
0
  #1
Nov 29th, 2008
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: i need help pls LINKED List

 
0
  #2
Nov 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: i need help pls LINKED List

 
0
  #3
Nov 29th, 2008
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 308 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC