本篇文章給大家談談fifo算法,以及fifo算法模擬對應的知識點,希望對各位有所幫助,不要忘了收藏本站!
內容導航:- fifo算法怎麽寫
- FIFO算法的解釋
- 操作係統先進先出(FIFO)和先來先服務(FCFS)有什麽區別
- FIFO調度算法和LRU算法?
- FIFO和LRU小結
- FIFO算法(假定開始時先把1,2,3,4號頁麵裝入內存)
Q1:fifo算法怎麽寫
用C語言編寫簡單的FIFO置換算法#include "stdio.h"
#include "malloc.h"
#define OK 1
#define ERROR 0
#define NULL 0
#define status int
typedef int Elemtype;
/*這個定義的是隊列的元素的數據結構*/
typedef struct tailDATA{
Elemtype data;/*這個存放的是隊列元素的值*/
struct tailDATA *next;//指向下一個元素
}datatail,*map;
/*以下定義的是隊列頭的數據結構*/
typedef struct Tail{
/*說明:對隊列進行操作的時候,插入的時候是對front操作,刪除*/
Elemtype data;/*這個記載的是隊列的元素的個數*/
map front;/*這個是隊列的頭*/
map rear;/*這個是隊列的尾*/
}tail,*mappath;
/*以下定義的就是操作了,初始話的操作就不想做了,直接寫個插入和刪除等的一些的算法就可以了*/
status inserttail(mappath &T,map P)
{/*這個函數的功能是將一個個已知的元素插入隊列中*/
if(T==NULL)
{
T=(mappath)malloc(sizeof(tail));
T->data=0;
T->front=NULL;
T->rear=NULL;
}
if(!P) return OK;
T->rear->next=P;
T->rear=P;
if(!(T->front)) T->front=P;
return OK;
}
status insertdatatail(mappath &T,int a)
{/*這個函數將一個元素插入隊列中,其實這個函數是沒有必要的,但是為了方便起見,還是寫了個*/
if(T==NULL)
{
T=(mappath)malloc(sizeof(tail));
T->data=0;
T->front=NULL;
T->rear=NULL;
map linshi=(map)malloc(sizeof(datatail));
linshi->data=a;
linshi->next=NULL;
T->front=linshi;
T->rear=linshi;
T->data=1;
return OK;
}
map linshi=(map)malloc(sizeof(datatail));
linshi->data=a;
linshi->next=NULL;
T->rear->next=linshi;
T->rear=linshi;
if(!(T->front)) T->front=linshi;
T->data++;
return OK;
}
status deltail(mappath &T)
{/*因為對隊列進行刪除操作的時候,基本上是沒有什麽條件,就是對front做一些相應的操作就可以了
,所以他的函數列表也就比較少了*/
if(!T) return ERROR;/*如果隊列本來就是空的,那麽就返回一個錯誤的信息*/
if(T->front==T->rear)
{/*如果隊列隻有一個元素,就執行下麵的操作,防止出現了錯誤*/
map linshi=T->front;
free(linshi);
T->data=0;
T->front=NULL;
T->rear=NULL;
return OK;
}
map linshi=T->front;
T->front=T->front->next;
T->data--;
free(linshi);
return OK;
}
status puttail(mappath T)
{/*這個是對一個已經存在的隊列進行輸出*/
if(!T) return ERROR;
printf("the tail'count is %d
",T->data);
int count=T->data;map q=T->front;
for(int i=0;i<count;i++)
{
printf("%d",q->data);
q=q->next;
}
return OK;
}
int main()
{
printf("hello,world!
");
mappath q=NULL;int count1=0;int dataa=0;
printf("please input a number to the count of tail
");
scanf("%d",&count1);
for(int i=0;i<count1;i++)
{
printf("please input a number to tail
");
scanf("%d",&dataa);
insertdatatail(q,dataa);
}
puttail(q);
deltail(q);
puttail(q);
return 0;
}
还没有评论,来说两句吧...