C++ Program To Implement Queue Using Array
C++ Program To Implement Queue Using Array
#include <iostream>
int ar[10];
int n = 10;
int front = - 1;
int rear = - 1;
if (rear == n - 1){
cout<<"Overflow!"<<endl;
return;
else {
front = 0;
rear=0;
else{
rear++;
ar[rear] = item;
cout<<"Element inserted"<<endl;
}
//function to implement dequeue operation
void dequeue() {
cout<<"Underflow!";
return ;
else {
int item=ar[front];
if(front == rear) {
front = -1;
rear = -1;
else{
front++;
void display() {
if (front == - 1){
cout<<"Queue is empty"<<endl;
return;
else {
// if queue is not empty print all the elements from rear to front
cout<<ar[i]<<" ";
cout<<endl;
void fronte() {
if (front == - 1){
cout<<"Queue is empty"<<endl;
return;
else {
//driver code
int main() {
int ch;
cout<<"5: Exit"<<endl;
do {
cin>>ch;
switch (ch) {
case 1: {
int item;
cin>>item;
enqueue(item);
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: fronte();
break;
case 5: cout<<"Exit"<<endl;
break;
} while(ch!=5);
return 0;
}
Stack Implementation using Array
#include <stdio.h>
// Function declarations
void push();
void pop();
void peek();
int stack[50],i,j,option=0,n,top=-1;
void main ()
// Size of stack
scanf("%d",&n);
while(option != 4)
printf("\n1.Push\n2.Pop\n3.Peek\n4.Exit");
scanf("%d",&option);
switch(option)
case 1:
{
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
printf("Exiting");
break;
default:
};
void push ()
int value;
if (top == n )
printf("\n Overflow");
else
scanf("%d",&val);
stack[top] = value;
void pop ()
if(top == -1)
printf("Underflow");
else
void peek()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
}
Stack Using Linked List
include <stdio.h>
#include <stdlib.h>
struct node {
int info;
}*top,*top1,*temp;
int count = 0;
if (top == NULL)
top->ptr = NULL;
top->info = data;
else
temp->ptr = top;
temp->info = data;
top = temp;
count++;
printf("Node is Inserted\n\n");
int pop() {
top1 = top;
if (top1 == NULL)
printf("\nStack Underflow\n");
return -1;
else
top1 = top1->ptr;
free(top);
top = top1;
count--;
return popped;
void display() {
top1 = top;
if (top1 == NULL)
printf("\nStack Underflow\n");
return;
printf("%d--->", top1->info);
top1 = top1->ptr;
printf("NULL\n\n");
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(value);
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
struct node {
int data;
};
} else {
printf("Node is Inserted\n\n");
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
free(temp);
return temp_data;
void display() {
printf("\nQueue is Empty\n");
} else {
temp = front;
while (temp) {
}
printf("NULL\n\n");
int main() {
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
switch (choice) {
case 1:
enqueue(value);
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
return 0;
}