aboutsummaryrefslogtreecommitdiffstats
path: root/src/misc/queue.c
blob: b395acf0cb9f4c0e324751dac8d5e502df37aad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef GQUEUE_C
#define GQUEUE_C GQUEUE_C
#include<queue.h>


void Queue_Init(struct Queue *q)
{
	q->first=q->last=NULL;
	q->size=0;
}
void Queue_Push(struct Queue *q,void *data)
{
	if(q==NULL)return;
	if(q->first==NULL)
	{
		q->first=q->last=malloc(sizeof(struct Queue_Node));	

		q->first->data=data;
		q->first->prev=NULL;

		++q->size;
	}else
	{
		struct Queue_Node *temp=malloc(sizeof(struct Queue_Node));
		q->last->prev=temp;
		temp->data=data;
		temp->prev=NULL;
		q->last=temp;
		++q->size;
	}

}
void* Queue_Pop(struct Queue *q)
{
	if(q==NULL)return NULL;
	if(q->size==0)return NULL;

	void *return_value=q->first->data;

	if(q->size==1)
	{
		free(q->last);
		q->first=q->last=NULL;
		q->size=0;
	}else
	{
		struct Queue_Node *temp_first=q->first;
		q->first=q->first->prev;
		free(temp_first);
		--q->size;
	}
	return return_value;
}
void Queue_Destroy(struct Queue *q)
{
	
	struct Queue_Node *temp_first;
	while(q->first!=NULL)
	{
		temp_first=q->first;
		q->first=q->first->prev;
		free(temp_first->data);
		free(temp_first);	
	}	

}
#endif