1. What is a circular queue? Write a C program for static implementation of Circular Queue.
#include<stdio.h>
#define MAX 5
struct queue
{
int arr[MAX];
int front,rear;
int count;
};
void init(struct queue *qp)
{
qp->front = qp->rear = -1;
qp->count = 0;
}
int is_empty(struct queue *qp)
{
return qp->count==0;
}
int is_full(struct queue *qp)
{
return qp->count==MAX;
}
void enqueue(struct queue *qp,int num)
{
if(is_full(qp))
{
printf("The queue is full. Can not add elements...");
}
else
{
(qp->rear)++;
int index = qp->rear % MAX;
qp->arr[index] = num;
(qp->count)++;
if(qp->front==-1)
qp->front = 0;
}
}
int dequeue(struct queue *qp)
{
int temp = -1;
if(is_empty(qp))
{
printf("Queue is empty");
}
else
{
int index = qp->front %MAX;
temp = qp->arr[index];
(qp->front)++;
(qp->count)--;
}
return temp;
}
void print_queue(struct queue *qp)
{
int i;
printf("Queue is ");
for(i = qp->front;i<=qp->rear;i++)
printf("%d---",qp->arr[i%MAX]);
printf("\n");
}
int main()
{
struct queue q1;
init(&q1);
while(1)
{
printf("Enter 1 - enqueue 2 - dequeue 3 - exit");
int opt;
scanf("%d",&opt);
if(opt==1)
{
int n;
printf("Enter a number:");
scanf("%d",&n);
enqueue(&q1,n);
print_queue(&q1);
}
else if(opt==2)
{
int n;
n = dequeue(&q1);
if (n!=-1){
printf("Value dequed is %d\n",n);
print_queue(&q1);
}
}
else
break;
}
return 0;
}
2. Explain how queue is implemented using a linked list. Write functions to insert and delete elements to such a queue.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int n;
struct node *next;
struct node *prev;
};
typedef struct node * NODEPTR;
struct queue
{
NODEPTR front;
NODEPTR rear;
};
NODEPTR create_node(int value)
{
NODEPTR temp = (NODEPTR) malloc(sizeof(struct node));
temp->prev = temp->next = NULL;
temp->n = value;
return temp;
}
int dequeue(struct queue *qp)
{
if(qp->front==NULL)
{
printf("Queue empty...\n");
return -1;
}
NODEPTR nd = qp->front;
if(qp->rear==qp->front)//deleting the only node
qp->rear = NULL;
qp->front = qp->front->next;
if(qp->front!=NULL)
qp->front->prev= NULL;
int num = nd->n;
free(nd);
return num;
}
void enqueue(struct queue *qp, int num)
{
NODEPTR newnode = create_node(num);
if(qp->rear!=NULL)
qp->rear->next = newnode;
newnode->prev = qp->rear;
qp->rear = newnode;
if(qp->front==NULL)
qp->front = qp->rear;
}
void display_queue(struct queue *qp)
{
NODEPTR temp = qp->front;
while(temp!=NULL)
{
printf("%d====>",temp->n);
temp = temp->next;
}
}
int main()
{
struct queue q1;
q1.front = q1.rear = NULL;
while(1)
{
int option, number;
printf("Enter 1-enqueue 2 - dequeue 3- display 4 - exit:");
scanf("%d",&option);
if(option==1)
{
printf("Enter number to enqueue:");
scanf("%d",&number);
enqueue(&q1,number);
}
else if(option==2)
{
number = dequeue(&q1);
if(number !=-1)
printf("Number dequeued is %d\n",number);
}
else if(option==3)
display_queue(&q1);
else if(option==4)
break;
}
}
3. Implement a queue with two stacks. Please implement two functions: appendTail to append an element into tail of a queue, and deleteHead to delete an element from head of a queue.
#include<stdio.h>
#include<stdlib.h>
#define ERROR -1000
struct node
{
int n;
struct node *next;
};
typedef struct node * NODEPTR;
NODEPTR create_node(int value)
{
NODEPTR temp = (NODEPTR) malloc(sizeof(struct node));
temp->next = NULL;
temp->n = value;
return temp;
}
NODEPTR push(NODEPTR top,int val)
{
NODEPTR newnode = create_node(val);
newnode->next = top;
top = newnode;
return top;
}
int isempty(NODEPTR top)
{
return top==NULL;
}
int pop(NODEPTR *topPtr)
{
if(isempty(*topPtr))
return ERROR;
int num = (*topPtr)->n;
NODEPTR temp = *topPtr;
*topPtr = (*topPtr)->next;
free(temp);
return num;
}
/*enqueue using push*/
void enqueue(NODEPTR *top1,int val)
{
*top1 = push(*top1,val);
}
/*dequeue is expensive*/
/*pop all to second stack from first
now return top value from second*/
int dequeue(NODEPTR *topptr1,NODEPTR *topptr2)
{
if(isempty(*topptr2))
while(!isempty(*topptr1))
{
int a = pop(topptr1);
*topptr2 = push(*topptr2,a);
}
return pop(topptr2);
}
int main()
{
NODEPTR top1 = NULL,top2=NULL;
NODEPTR nd;
while(1)
{
int option,value;
printf("Enter 1- nq 2- dq 3 - exit");
scanf("%d",&option);
if(option==3)
break;
switch(option)
{
case 1: printf("new value to enqueue=");
scanf("%d",&value);
enqueue(&top1,value);
break;
case 2: value = dequeue(&top1,&top2);
if(value==ERROR)
printf("Queue empty\n");
else
printf("Value removed is %d\n",value);
break;
}
}
}
4. What is a deque?
5. Write a function to count the number of nodes in a circular queue
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
typedef struct node *NODEPTR;
struct queue
{
NODEPTR front,rear;
};
typedef struct queue QUEUE;
NODEPTR create_node(int n)
{
NODEPTR newnode = malloc(sizeof(struct node));
newnode->num = n;
newnode->next = NULL;
return newnode;
}
int is_empty(QUEUE *q1)
{
return q1->front ==NULL;
}
void enqueue(QUEUE *q1,int n)
{
NODEPTR temp = create_node(n);
if(is_empty(q1))
{
q1->rear = q1->front = temp;
temp->next = temp;
}
else{
q1->rear->next = temp;
q1->rear = temp;
temp->next = q1->front;
}
}
int dequeue(QUEUE *q1)
{
if(is_empty(q1))
return -1;
NODEPTR temp = q1->front;
int m = temp->num;
q1->front = q1->front->next;
free(temp);
if(q1->front==NULL)
q1->rear = NULL;
else
q1->rear->next = q1->front;
return m;
}
void display(QUEUE q1)
{
NODEPTR node=q1.front;
printf("\nThe list is \n");
do
{
printf("%d----",node->num);
node = node->next;
}while(node!=q1.front);
}
int count(QUEUE q1)
{
NODEPTR temp = q1.front;
int c=0;
do
{
c++;
temp = temp->next;
}while(temp!=q1.front);
return c;
}
int main()
{
QUEUE q1;
/*initialize queue*/
q1.front = q1.rear = NULL;
while(1)
{
int ans;
printf("Enter 1 - Add a node 2 - remove a node 3 - quit\n");
scanf("%d",&ans);
if (ans==1)
{
int n;
printf("Node to enqueued:");
scanf("%d",&n);
enqueue(&q1,n);
display(q1);
}else if (ans==2)
{
int n;
n = dequeue(&q1);
if(n==-1)
printf("THe queue is empty");
else
printf("The value dequed is %d\n",n);
display(q1);
}
else
break;
}
printf("Number of nodes in the queue is %d\n",count(q1));
return 0;
}
6. Write a program to implement a deque using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
typedef struct node *NODEPTR;
struct queue
{
NODEPTR front,rear;
};
typedef struct queue QUEUE;
NODEPTR create_node(int n)
{
NODEPTR newnode = malloc(sizeof(struct node));
newnode->num = n;
newnode->next = NULL;
return newnode;
}
int is_empty(QUEUE *q1)
{
return (q1->front==NULL && q1->rear==NULL);
}
void enqueue(QUEUE *q1,int n)
{
NODEPTR temp = create_node(n);
if(is_empty(q1))
{
q1->rear = q1->front = temp;
}
else{
q1->rear->next = temp;
q1->rear = temp;
}
}
void enqueue_front(QUEUE *q1,int n)
{
NODEPTR temp = create_node(n);
if(is_empty(q1))
q1->rear = q1->front=temp;
else
{
temp->next = q1->front;
q1->front = temp;
}
}
NODEPTR find_previous(QUEUE q1,NODEPTR nd)
{
NODEPTR temp=q1.front;
while(temp!=NULL)
{
if(temp->next ==nd)
return temp;
temp = temp->next;
}
return NULL;
}
int dequeue(QUEUE *q1)
{
if(is_empty(q1))
return -1;
NODEPTR temp = q1->front;
int m = temp->num;
q1->front = q1->front->next;
free(temp);
if(q1->front==NULL)
q1->rear = NULL;
return m;
}
int dequeue_rear(QUEUE *q1)
{
if(is_empty(q1))
return -1;
NODEPTR temp = q1->rear;
int val = temp->num;
NODEPTR prevnode = find_previous(*q1,temp);
if(prevnode==NULL)
{
q1->rear = q1->front = NULL;
}
else
{
prevnode->next = NULL;
q1->rear = prevnode;
}
free(temp);
return val;
}
void display(QUEUE q1)
{
NODEPTR node=q1.front;
printf("\nThe list is \n");
while(node !=NULL)
{
printf("%d----",node->num);
node = node->next;
}
}
int main()
{
QUEUE q1;
/*initialize queue*/
q1.front = q1.rear = NULL;
while(1)
{
int ans;
printf("Enter 1 - Add a node at rear 2 - remove a node from front 3 - Add a node at front 4 - Remove a node from rear 5 - quit\n");
scanf("%d",&ans);
if (ans==1 || ans==3)
{
int n;
printf("Node to enqueued:");
scanf("%d",&n);
if(ans==1)
enqueue(&q1,n);
else
enqueue_front(&q1,n);
display(q1);
}else if (ans==2 || ans==4)
{
int n;
if(ans==2)
n = dequeue(&q1);
else if(ans==4)
n = dequeue_rear(&q1);
if(n==-1)
printf("The queue is empty");
else
{ printf("The value dequed is %d\n",n);
display(q1);}
}
else if(ans==5)
break;
}
return 0;
}
7. What is queue data structure? Write a program to implement a queue using an array
#include<stdio.h>
#define MAX 5
void enqueue(int arr[], int *rearptr,int num)
{
if(*rearptr == MAX)
{
printf("Queue is full");
}
else
{
arr[*rearptr]=num;
(*rearptr)++;
}
}
int is_empty(int front,int rear)
{
return front==rear;
}
int dequeue(int arr[],int *frontptr,int *rearptr)
{
int temp = -1;
if(*frontptr==*rearptr)
{
printf("Queue is empty");
}
else
{
temp = arr[*frontptr];
(*frontptr)++;
}
return temp;
}
void print_queue(int *arr,int front, int rear)
{
int i;
for(i = front;i<rear;i++)
printf("%d---",arr[i]);
printf("\n");
}
int main()
{
int arr[MAX];
int i ;
int front =0,rear = 0;
while(1)
{
printf("Enter 1 - enqueue 2 - dequeue 3 - exit");
int opt;
scanf("%d",&opt);
if(opt==1)
{
int n;
printf("Enter a number:");
scanf("%d",&n);
enqueue(arr,&rear,n);
print_queue(arr,front,rear);
}
else if(opt==2)
{
int n;
n = dequeue(arr,&front,&rear);
if (n!=-1)
printf("Value dequed is %d\n",n);
print_queue(arr,front,rear);
}
else
break;
}
return 0;
}