Implementation of the ADT interface linked list of the FIFO queue

Source: Internet
Author: User

Implementation of the ADT interface linked list of the FIFO queue

FIFO. h (Interface)

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

Item. h (custom type)

1 typedef char Item;

FIFO. c (interface implementation)

 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 (main Program)

 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 }

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.