Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct fifo_node
- {
- int data;
- struct fifo_node *next;
- };
- struct fifo_pointers
- {
- struct fifo_node *head, *tail;
- } fifo;
- void enqueue(struct fifo_pointers *fifo, int data)
- {
- struct fifo_node *new_node =
- (struct fifo_node *)malloc(sizeof(struct fifo_node));
- if(new_node) {
- new_node->data = data;
- new_node->next = NULL;
- if(fifo->head==NULL) fifo->head = fifo->tail = new_node;
- else {
- fifo->tail->next=new_node;
- fifo->tail=new_node;
- }
- } else
- fprintf(stderr,"Nowy element nie został utworzony!\n");
- }
- int dequeue(struct fifo_pointers *fifo)
- {
- if(fifo->head) {
- struct fifo_node *tmp = fifo->head->next;
- int data = fifo->head->data;
- free(fifo->head);
- fifo->head=tmp;
- if(tmp==NULL)
- fifo->tail = NULL;
- return data;
- }
- return -1;
- }
- void print_queue_req(struct fifo_pointers tmp)
- {
- if(tmp.head != NULL) return 0;
- printf("%d ",tmp.head->data);
- fifo.head = tmp.head->next;
- print_queue_req(tmp);
- }
- int main(void)
- {
- int i;
- for(i=0;i<20;i++)
- enqueue(&fifo,i);
- print_queue_req(fifo);
- while(fifo.head)
- printf("%d ",dequeue(&fifo));
- puts("");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement