Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program to create a dynamic queue to store non-zero positive integers.

ID: 3688815 • Letter: W

Question

Write a C program to create a dynamic queue to store non-zero positive integers. The input and output is shown below. Each line is an input followed by a single line output, where C is used to create a queue and it is followed by a non-zero positive size of the queue, I is used to write values into the queue and it is followed by the input value, R is used to read the head of the queue, P is used to print the queue contents, L is used to get the length of the queue, and M is used to modify the lengthPage2of3of the queue and it is followed by the new size of the queue(new size > old size).Comment your code.

C 3

0 0 0

I 2

0 0 2

R

2

R

Empty

I 2

0 0 2

I 3

0 3 2

I 4

4 3 2

I 5

Full

P

4 3 2

L

3

M 4

0 4 3 2

I 5

5 4 3 2

Explanation / Answer

Hi below i have written a sample program with for your reference

#include<stdio.h>
#include<conio.h>

typedef struct NUM

{ int data;
struct NUM *link;

}num;


num *createnode()
{
num *nn=NULL;
nn=(num*)malloc(sizeof(num));
if(nn==NULL)
{
printf(“ insufficient memory”);
exit(0);
}//end if
return(nn) ;

} //end createnode

num *create()
{ num *hn=NULL ,*cn=NULL ,*nn=NULL;
int i,n;
printf(“ enter no of node “);
scanf(“%d”,&n);
printf(“ enter data ”);
for(i=0;i<n;i++)
{
nn=createnode();
scanf(“%d”,&nn->data);
nn->link=NULL;
if(hn==NULL)
{
hn=nn;
cn=nn;
}//end if
else
{
cn->link=nn;
cn=nn;
}
}//end for
return(hn);
}//end create
void display(num *hn)
{
num *cn=NULL;
int cnt_nonzero=0,cnt_even=0,cnt_odd=0;
num *temp=NULL;
printf(“ data present in link list is “);
for(cn=hn;cn!=NULL;cn=cn->link)
{
printf(” %d”,cn->data);
}//end for
/*following code counts the nonzero,even,odd*/
temp=hn;
while(temp!=NULL)
{
if(temp->data!=0)
{
cnt_nonzero++;
}
if(temp->data!=0)
{
if((temp->data)%2==0 )
{

cnt_even++;
}
if((temp->data)%2!=0)
{
cnt_odd++;
}
}

temp=temp->link;
}

printf(“ The number of non-zero elements is %d”,cnt_nonzero);
printf(“ The number of even elements is %d”,cnt_even);
printf(“ The number of odd elements is %d”,cnt_odd);
}//end display
void main()
{
num *hn,*nn,*pn;
int ch;
clrscr();
hn= create();
display(hn);

getch();
} //end main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote