/*==========================================================================================
**
** File Name : queue_array.c
** Creation Date : Fri 13 Oct 2017 11:46:43 PM IST
** Last Modified : Thu 19 Oct 2017 07:59:05 PM IST
** Compiler : gcc
** Author : Manoj Kumar Patra, Asst. Professor
** Organization : Vignan University, Guntur, India.
**
**=========================================================================================*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int queue_array[MAX];
int front=-1;
int rear=-1;
void enqueue(int item);
int dequeue();
int peek();
void display();
int isFull();
int isEmpty();
main()
{
int choice,item;
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("= = = = = = = = = = Queue Using Array in C = = = = = = = = = =\n");
while(1)
{
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Peek\n");
printf("5. Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number to be added in queue : ");
scanf("%d",&item);
enqueue(item);
printf("Number %d das been added to the queue\n",item);
break;
case 2:
item=dequeue();
printf("Front element %d has been deleted\n",item);
break;
case 3:
display();
break;
case 4:
printf("Front element in the Queue is: %d\n",peek());
break;
case 5:
printf("Program Terminated..!!\n");
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
exit(1);
default:
printf("Oops... Wrong choice..!!\n");
}
}
}
// 1 ok
void enqueue(int item)
{
if(isFull())
{
printf("Queue Overflow\n");
return;
}
if(front == -1)
front=0;
rear=rear+1;
queue_array[rear]=item ;
}
// 2 ok
int dequeue()
{
int item;
if(isEmpty())
{
printf("Queue Underflow\n");
exit(1);
}
item=queue_array[front];
front=front+1;
return item;
}
// 3 ok
void display()
{
int i;
if(isEmpty())
{
printf("Queue is empty\n");
return;
}
printf("Queue elements are :\n\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue_array[i]);
printf("\n\n");
}
// 4 ok
int peek()
{
if(isEmpty())
{
printf("Queue Underflow\n");
exit(1);
}
return queue_array[front];
}
// 5 ok
int isEmpty()
{
if(front==-1 || front==rear+1)
return 1;
else
return 0;
}
// 6 ok
int isFull()
{
if(rear == MAX-1)
return 1;
else
return 0;
}
OutPut:
manoj@ubuntu:~/ds/queue$ gcc queue_array.c -o queue_array
manoj@ubuntu:~/ds/queue$ ./queue_array
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Queue Using Array in C = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 10
Number 10 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 20
Number 20 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 30
Number 30 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 40
Number 40 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 50
Number 50 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 3
Queue elements are :
10 20 30 40 50
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 2
Front element 10 has been deleted
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 3
Queue elements are :
20 30 40 50
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 4
Front element in the Queue is: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 5
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/queue$
**
** File Name : queue_array.c
** Creation Date : Fri 13 Oct 2017 11:46:43 PM IST
** Last Modified : Thu 19 Oct 2017 07:59:05 PM IST
** Compiler : gcc
** Author : Manoj Kumar Patra, Asst. Professor
** Organization : Vignan University, Guntur, India.
**
**=========================================================================================*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int queue_array[MAX];
int front=-1;
int rear=-1;
void enqueue(int item);
int dequeue();
int peek();
void display();
int isFull();
int isEmpty();
main()
{
int choice,item;
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("= = = = = = = = = = Queue Using Array in C = = = = = = = = = =\n");
while(1)
{
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Peek\n");
printf("5. Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number to be added in queue : ");
scanf("%d",&item);
enqueue(item);
printf("Number %d das been added to the queue\n",item);
break;
case 2:
item=dequeue();
printf("Front element %d has been deleted\n",item);
break;
case 3:
display();
break;
case 4:
printf("Front element in the Queue is: %d\n",peek());
break;
case 5:
printf("Program Terminated..!!\n");
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
exit(1);
default:
printf("Oops... Wrong choice..!!\n");
}
}
}
// 1 ok
void enqueue(int item)
{
if(isFull())
{
printf("Queue Overflow\n");
return;
}
if(front == -1)
front=0;
rear=rear+1;
queue_array[rear]=item ;
}
// 2 ok
int dequeue()
{
int item;
if(isEmpty())
{
printf("Queue Underflow\n");
exit(1);
}
item=queue_array[front];
front=front+1;
return item;
}
// 3 ok
void display()
{
int i;
if(isEmpty())
{
printf("Queue is empty\n");
return;
}
printf("Queue elements are :\n\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue_array[i]);
printf("\n\n");
}
// 4 ok
int peek()
{
if(isEmpty())
{
printf("Queue Underflow\n");
exit(1);
}
return queue_array[front];
}
// 5 ok
int isEmpty()
{
if(front==-1 || front==rear+1)
return 1;
else
return 0;
}
// 6 ok
int isFull()
{
if(rear == MAX-1)
return 1;
else
return 0;
}
OutPut:
manoj@ubuntu:~/ds/queue$ gcc queue_array.c -o queue_array
manoj@ubuntu:~/ds/queue$ ./queue_array
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Queue Using Array in C = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 10
Number 10 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 20
Number 20 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 30
Number 30 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 40
Number 40 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 1
Enter the number to be added in queue : 50
Number 50 das been added to the queue
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 3
Queue elements are :
10 20 30 40 50
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 2
Front element 10 has been deleted
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 3
Queue elements are :
20 30 40 50
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 4
Front element in the Queue is: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Enqueue
2. Dequeue
3. Display
4. Peek
5. Quit
Enter your choice : 5
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/queue$
No comments:
Post a Comment