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)
Derivatives stock list at NSE
Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...
-
Acronym Full Form AJAX Asynchronous JavaScript and XML API Application Programming Interface APK Android Application Package ASP Activ...
-
//The HTML index page <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change ...
-
The sequence of topics to be learnt to become a full stack developer is a minimum of : I. Front-end Developer/ Web Designer: -----------...