Advertisement
kobikirmayer

Untitled

Mar 27th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. /*Queue - Linked List implementation*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. struct Node {
  5.     int data;
  6.     struct Node* next;
  7. };
  8. // Two glboal variables to store address of front and rear nodes.
  9. struct Node* front = NULL;
  10. struct Node* rear = NULL;
  11.  
  12. // To Enqueue an integer
  13. void Enqueue(int x) {
  14.     struct Node* temp =
  15.         (struct Node*)malloc(sizeof(struct Node));
  16.     temp->data =x;
  17.     temp->next = NULL;
  18.     if(front == NULL && rear == NULL){
  19.         front = rear = temp;
  20.         return;
  21.     }
  22.     rear->next = temp;
  23.     rear = temp;
  24. }
  25.  
  26. // To Dequeue an integer.
  27. void Dequeue() {
  28.     struct Node* temp = front;
  29.     if(front == NULL) {
  30.         printf("Queue is Empty\n");
  31.         return;
  32.     }
  33.     if(front == rear) {
  34.         front = rear = NULL;
  35.     }
  36.     else {
  37.         front = front->next;
  38.     }
  39.     free(temp);
  40. }
  41.  
  42. int Front() {
  43.     if(front == NULL) {
  44.         printf("Queue is empty\n");
  45.         return;
  46.     }
  47.     return front->data;
  48. }
  49.  
  50. void Print() {
  51.     struct Node* temp = front;
  52.     while(temp != NULL) {
  53.         printf("%d ",temp->data);
  54.         temp = temp->next;
  55.     }
  56.     printf("\n");
  57. }
  58.  
  59. int main(){
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement