设为首页 加入收藏

TOP

FIFO队列 ADT接口 链表实现
2018-10-21 16:10:00 】 浏览:37
Tags:FIFO 队列 ADT 接口 实现

FIFO.h (接口)

1 #include "Item.h" 2 void QUEUinit(int); 3 int QUEUempty(void); 4 void QUEUput(Item); 5 Item QUEUget(void);

Item.h (自定义类型)

1 typedef char Item;

FIFO.c (接口实现)

 1 #include "FIFO.h"  2 #include <stdlib.h>  3  4 typedef struct STACKnode *link;  5 struct STACKnode  6 {  7  Item item;  8  link next;  9 }; 10 11 static link head,tail; 12 static int N=1,N1; 13 14 static int STACKerror(int i) 15 { 16 if(i) 17 return N<N1?1:0; 18 19 else 20 return N>0 ?1:0; 21 } 22 link NEW(Item item, link next) 23 { 24 link x = malloc(sizeof *x); 25 x->item=item; x->next=next; 26 return x; 27 } 28 void QUEUinit(int maxN) 29 { 30 N1=maxN; 31 head=NULL; 32 } 33 int QUEUempty(void) 34 { 35 return N; 36 } 37 void QUEUput(Item item) 38 { 39 if(head==NULL) 40  { 41 head=(tail=NEW(item, head)); 42 return ; 43  } 44 tail->next=NEW(item, tail->next); 45 tail=tail->next; 46 N++; 47 } 48 Item QUEUget(void) 49 { 50 if(STACKerror(0)) 51  { 52 Item item=head->item; 53 link t=head->next; 54 free(head);head=t; 55 N--; 56 return item; 57  } 58 else 59 printf("\nSTACKpop false"); 60 return NULL; 61 }

main.c (主程序)

 1 #include <stdio.h>  2 #include "FIFO.h"  3  4 int main(void)  5 {  6 int N;  7 Item str[11];  8 scanf("%s", str);  9  getchar(); 10 11 N=sizeof(str)/sizeof(str[0]); 12 printf("%d\n",N); 13 14  QUEUinit(N); 15 for(int i=0; i<N; i++) 16  { 17  QUEUput(str[i]); 18  } 19 for(int i=0; i<N; i++) 20  { 21 printf("%c",QUEUget()); 22  } 23 24 return 0; 25 }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇数据结构学习 下一篇计蒜客——字符串长度

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目