Created
November 28, 2025 11:54
-
-
Save mgabrielmarin/ef94900edb7df6e044643e4461d904fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Que implementation using linked list */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| struct node { | |
| int data; | |
| struct node *next; | |
| }; | |
| struct node *createNode(int data) { | |
| struct node *n = malloc(sizeof(struct node)); | |
| n->data = data; | |
| n->next = NULL; | |
| return n; | |
| } | |
| struct que { | |
| struct node *rear; | |
| struct node *front; | |
| }; | |
| struct que *createQue() { | |
| struct que *q = malloc(sizeof(struct que)); | |
| q->rear = q->front = NULL; | |
| return q; | |
| } | |
| void enQue(struct que *q, int data) { | |
| struct node *n = createNode(data); | |
| if (q->front == NULL) { | |
| q->rear = q->front = n; | |
| } else { | |
| q->rear->next = n; | |
| q->rear = n; | |
| } | |
| } | |
| int deQue(struct que *q) { | |
| if (q->front == NULL) { | |
| printf("Error: que empty cannot deQue()\n"); | |
| return 1; | |
| } | |
| struct node *n = q->front; | |
| int data = n->data; | |
| q->front = q->front->next; | |
| free(n); | |
| return data; | |
| } | |
| void printQue(struct que *q) { | |
| if (q->front == NULL) { | |
| printf("Error: que empty cannot printQue()\n"); | |
| return; | |
| } | |
| struct node *n = q->front; | |
| while (n) { | |
| printf("%d ", n->data); | |
| n = n->next; | |
| } | |
| printf("\n"); | |
| } | |
| int main(void) { | |
| struct que *q = createQue(); | |
| enQue(q, 3); | |
| enQue(q, 30); | |
| enQue(q, 13); | |
| printQue(q); | |
| deQue(q); | |
| printQue(q); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment