Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include "queue.h"
- struct queue{
- struct node *head;
- struct node *tail;
- int size;
- };
- struct node{
- char *info;
- struct node *next;
- };
- typedef struct node *Node;
- /* post: costruisce una coda vuota */
- Queue initqueue(){
- Queue q;
- q=malloc(sizeof(struct queue));
- q->head=NULL;
- q->tail=NULL;
- q->size=0;
- return q;
- }
- /*post: ritorna 1 se la coda e' vuota, 0 altrimenti */
- int queueEmpty(Queue q){ if(q->size==0)return 1; else return 0; }
- /*post: restituisce il numero di elementi nella coda */
- int size(Queue q){ return q->size; }
- /*post: inserisce elem in coda */
- void enqueue(Queue q, char * elem){
- Node new;
- new=malloc(sizeof(struct node));
- new->info=malloc(sizeof(char *));
- strcpy(new->info,elem);
- new->next=NULL;
- if(q->size==0){ q->head=new; }
- else{q->tail->next=new;}
- q->tail=new;
- q->size++;
- return;
- }
- /*pre: coda non vuota */
- /*post: restituisce il primo elemento in coda */
- char * first(Queue q){ return q->head->info; }
- /*pre: coda non vuota */
- /*post: restituisce e rimuove il primo elemento in coda */
- char * dequeue(Queue q){
- Node temp; char *tz;
- temp=malloc(sizeof(struct node));
- tz=q->head->info;
- if(q->size==1)q->head=NULL;
- else q->head = q->head->next;
- q->size--;
- free(temp);
- return tz;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement