0% found this document useful (0 votes)
15 views14 pages

C++ Program To Implement Queue Using Array

Uploaded by

rohithlaveti1822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

C++ Program To Implement Queue Using Array

Uploaded by

rohithlaveti1822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C++ program to implement queue using array

#include <iostream>

using namespace std;

// declaring the array of maximum capacity

int ar[10];

int n = 10;

// declaring front and rear and initializing both with -1

int front = - 1;

int rear = - 1;

//function to perform enqueue operation

void enqueue(int item) {

// checking overflow condition

if (rear == n - 1){

cout<<"Overflow!"<<endl;

return;

else {

// front and rear both are at -1 then

// set front and rear at 0 otherwise increment rear

if (front == - 1 && rear==-1){

front = 0;

rear=0;

else{

rear++;

//inserting element at rear

ar[rear] = item;

cout<<"Element inserted"<<endl;

}
//function to implement dequeue operation

void dequeue() {

//checking underflow condition

if (front == - 1 || front > rear) {

cout<<"Underflow!";

return ;

else {

int item=ar[front];

//displaying deleted element

cout<<"Element deleted from queue is : "<< item <<endl;

// if front and rear reach at end then initialize it at -1

if(front == rear) {

front = -1;

rear = -1;

else{

//incrementing the front

front++;

//function to display all elements of queue

void display() {

//checking whether queue is empty or not

if (front == - 1){

//if queue is empty simply return

cout<<"Queue is empty"<<endl;

return;

else {
// if queue is not empty print all the elements from rear to front

cout<<"Elements are : ";

for (int i = front; i <= rear; i++)

cout<<ar[i]<<" ";

cout<<endl;

//function to display front element of queue

void fronte() {

//checking whether queue is empty or not

if (front == - 1){

//if queue is empty simply return

cout<<"Queue is empty"<<endl;

return;

else {

// if queue is not empty print front element

cout<<"Front Element is :"<<ar[front]<<endl;

//driver code

int main() {

int ch;

//displaying options of enqueue, dequeue, front, display to the user

cout<<"1: Inserting element to queue(enqueue)"<<endl;

cout<<"2: Deleting element from queue(dequeue)"<<endl;

cout<<"3: Display front element of queue"<<endl;

cout<<"4: Display all the elements of queue"<<endl;

cout<<"5: Exit"<<endl;

do {

//taking user choice


cout<<"Enter your choice : "<<endl;

cin>>ch;

switch (ch) {

//calling functions according to the choice of user

case 1: {

cout<<"enter element to be inserted:"<<endl;

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;

default: cout<<"Invalid choice"<<endl;

} while(ch!=5);

return 0;

}
Stack Implementation using Array

#include <stdio.h>

// Function declarations

void push();

void pop();

void peek();

// Taking stack array of size 50

int stack[50],i,j,option=0,n,top=-1;

void main ()

// Size of stack

printf("Enter the number of elements in the stack ");

scanf("%d",&n);

printf("Stack implementation using array\n");

// Taking user input for the operation to be performed

while(option != 4)

printf("Chose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Peek\n4.Exit");

printf("\n Enter your option \n");

scanf("%d",&option);

switch(option)

case 1:

{
push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

printf("Exiting");

break;

default:

printf("Please Enter valid option");

};

// Push operation function

void push ()

int value;

if (top == n )
printf("\n Overflow");

else

printf("Enter the value?");

scanf("%d",&val);

top = top +1;

stack[top] = value;

// Pop operation function

void pop ()

if(top == -1)

printf("Underflow");

else

top = top -1;

// peek operation function

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>

// Structure to create a node with data and the next pointer

struct node {

int info;

struct node *ptr;

}*top,*top1,*temp;

int count = 0;

// Push() operation on a stack

void push(int data) {

if (top == NULL)

top =(struct node *)malloc(1*sizeof(struct node));

top->ptr = NULL;

top->info = data;

else

temp =(struct node *)malloc(1*sizeof(struct node));

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;

int popped = top->info;

free(top);

top = top1;

count--;

return popped;

void display() {

// Display the elements of the stack

top1 = top;

if (top1 == NULL)

printf("\nStack Underflow\n");

return;

printf("The stack is \n");


while (top1 != NULL)

printf("%d--->", top1->info);

top1 = top1->ptr;

printf("NULL\n\n");

int main() {

int choice, value;

printf("\nImplementation of Stack using Linked List\n");

while (1) {

printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");

printf("\nEnter your choice : ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("\nEnter the value to insert: ");

scanf("%d", &value);

push(value);

break;

case 2:

printf("Popped element is :%d\n", pop());

break;

case 3:

display();

break;

case 4:

exit(0);

break;
default:

printf("\nWrong Choice\n");

Implementation of Queue using Linked List

#include < stdio.h >

#include < stdlib.h >

// Structure to create a node with data and the next pointer

struct node {

int data;

struct node * next;

};

struct node * front = NULL;

struct node * rear = NULL;

// Enqueue() operation on a queue

void enqueue(int value) {

struct node * ptr;

ptr = (struct node * ) malloc(sizeof(struct node));

ptr - > data = value;

ptr - > next = NULL;

if ((front == NULL) && (rear == NULL)) {

front = rear = ptr;

} else {

rear - > next = ptr;


rear = ptr;

printf("Node is Inserted\n\n");

// Dequeue() operation on a queue

int dequeue() {

if (front == NULL) {

printf("\nUnderflow\n");

return -1;

} else {

struct node * temp = front;

int temp_data = front - > data;

front = front - > next;

free(temp);

return temp_data;

// Display all elements of the queue

void display() {

struct node * temp;

if ((front == NULL) && (rear == NULL)) {

printf("\nQueue is Empty\n");

} else {

printf("The queue is \n");

temp = front;

while (temp) {

printf("%d--->", temp - > data);

temp = temp - > next;

}
printf("NULL\n\n");

int main() {

int choice, value;

printf("\nImplementation of Queue using Linked List\n");

while (choice != 4) {

printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");

printf("\nEnter your choice : ");

scanf("%d", & choice);

switch (choice) {

case 1:

printf("\nEnter the value to insert: ");

scanf("%d", & value);

enqueue(value);

break;

case 2:

printf("Popped element is :%d\n", dequeue());

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nWrong Choice\n");

return 0;
}

You might also like