Last active
December 18, 2024 13:53
-
-
Save mycodeschool/7510222 to your computer and use it in GitHub Desktop.
Linked List implementation of Queue.
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
/*Queue - Linked List implementation*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
struct Node { | |
int data; | |
struct Node* next; | |
}; | |
// Two glboal variables to store address of front and rear nodes. | |
struct Node* front = NULL; | |
struct Node* rear = NULL; | |
// To Enqueue an integer | |
void Enqueue(int x) { | |
struct Node* temp = | |
(struct Node*)malloc(sizeof(struct Node)); | |
temp->data =x; | |
temp->next = NULL; | |
if(front == NULL && rear == NULL){ | |
front = rear = temp; | |
return; | |
} | |
rear->next = temp; | |
rear = temp; | |
} | |
// To Dequeue an integer. | |
void Dequeue() { | |
struct Node* temp = front; | |
if(front == NULL) { | |
printf("Queue is Empty\n"); | |
return; | |
} | |
if(front == rear) { | |
front = rear = NULL; | |
} | |
else { | |
front = front->next; | |
} | |
free(temp); | |
} | |
int Front() { | |
if(front == NULL) { | |
printf("Queue is empty\n"); | |
return; | |
} | |
return front->data; | |
} | |
void Print() { | |
struct Node* temp = front; | |
while(temp != NULL) { | |
printf("%d ",temp->data); | |
temp = temp->next; | |
} | |
printf("\n"); | |
} | |
int main(){ | |
/* Drive code to test the implementation. */ | |
// Printing elements in Queue after each Enqueue or Dequeue | |
Enqueue(2); Print(); | |
Enqueue(4); Print(); | |
Enqueue(6); Print(); | |
Dequeue(); Print(); | |
Enqueue(8); Print(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
same code in java pls provid it