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;


}

[COLOR="Red"]void DeleteElement(int ID)[/COLOR]
{

    //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;
}

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.