c code to implement Deques using linked list

  • clinked list Deques
  • implement Deques using linked list

    Write a menu-driven program to implement Deques (both Input-restricted and Output-restricted) operations such as Enqueue, Dequeue, Peek, Display of elements, and IsEmpty using a linked list.

    Code

    //restricted_input_DEqueue #include <stdio.h> #include <stdlib.h> struct Node *f = NULL; struct Node *r = NULL; struct Node { int data; struct Node *next; }; void Display(struct Node *ptr) { printf("Elements of this linked list\n"); while (ptr != NULL) { printf("Element: %d\n", ptr->data); ptr = ptr->next; } } void restricted_input_DEqueue_enqueue_rear(int val) { struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL) { printf("Queue is Full"); } else { n->data = val; n->next = NULL; if (f == NULL) { f = r = n; } else { r->next = n; r = n; } } } int restricted_input_DEqueue_dequeue_rear() { int val = -1; struct Node *ptr = r; struct Node *ptr1 = f; if (r == NULL) { printf("Queue is Empty\n"); } else { val = ptr->data; while (ptr1->next->next != NULL) { ptr1 = ptr1->next; } ptr1->next = NULL; r = ptr1; free(ptr); } return val; } int restricted_input_DEqueue_dequeue_front() { int val = -1; struct Node *ptr = f; if (f == NULL) { printf("Queue is Empty\n"); } else { f = f->next; val = ptr->data; free(ptr); } return val; } int main() { restricted_input_DEqueue_enqueue_rear(34); restricted_input_DEqueue_enqueue_rear(4); restricted_input_DEqueue_enqueue_rear(7); restricted_input_DEqueue_enqueue_rear(17); Display(f); printf("Dequeuing element %d\n", restricted_input_DEqueue_dequeue_rear()); printf("Dequeuing element %d\n", restricted_input_DEqueue_dequeue_front()); // printf("Dequeuing element %d\n", dequeue()); // printf("Dequeuing element %d\n", dequeue()); Display(f); return 0; } //restricted_output_DEqueue #include <stdio.h> #include <stdlib.h> struct Node *f = NULL; struct Node *r = NULL; struct Node { int data; struct Node *next; }; void Display(struct Node *ptr) { printf("Elements of this linked list\n"); while (ptr != NULL) { printf("Element: %d\n", ptr->data); ptr = ptr->next; } } void restricted_output_DEqueue_enqueue_rear(int val) { struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL) { printf("Queue is Full"); } else { n->data = val; n->next = NULL; if (f == NULL) { f = r = n; } else { r->next = n; r = n; } } } void restricted_output_DEqueue_enqueue_front(int val) { struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL) { printf("Queue is Full"); } else { n->data = val; n->next = NULL; if (f == NULL) { f = r = n; } else { struct Node *ptr=f; while(ptr->next!=NULL){ ptr=ptr->next; } ptr->next = n; } } } int restricted_output_DEqueue_dequeue_front() { int val = -1; struct Node *ptr = f; if (f == NULL) { printf("Queue is Empty\n"); } else { f = f->next; val = ptr->data; free(ptr); } return val; } int main() { restricted_output_DEqueue_enqueue_front(34); restricted_output_DEqueue_enqueue_front(4); restricted_output_DEqueue_enqueue_front(7); restricted_output_DEqueue_enqueue_front(17); Display(f); printf("Dequeuing element %d\n", restricted_output_DEqueue_dequeue_front()); printf("Dequeuing element %d\n", restricted_output_DEqueue_dequeue_front()); // printf("Dequeuing element %d\n", dequeue()); // printf("Dequeuing element %d\n", dequeue()); Display(f); return 0; }