0

Can you tell me if this is correct way to do?

This is my sample code in student information in queue. my problem is that I want to insert a student number in the struct but it doesn't repeat when I input the same student number.

#include <stdio.h>
#include <stdlib.h>

struct node
{

    char No[12],Name[24],crsysr[10];
    float gwa;
    struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void insert(char *data, char *name,char *yearsec,float gwa);
void del();
void empty();
void display();
void create();
void queuesize();
void removeduplicate();
int count = 0;

void main()
{
    int  ch, pos,i,e;
    char no[12], name[24],crsyr[10];
    float gwa,tgwa;
    create();
    while (1)
    {
            printf("\n 1 - Enque");
            printf("\n 2 - Deque");
            printf("\n 3 - Front element");
            printf("\n 4 - Empty");
            printf("\n 5 - Exit");
            printf("\n 6 - Display");
            printf("\n 7 - Queue size");
            printf("\n Enter choice : ");
            scanf("%d", &ch);


        switch (ch)
        {
        case 1:
            printf ("how Many student do you want to enter?: ");
            int n;
            scanf("%d",&n);
            for(int i=0; i<n; ++i)
            {
            fflush(stdin);
            printf("\n Enter Student number : ");
            gets(no);
            printf("\n Enter Student name : ");
            gets(name);
            printf("\n Enter Student Year and Sec : ");
            gets(crsyr);
            fflush(stdin);
            printf("\n Enter Student gwa : ");
            scanf("%f", &gwa);
            insert(no, name, crsyr, gwa);
            count++;
            }
            printf("Press any key to contiue...");
            getch();
            system("cls");
            break;
        case 2:
            del();
            break;
        case 3:
            e = frontelement();
            if (e != 0)
                printf("Front element : %d", e);
            else
                printf("\n No front element in Queue as queue is empty");
                system("cls");
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            system("cls");
            removeduplicate(temp->ptr);
            display();
            printf("Press any key to contiue...");
            getch();
            system("cls");
            break;
        case 7:
            queuesize();
            break;

        default:
            printf("Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}


void create()
{
    front = rear = NULL;
}
void queuesize()
{
    printf("\n Queue size : %d", count);
}


void insert(char *data, char *name,char *yearsec, float gwa)
{
    if (rear == NULL)
    {
        rear = (struct node *)malloc(1*sizeof(struct node));
        rear->ptr = NULL;
        strcpy(rear->No, data);
        strcpy(rear->Name, name);
        strcpy(rear->crsysr, yearsec);
        rear->gwa=gwa;
        front = rear;
    }
    else
    {
        temp=(struct node *)malloc(1*sizeof(struct node));
        rear->ptr = temp;
        strcpy(temp->No, data);
        strcpy(temp->Name, name);
        strcpy(temp->crsysr, yearsec);
        temp->gwa=gwa;
        temp->ptr = NULL;

        rear = temp;
    }
}


void display()
{
    front1 = front;
    int i;
    if ((front1 == NULL) && (rear == NULL))
    {
        printf("Queue is empty");
        return;
    }
        printf("Student Number\t\tName\t\tSection\t\tGwa\n\n");

    while (front1 != rear)
    {
        printf("%s \t",front1->No);
        printf("%s \t",front1->Name);
        printf("%s \t",front1->crsysr);
        printf("%f \t", front1->gwa);
        front1 = front1->ptr;
        printf("\n");


    }
    if (front1 == rear)
        printf("%s \t",front1->No);
        printf("%s \t",front1->Name);
        printf("%s \t",front1->crsysr);
        printf("%f\t", front1->gwa);
        printf("\n");
}

void del()
{
    front1 = front;

    if (front1 == NULL)
    {
        printf("\n Error: Trying to display elements from empty queue");
        return;
    }
    else
        if (front1->ptr != NULL)
        {
            front1 = front1->ptr;
            printf("\n Dequed Student num : " );
            puts(front->No);
            printf("\n Dequed Name : ");
            puts(front->Name);
            printf("\n Dequed Year and section : ");
            puts(front->crsysr);
            printf("\n Dequed GWA : %f", front->gwa);

            free(front);
            front = front1;
        }
        else
        {
            printf("\n Dequed Student Num : ", front->No);
            printf("\n Dequed Name : ", front->Name);
            printf("\n Dequed Year and section :", front->crsysr);
            printf("\n Dequed GWA : %f", front->gwa);
            free(front);
            front = NULL;
            rear = NULL;
        }
        count--;
}

int frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->No,front->Name,front->crsysr,front->gwa);
    else
        return 0;
}

void empty()
{
     if ((front == NULL) && (rear == NULL))
        printf("\n Queue empty");
    else
       printf("Queue not empty");
}

void removeduplicate(struct node *front) {
    struct node *ptr1, *ptr2, *duplicate;
ptr1 = front;

while (ptr1 != NULL && ptr1->ptr != NULL)
{
ptr2 = ptr1;

/* Compare the current element with rest of the elements */
while (ptr2->ptr != NULL)
{
    if (strcmp(ptr1->No == ptr2->ptr->No && ptr1->Name == ptr2->ptr->Name && ptr1->crsysr == ptr2->ptr->crsysr && ptr1->gwa == ptr2->ptr->gwa)==0)
    {
    duplicate = ptr2->ptr;
    ptr2->ptr = ptr2->ptr->ptr;
    free(duplicate);
    } else
    {
    ptr2 = ptr2->ptr;
    }
ptr1 = ptr1->ptr;
}
}
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    _Side note:_ Do _not_ use `gets`: https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – Craig Estey Jun 20 '21 at 00:35
  • 1
    Also, _never_ do `fflush(stdin);` See: https://stackoverflow.com/questions/2979209/using-fflushstdin And, don't intermix `scanf` and `fgets` [nee `gets`]. – Craig Estey Jun 20 '21 at 00:39
  • Don't call the parameter to `removeduplicate` by the name `front`. It conflicts with the global `front`. When removing an element, you may need to adjust the global `front/rear` variables and with the naming you have, this may be problematic. – Craig Estey Jun 20 '21 at 00:46

0 Answers0