I am also active at:
Sunday, September 9, 2018
Saturday, September 1, 2018
Queue : Array Implementation in C
#include <stdio.h>
#define QUEUE_LENGTH 50
struct queue
{
int element[QUEUE_LENGTH];
int front,rear,choice,x,y;
};
void traverse(struct queue q)
{ printf("The elements of the queue are\n");
for(int i=0;i<=q.rear;i++)
{
printf("%d\n",q.element[i]);
}
}
void add(struct queue *p,int y)
{
if((*p).rear<QUEUE_LENGTH)
{ (*p).rear +=1;
(*p).element[(*p).rear]=y;
printf("rear = %d\n",(*p).rear);
printf("type -1 to exit\n");
scanf("%d",&y);
if(y==(-1))
{
return;
}
add(p,y);
}
else
{
printf("Queue overflow\n");
}
}
void delete(struct queue *p)
{
int x=0,y=0;
if((*p).front>(*p).rear)
{
printf("Queue empty\n");
}
else
{
x=(*p).element[(*p).front];
(*p).element[(*p).front]=0;
(*p).front=(*p).front+ 1;
}
printf("type 1 to delete or 2 to exit\n");
scanf("%d",&y);
if(y==(2))
{
return ;
}
if(y==(1))
{
traverse(*p);
printf("%d is deleted\n",(*p).element[(*p).front]-1);
delete(p);
}
}
int main()
{ int choice=0,x=0;
struct queue q;
q.rear= -1,q.front=0;
printf("\nEnter element to be added:\n");
scanf("%d",&x);
add(&q,x);
traverse(q);
delete(&q);
printf("Deletion completed \n");
return 0;
}
Output:
#define QUEUE_LENGTH 50
struct queue
{
int element[QUEUE_LENGTH];
int front,rear,choice,x,y;
};
void traverse(struct queue q)
{ printf("The elements of the queue are\n");
for(int i=0;i<=q.rear;i++)
{
printf("%d\n",q.element[i]);
}
}
void add(struct queue *p,int y)
{
if((*p).rear<QUEUE_LENGTH)
{ (*p).rear +=1;
(*p).element[(*p).rear]=y;
printf("rear = %d\n",(*p).rear);
printf("type -1 to exit\n");
scanf("%d",&y);
if(y==(-1))
{
return;
}
add(p,y);
}
else
{
printf("Queue overflow\n");
}
}
void delete(struct queue *p)
{
int x=0,y=0;
if((*p).front>(*p).rear)
{
printf("Queue empty\n");
}
else
{
x=(*p).element[(*p).front];
(*p).element[(*p).front]=0;
(*p).front=(*p).front+ 1;
}
printf("type 1 to delete or 2 to exit\n");
scanf("%d",&y);
if(y==(2))
{
return ;
}
if(y==(1))
{
traverse(*p);
printf("%d is deleted\n",(*p).element[(*p).front]-1);
delete(p);
}
}
int main()
{ int choice=0,x=0;
struct queue q;
q.rear= -1,q.front=0;
printf("\nEnter element to be added:\n");
scanf("%d",&x);
add(&q,x);
traverse(q);
delete(&q);
printf("Deletion completed \n");
return 0;
}
Output:
- $gcc -o main *.c
- $main
-
- Enter element to be added:
- 1
- rear = 0
- type -1 to exit
- 2
- rear = 1
- type -1 to exit
- 3
- rear = 2
- type -1 to exit
- 4
- rear = 3
- type -1 to exit
- 5
- rear = 4
- type -1 to exit
- 6
- rear = 5
- type -1 to exit
- 7
- rear = 6
- type -1 to exit
- -1
- The elements of the queue are
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- type 1 to delete or 2 to exit
- The elements of the queue are
- 0
- 2
- 3
- 4
- 5
- 6
- 7
- 1 is deleted
- type 1 to delete or 2 to exit
- The elements of the queue are
- 0
- 0
- 3
- 4
- 5
- 6
- 7
- 2 is deleted
- type 1 to delete or 2 to exit
- The elements of the queue are
- 0
- 0
- 0
- 4
- 5
- 6
- 7
- 3 is deleted
- type 1 to delete or 2 to exit
- Deletion completed
1 view · Posted Just now
Subscribe to:
Posts (Atom)
इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया - सुदर्शन फ़ाकिर
इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया वरना क्या बात थी किस बात ने रोने ना दिया आप कहते थे कि रोने से ना बदलेंगे नसीब उमर भर आप की इस बात...
-
Acronym Full Form AJAX Asynchronous JavaScript and XML API Application Programming Interface APK Android Application Package ASP Activ...
-
#include<stdio.h> int main() { int M,N,q; scanf("%i %i",&M, &N); if((M%N)!=0) {printf("0...
-
"A good company understands that problem solving ability and the ability to learn new things are far more important than knowledge o...