this is my sample code to create a student record using singly linked list 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 *next;
}*front,*rear,*temp,*front1;
struct node *rear=NULL;
struct node *tail=NULL;
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()
{
struct node* rear;
int ch, pos,i,e;
char no[12], name[24],crsyr[10];
float gwa,tgwa;
create();
while (1)
{
system("cls");
printf("\n 1 - Insert Student Record");
printf("\n 2 - Delete Student Record");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Remove Duplicate");
printf("\n 6 - Display Record Student");
printf("\n 7 - Queue size");
printf("\n 8 - Exit");
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
system("cls");
printf ("how Many student do you want to enter?: ");
int n;
scanf("%d",&n);
for(int i=0; i<n; ++i)
{
system("cls");
printf("Student %d",count+1);
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:
system("cls");
del();
break;
case 3:
system("cls");
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:
system("cls");
removeduplicate();
display();
printf("Press any key to contiue...");
getch();
break;
case 6:
system("cls");
display();
printf("Press any key to contiue...");
getch();
system("cls");
break;
case 7:
queuesize();
break;
case 8:
exit(0);
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->next = 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->next = temp;
strcpy(temp->No, data);
strcpy(temp->Name, name);
strcpy(temp->crsysr, yearsec);
temp->gwa=gwa;
temp->next = 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("%.2f \t", front1->gwa);
front1 = front1->next;
printf("\n");
}
if (front1 == rear)
printf("%s \t",front1->No);
printf("%s \t",front1->Name);
printf("%s \t",front1->crsysr);
printf("%.2f\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->next != NULL)
{
front1 = front1->next;
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() {
// empty list
if (rear == NULL) return;
// element with one list .. nothing to remove
if (rear == tail) return;
struct node * previous = rear;
struct node * current = rear->next;
struct node * newcurrent = rear;
struct node * temp;
while (current != NULL) {
newcurrent = rear;
while (newcurrent != current) {
if (strncmp(newcurrent->No == current->No)) {
temp = current;
previous->next = current->next;
current = current->next;
printf("Deleting %s \n", temp->No);
free(temp);
break;
}
newcurrent = newcurrent->next;
}
if (newcurrent == current) {
previous = current;
current = current->next;
}
}
tail = previous;
}