Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created November 15, 2019 07:45
Show Gist options
  • Save surinoel/47c687639a685cea463db99483b2ad4e to your computer and use it in GitHub Desktop.
Save surinoel/47c687639a685cea463db99483b2ad4e to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Queue {
struct Node *front;
struct Node *rear;
};
struct Node *InitNode(int data) {
struct Node *tmp = (struct Node *)malloc(sizeof(struct Node));
tmp->data = data;
tmp->next = NULL;
return tmp;
}
struct Queue *createQueue(void) {
struct Queue *tmp = (struct Queue *)malloc(sizeof(struct Queue));
tmp->front = tmp->rear = NULL;
return tmp;
}
void enQueue(struct Queue *q, int data) {
struct Node *tmp = InitNode(data);
if (q->front == NULL) {
q->front = q->rear = tmp;
return;
}
q->rear->next = tmp;
q->rear = tmp;
}
struct Node *deQueue(struct Queue *q) {
struct Node *tmp = q->front;
if (tmp != NULL) {
q->front = tmp->next;
return tmp;
}
return NULL;
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
struct Node* n = deQueue(q);
if (n != NULL)
printf("Dequeued item is %d", n->data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment