• 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("
    STACKpop 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);
    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 }
  • 相关阅读:
    解决Linux Ubuntu 突然出现死机无法进入系统的问题(sysrq magic key)
    [python笔记]文件处理
    [python笔记]pandas应用
    将Excel中列数转为列号
    Linq学习笔记
    install chrome for opensuse 12.1
    Word 文档拆分
    原型模式?java深克隆和浅克隆
    单例模式?学习懒汉式和饿汉式
    简单工厂模式?接口引用指向实例对象(面向接口)
  • 原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/8668011.html
Copyright © 2020-2023  润新知