-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIFO.c
103 lines (88 loc) · 1.72 KB
/
FIFO.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Suzana Nesic
// April 14, 2017
// ECGR 3101 Homework 6
#include <stdio.h>
#include <stdlib.h>
struct FIFOstruct {
char character;
struct FIFOstruct *next;
}*head, *tail, *temp;
void FIFO();
void add(char ch);
void rem();
int counter=0;
int main(){
int i,j,k,l,s=0;
printf("*********Test case 1*********\n");
printf("Inputting 4 characters and outputting 2 characters\n");
FIFO();
for(j=0;j<2;j++) {
add('a');
counter=counter+4;
printf("Input: FIFO size : %d\n", counter);
}
do {
rem();
counter=counter-2;
printf("Output: FIFO size : %d\n", counter);
add('a');
counter=counter+4;
printf("Input: FIFO size : %d\n", counter);
}while(counter<=14);
if(counter>14) {
printf("Fifo full\n");
}
printf("*********Test case 2*********\n");
printf("Inputting 2 characters and outputting 6 characters\n");
FIFO();
counter=0;
for(s=0;s<4;s++) {
add('b');
counter=counter+2;
printf("Input: FIFO size : %d\n", counter);
}
do {
rem();
counter=counter-6;
if(counter<0) {
counter=0;
printf("Output: FIFO size : %d\n", counter);
printf("Fifo empty\n");
break;
}
printf("Output: FIFO size : %d\n", counter);
add('a');
counter=counter+2;
printf("Input: FIFO size : %d\n", counter);
} while (counter >=0);
return 0;
}
void FIFO() {
tail = NULL;
head = NULL;
printf("Fifo Empty\n");
}
void add(char ch) {
temp= (struct FIFOstruct *)malloc(sizeof(struct FIFOstruct));
temp->character = ch;
temp->next = NULL;
if (head==NULL) {
tail = temp;
head = tail;
}
else {
tail->next=temp;
tail=temp;
}
}
void rem() {
if (temp == NULL) {
printf("Error: empty FIFO\n");
return;
}
else {
temp = temp->next;
free(head);
head = temp;
}
}