Skip to content

Instantly share code, notes, and snippets.

@ashutoshbsathe
Created October 6, 2017 04:00
Show Gist options
  • Save ashutoshbsathe/b6b68d6cf8fc20eed16956f0267dc075 to your computer and use it in GitHub Desktop.
Save ashutoshbsathe/b6b68d6cf8fc20eed16956f0267dc075 to your computer and use it in GitHub Desktop.
Queue using structures and pointers (For Moodle Discussion Forum)
#include "queue.h"
#include <stdlib.h>
/*
* In queue.h
* Queue is defined as
* typedef struct queue {
* node *head;
* node *tail;
* node *tmp;
* } queue;
*/
void qinit(queue *a) {
a->head = NULL;
a->tail = NULL;
a->tmp = NULL;
}
void enq(queue *a, data b) {
/*
* Make a->tmp ass your temporary node
* Put all your data in it
* Link it properly
*/
a->tmp = NULL;
}
int qfull(queue *a) {
a->tmp = (node *)malloc(sizeof(node)); /* Reserving memory using tmp */
if(a->tmp == NULL)
return 1;
else
return 0;
}
int qempty(queue *a) {
return a->head == NULL;
}
@DevashishX
Copy link

DevashishX commented Oct 6, 2017

```
 int qfull(queue *a) {
 if(a->head!=NULL) free(a->temp); /*but what if you are calling full for the first time? */
a->tmp = (node *)malloc(sizeof(node)); /* Reserving memory using tmp */
if(a->tmp == NULL)
	return 1;
else
	return 0;
}
int qempty(queue *a) {
    return a->head == NULL;
}

@DevashishX
Copy link

sorry for bad indentation 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment