This tutorial demonstrate how to insert and delete element in the queue using array.
=====================================================================
========================================================
Description:
In this tutorial you will see how to implement queue using array and queue insert & delete operations.
Code:
#include <stdio.h> #define MAX 5 #include <stdlib.h> void insert(int queue[], int *rear, int value) { if(*rear < MAX-1) { *rear= *rear +1; queue[*rear] = value; } else { printf("The queue is full \n"); exit(1); } } void deleteQ(int queue[], int *front, int rear, int *value) { if(*front == rear) { printf("The queue is empty \n"); exit(1); } *front = *front + 1; *value = queue[*front]; } void main() { int queue[MAX]; int front,rear; int n,value; front=rear=(-1); do { do { printf("Enter the element to be inserted in queue\n"); scanf("%d",&value); insert(queue,&rear,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element from queue\n"); scanf("%d",&n); while( n == 1) { deleteQ(queue,&front,rear,&value); printf("The value deleted is %d\n",value); printf("Enter 1 to delete an element from queue\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
Output:
=====================================================================
Queue implementation using linked list.
Description:
The advantage of using linked list is that there is no size limit. The size of queue grow and shrink as per insertion and deletion takes place.
Code:
# include <stdio.h> # include <stdlib.h> struct node { int data; struct node *link; }; void insert(struct node **front, struct node **rear , int value) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) { printf("No Memory available\n"); exit(0); } temp->data = value; temp->link=NULL; if(*rear == NULL) { *rear = temp; *front = *rear; } else { (*rear)->link = temp; *rear = temp; } } void deleteQ(struct node **front, struct node **rear , int *value) { struct node *temp; if((*front == *rear) && (*rear == NULL)) { printf(" The queue is empty can not delete Error\n"); exit(0); } *value = (*front)->data; temp = *front; *front = (*front)->link; if(*rear == temp) *rear = (*rear)->link; free(temp); } void main() { struct node *front=NULL,*rear = NULL; int n,value; do { do { printf("Enter the element to be inserted\n"); scanf("%d",&value); insert(&front,&rear,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { deleteQ(&front,&rear,&value); printf("The value deleted is %d\n",value); printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
Output
Circular queue implementation using array.
#include <stdio.h> #define MAX 5 #include <stdlib.h> void insert(int queue[], int *rear, int front, int value) { *rear= (*rear +1) % MAX; if(*rear == front) { printf("The queue is full\n"); exit(0); } queue[*rear] = value; } void deleteQ(int queue[], int *front, int rear, int * value) { if(*front == rear) { printf("The queue is empty\n"); exit(0); } *front = (*front + 1) % MAX; *value = queue[*front]; } void main() { int queue[MAX]; int front,rear; int n,value; front=0; rear=0; do { do { printf("Enter the element to be inserted\n"); scanf("%d",&value); insert(queue,&rear,front,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { deleteQ(queue,&front,rear,&value); printf("The value deleted is %d\n",value); printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
No comments:
Post a Comment