Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node{
- int data;
- struct node *next, *prev;
- } *head = NULL, *tail = NULL;
- void display(struct node *help){
- if(!help) printf("\nEmpty Queue.");
- else while(help){
- printf(" %d ", help->data);
- help = help->next;
- }
- }
- void frontInsert(struct node *newNode){
- newNode->prev = NULL;
- newNode->next = head;
- if(!head) tail = newNode;
- else head->prev = newNode;
- head = newNode;
- }
- void backInsert(struct node *newNode){
- newNode->next = NULL;
- newNode->prev = tail;
- if(!tail) head = newNode;
- else tail->next = newNode;
- tail = newNode;
- }
- void frontDelete(){
- if(!head->next) head = tail = NULL;
- else{
- struct node *target = head;
- head = head->next;
- head->prev = NULL;
- free(target);
- }
- }
- void backDelete(){
- if(!tail->prev) tail = head = NULL;
- else{
- struct node *target = tail;
- tail = tail->prev;
- tail->next = NULL;
- free(target);
- }
- }
- int main(){
- int choice, value;
- while(1){
- display(head);
- printf("\n1. Insert 2. Delete 3. Exit: ");
- scanf("%d", &choice);
- switch(choice){
- case 1:{
- printf("\nEnter the value to be inserted: ");
- scanf("%d", &value);
- printf("\nInsert at 1. Front 2. Back: ");
- scanf("%d",&choice);
- struct node *newNode = (struct node*) malloc(sizeof(struct node));
- newNode->data = value;
- if(choice == 1) frontInsert(newNode);
- else if(choice == 2) backInsert(newNode);
- else goto down;
- break;
- }
- case 2: if(!head) printf("\nCan't Delete.");
- else{
- printf("\nDelete from 1. Front 2. Back: ");
- scanf("%d", &choice);
- if(choice == 1) frontDelete();
- else if(choice == 2) backDelete();
- else goto down;
- } break;
- case 3: return 0;
- default: down: printf("\nInvalid input.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement