aboutsummaryrefslogtreecommitdiffstats
path: root/queue.c
diff options
context:
space:
mode:
authorAdam Branes <adam@adam>2021-05-02 17:45:58 +0300
committerAdam Branes <adam@adam>2021-05-02 17:45:58 +0300
commita3e36c1918e63761dfc4d2221cca3636b98e93aa (patch)
tree143722876a02156b0b423de6248298b5cf8c0707 /queue.c
downloadMEGATRON-a3e36c1918e63761dfc4d2221cca3636b98e93aa.tar.gz
initial housekeeping done
Diffstat (limited to 'queue.c')
-rw-r--r--queue.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/queue.c b/queue.c
new file mode 100644
index 0000000..187519a
--- /dev/null
+++ b/queue.c
@@ -0,0 +1,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;
+
+ 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