読者です 読者をやめる 読者になる 読者になる

stack,queueの実装

出来上がったと思うので一応うp

 

stack

 

#define make_stack(x); stack x;x.last=NULL;
#define make_stack_p(x); stack *(x)=(stack*)malloc(sizeof(stack));(x)->last=NULL;
#define break_stack(x); while(top_stack(&x)!=0x00000000){pop_stack(&x);}
#define break_stack_p(x); while(top_stack(x)!=0x00000000){pop_stack(x);}free(x);

struct _Stack{
  void *content;
  struct _Stack *front;
};
 
typedef struct{
  struct _Stack *last;
}stack;
 
void *top_stack(stack *st){
  if(st->last == NULL){
    return (void*)0x00000000;
  }
  return st->last->content;
}


int push_stack(stack *st, void *variable){
  struct _Stack *tmp;
  tmp = (struct _Stack*)malloc(sizeof(struct _Stack));
  if(tmp == NULL)return -1;
  if(st->last == NULL){
    tmp->front = NULL;
  }else{
    tmp->front = st->last;
  }
  tmp->content = variable;
  st->last = tmp;
  return 0;
}

void *pop_stack(stack *st){
  if(st->last == NULL)return NULL;
  if(st->last->front == NULL){
    st->last = NULL;
  }else{
    struct _Stack *tmp = st->last;
    st->last = st->last->front;
    free(tmp);
  }
  return NULL;
}

 

 

queue

 

#define make_queue(x) queue x;x.first=NULL
#define make_queue_p(x) queue *(x)=(queue*)malloc(sizeof(queue));(x)->first=NULL
#define break_queue(x) while(top_queue(&x)!=0x00000000){pop_queue(&x);}
#define break_queue_p(x) while(top_queue(x)!=0x00000000){pop_queue(x);}free(x);

struct _Queue{
  void *content;
  struct _Queue *next;
};
 
typedef struct{
  struct _Queue *first;
  struct _Queue *last;
}queue;
 
void *top_queue(queue *que){
  if(que->first == NULL)return 0x00000000;
  return que->first->content;
}
 
int push_queue(queue *que, void *variable){
  struct _Queue *tmp;
  tmp = (struct _Queue*)malloc(sizeof(struct _Queue));
  if(tmp == NULL)return -1;
  tmp->content = variable;
  if(que->first == NULL)que->first = tmp;
  que->last = tmp;
  return 0;
}
 
void *pop_queue(queue *que){
  if(que->first == NULL)return NULL;
  if(que->first->next == NULL){
    que->first = NULL;
    que->last = NULL;
  }else{
    struct _Queue *tmp = que->first;
    que->first = que->first->next;
    free((void*)tmp);
  }
  return NULL;
}