In C, create a sequence that inputs and outputs character stream. The synchroniz
ID: 3816317 • Letter: I
Question
In C, create a sequence that inputs and outputs character stream. The synchronization data structure between the IO stream is a FIFO which has to be designed in C. The FIFO should be designed as dynamic memory and the maximum depth should be 16 characters.
FIFO operating conditions are such that, when FIFO is full, it should stop the input stream until the FIFO is empty and when the empty it should stop output stream until the FIFO is half full (8 characters).
Create two test method to this interface, in first method the input stream is 4 characters and output stream is 2 characters; in the second method the input stream is 2 characters and output stream is 6 characters.
Your output should include printf statements that prints if it is the input or output stream and the current size of the FIFO for 2 full conditions for the first test and 3 empty conditions for second test.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
struct fifo {
char c_ch;
struct fifo *next;
};
struct fifo *start=NULL;
void insert(int c){
int a=0,i;
struct fifo *p=start;
while(p!=NULL){
a++;
p=p->next;
}
if((a+c)<=16){
printf(" INPUT STREAM ");
for(i=0;i<c;i++){
add();
}
}
else{
printf(" List cannot be added ***********ERROR*************** ");
}
}
void add(){
struct fifo *temp,*p;
temp=(struct fifo*)malloc(sizeof(struct fifo));
scanf("%c",&temp->c_ch);
if(start==NULL){
start=temp;
}
else{
p=start;
while(p!=NULL){
p=p->next;
}
p=temp;
}
}
void display(){
struct fifo *temp,*p;
int d=0,i,a=0;
again :
printf(" *****************Note:Elements that are printed will be deleted ");
printf(" Enter the number of chars you want to display :");
scanf("%d",&d);
while(p!=NULL){
a++;
p=p->next;
}
printf(" ");
printf(" OUTPUT STREAM ");
temp=start;
if(a>d)
for(i=0;i<d;i++){
printf("%c ",temp->c_ch);
del();
}
else{
printf(" Not Enough chars to display try again ");
goto again;
}
}
void del(){
struct fifo *temp;
temp=start->next;
start=temp;
}
void main(){
int c;
printf("Enter the input stream of chars in a sequence: ");
printf("MAX limit for the chars is 16 ");
while(1){
printf("Enter the max number of chars you want to enter :");
scanf("%d",&c);
printf(" ");
insert(c);
display();
}
return 0;
}
typically I wrote a code for manually entering the printing and deleting elements. as mentioned in question for second test case it is not possible to print 6 chars out because enter and deleting is powers of 2 and decreasing the number.