C PROGRAM TO IMPLEMENT QUEUE OPERATIONS USING ARRAY
- #include<stdio.h>
- #include<conio.h>
- #define MAX 10
- int
queue[MAX],
front = -1, rear = -1;
- void
Insert_Element();
- void
Delete_Element();
- void
Display_Queue();
- void
Empty_Queue();
-
- int main()
- {
- int
option;
- printf(">>> c program to implement queue operations
<<<");
- do
- {
- printf("\n\n
1.Insert an element");
- printf("\n
2.Delete an element");
- printf("\n
3.Display queue");
- printf("\n
4.Empty queue");
- printf("\n
5.Exit");
- printf("\n Enter
your choice: ");
- scanf("%d", &option);
- switch (option)
- {
- case 1: Insert_Element();
- break;
- case 2: Delete_Element();
- break;
- case 3: Display_Queue();
- break;
- case 4: Empty_Queue();
- break;
- case 5: return 0; /*program ends*/
- }
-
- } while (option
!= 5);
- }
-
- void
Insert_Element()
- {
- int num;
- if (rear < MAX - 1)
- {
- if (front
== -1)
- /*when queue is initially empty */
- front = 0;
- printf("\n Enter
the number to be inserted: ");
- scanf("%d", &num);
- rear = rear + 1;
- queue[rear] = num;
- }
- else
- {
- printf("\n Queue
OverFlow Occured");
- }
- }
-
- void
Delete_Element()
- {
- int
element;
-
- if (front
== -1 || front > rear)
- {
- printf("\n Queue
Underflow occured.\n");
- return;
- }
- else
- {
- element = queue[front];
- printf("\n
Element deleted from queue is : %d", element);
- front = front + 1;
- }
- }
-
- void
Display_Queue()
- {
- int i;
- if (front
== -1 || front > rear)
- printf("\n No
elements to display");
- else
- {
- printf("\n The
queue elements are:\n ");
- for (i =
front; i <= rear; i++)
- {
- printf("\t
%d", queue[i]);
- }
- }
- }
-
- void
Empty_Queue()
- {
- /*Reset queue or Creates Empty queue*/
- front = -1;
- rear = -1;
- printf("\n New
Queue created successfully.");
- }
Sample output :
>>> c program to implement queue operations <<<
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 1
Enter the number to be inserted: 10
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 1
Enter the number to be inserted: 20
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 1
Enter the number to be inserted: 30
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 2
Element deleted from queue is : 10
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 3
The queue elements are:
20 30
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice:
Download Source Code
Good job! I followed this link searching for the ways to highlight code on blogger. I'm going to try ideone.com
ReplyDeleteThanks for sharing
@horizon : keep visiting my blogs
ReplyDeleteI need the author of this blog, kindly respond to me on whatsapp @ 7042028396 or email @ abhinav.kakku@gmail.com
ReplyDeletefor http://www.ethicalhackx.com/
Ethical Hacking Tutorials Coling AUthor
ReplyDeleteThe author of this blog please contact me @ abhinav.kakku@gmail.com or whatsapp 7042028396
Can you explain the display_queue() module if front > rear.
ReplyDeleteThank You
front>rear is not possible in queue. we are printing all elements from front to rear.
DeleteThis comment has been removed by the author.
ReplyDeleteFor usful codes of C , Batch Script etc visit
ReplyDeletehttp://codeitoff.blogspot.in/
thanks it will be useful to every person
ReplyDeleteits good
ReplyDeleteAnother easy program to implementing a queue using arrays
ReplyDelete#include
#include
void main()
{
int queue[8];
int rear=0;
while(1)
{
printf("enter elements");
scanf("%d",&queue[rear];
rear++;
if(rear==7)
{
printf(queue is full");
break;
}
}
prinf("elements of queue are");
rear=0;
while(1)
{
printf("%d",queue[rear]);
rear++;
if(rear==7);
break;
}
}
This comment has been removed by the author.
DeleteThis comment has been removed by the author.
DeleteThis comment has been removed by the author.
Deletedear srinivasulu srinu,
Deleteyour code have some syntax errors,but the concept was good.
i has modify this as better.
Thank you.
code:
#include <stdio.h>
#define MAXQUEUE 5
int main() {
int queue[MAXQUEUE]={},rear=0;
printf("Enter %d Integer:\n",MAXQUEUE);
while(rear<MAXQUEUE) //enqueue
scanf("%d",&queue[rear++]);
printf("Queue full,contents are\n");
printf("\nRear\t\tfront\n");
while(rear--) //dequeue all
printf(" %d ",queue[rear]);
return 0;
}
This program is ... wrong ...
DeleteUpto inserting 10 element in the Queue programm is write ..
but if we delete front element & try to insert new element in the queue ,, then program is wrong
Great idea for an article! Looking forward to the next part.
ReplyDeleteTally Training
This comment has been removed by the author.
ReplyDeleteIn delete()
ReplyDeleteUnder front=max-1
Rear also needs to be on 0
So, front=rear=0
In delete()
ReplyDeleteUnder front=max-1
Rear also needs to be on 0
So, front=rear=0
ReplyDeleteVery informative article.Thank you author for posting this kind of article .
http://www.wikitechy.com/view-article/explain-in-details-about-array-implementation-of-linked-list-in-c
Both are really good,.
Cheers,
Venkat
Very informative article.Thank you author for posting this kind of article .
ReplyDeletehttp://www.wikitechy.com/view-article/static-block-in-java-with-example-and-explanation
Both are really good,
Cheers,
Venkat
Thanks, This C Programming Examples will be helpful.
ReplyDeleteWow, I was writing a program to represent a queue using R- programming software and this blog has helped me to write the program correctly and I am very grateful to the author for sharing this information with us. Find time and read my article by clicking on Useful Tips for Writing a Literature Review.
ReplyDeletesuperb easy way of understanding
ReplyDeleteI write in tears and yet joy in my heart ‘cos I brought that scumbag of a BF down after hiring WISETECHHACKER@GMAIL.COM He was my savior, he is so good at what he does, though quite expensive but IT WAS WORTH IT AT THE END. Hey ladies! Let’s say NO to cheats, expose your partner today by hiring wisetechhacker@gmail.com or Whatsapp:+19175085328. He is very reliable as I choose…
ReplyDelete