Advertisement
Jerkiller

2.1 (esercitazione 2 esercizio 1)

Dec 3rd, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "queue.h"
  4.  
  5. struct queue{
  6.     struct node *head;
  7.     struct node *tail;
  8.     int size;
  9. };
  10.  
  11. struct node{
  12.     char *info;
  13.     struct node *next;
  14. };
  15.  
  16. typedef struct node *Node;
  17.  
  18. /* post: costruisce una coda vuota */
  19. Queue initqueue(){
  20.  Queue q;
  21.  q=malloc(sizeof(struct queue));
  22.  q->head=NULL;
  23.  q->tail=NULL;
  24.  q->size=0;
  25.  return q;
  26. }
  27.  
  28. /*post: ritorna 1 se la coda e' vuota, 0 altrimenti */
  29. int queueEmpty(Queue q){ if(q->size==0)return 1; else return 0; }
  30.  
  31. /*post: restituisce il numero di elementi nella coda */
  32. int size(Queue q){ return q->size; }
  33.  
  34. /*post: inserisce elem in coda */
  35. void enqueue(Queue q, char * elem){
  36.     Node new;
  37.     new=malloc(sizeof(struct node));
  38.     new->info=malloc(sizeof(char *));
  39.     strcpy(new->info,elem);
  40.     new->next=NULL;
  41.     if(q->size==0){ q->head=new; }
  42.     else{q->tail->next=new;}
  43.     q->tail=new;
  44.     q->size++;
  45.     return;
  46. }
  47.  
  48. /*pre: coda non vuota  */
  49. /*post: restituisce il primo elemento in coda */
  50. char * first(Queue q){ return q->head->info; }
  51.  
  52. /*pre: coda non vuota  */
  53. /*post: restituisce e rimuove il primo elemento in coda */
  54. char * dequeue(Queue q){
  55.     Node temp; char *tz;
  56.     temp=malloc(sizeof(struct node));
  57.     tz=q->head->info;
  58.     if(q->size==1)q->head=NULL;
  59.     else q->head = q->head->next;
  60.     q->size--;
  61.     free(temp);
  62.     return tz;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement