Code:
#include <stdio.h>#include <stdlib.h>
//Linked List data structure declaration
struct node
{
int num;
struct node *next;
};
// Function Prototype declaration
void create(struct node **);
void display_valid(struct node *, const char * );
void display_invalid (struct node *, const char * );
//Main Program
int main()
{
struct node *p = NULL;
create(&p);
display_valid(p, "Linked-List of valid Scores => %d");
display_invalid(p, "Linked-List of invalid Scores => %d");
return 0;
}
// Function to Insert
void create(struct node **head)
{
int c,ch;
struct node *temp, *rear;
do
{
printf("Enter Number:");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp -> num = c;
temp -> next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]:");
scanf("%d", &ch);
}while(ch !=0);
printf("\n");
}
// Function to Display
void display_valid(struct node *p, const char * str)
{
if(p -> num >= 0 && p -> num <= 50)
{
printf(str, p->num);
if ( p-> next)
{
display_valid(p->next, "%d");
}
else
{
if (p->next)
{
display_valid(p->next, str);
}
else
{
fputs("\n", stdout);
}
}
}
void display_invalid (struct node *p, const char * str)
{
if(p -> num<0 || p -> num > 50)
{
printf(str, p -> num);
if (p -> next)
{
display_invalid(p->next, "%d");
}
else
{
fputs("\n",stdout);
}
}
else
{
if (p -> next)
{
display_invalid(p->next, str);
}
else
{
fputs("\n", stdout);
}
}
}
can someone help me to fix this program?
In the following code, it required for users to enter scores between 0 -50 which should print valid link list and if values beyond the range are entered, should be flagged as invalid and print invalid list.
I WANT OUTPUT TO BE LIKE THIS:
Linked-List of valid scores 0->30->27->43-> END OF VALID LIST
Linked-List of invalid scores 0->55->65->66->END OF ERROR LIST