Q1

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.7 kB
7
Indexable
Never
#include <iostream>
using namespace std;

struct data
{
    float mark;
    data *next;
};

void push(data **head, data** tail, float mark)
{
    data* n = new data();
    n->mark = mark;
    n->next = NULL;
    
    /*insertion process*/
    if(*head == NULL)
        *head = *tail = n;
    else
    {
        (*tail)->next = n;
        *tail = n;
    }
}

void grading(data *head)
{
    data *p;
    int l,g;
    p=head;

    cout << "The gradings: " << endl;
    while(p != NULL)
    {
        
        cout << endl;
        cout << "Grading : " << p-> mark;
        if((p->mark>=50.0))
        {
            cout << " (Pass) "<< endl;
            l++;
        }
        else if((p->mark<50.0))
        {
            cout << " (Fail) "<< endl;
            g++;
        }    
        p=p->next;
    }
    cout << "Total Pass: " << l << " students." <<endl;
    cout << "Total Fail: " << g-1 << " students." <<endl;
}

void pop(data **head, data **tail)
{
    data *p;
    while(*head!=NULL)
    {
        p = *head;
        *head = p->next;
        free(p);

        if (*head == NULL)
        {
            *tail ==NULL;
        }
    }

    if(*head ==NULL)
    {
        cout<<"Queue is empty";
    }
}

int main()
{
    data *head = NULL, *tail=NULL;
    int size;
    float mark;

    cout<<"Enter total data to insert: ";
    cin>> size;
    for(int i = 1; i<=size; i++)
    {
        cout<<"Enter mark : ";
        cin>>mark;
        push(&head,&tail,mark);
    }
    grading (head);
    cout<<"Removing all marks..."<< endl;
    pop(&head,&tail);
    cout<<"\nEnd of program";
    return 0;
}