0% found this document useful (0 votes)
24 views

Stack and Queue Implementation

The document describes implementations of a stack and queue using arrays in C++. It defines functions for push, pop, peek and display for the stack, and enqueue, dequeue, peek and display for the queue. Both make use of global variables to track the top/rear and front of the data structures, and check for overflow and underflow conditions before performing operations.

Uploaded by

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

Stack and Queue Implementation

The document describes implementations of a stack and queue using arrays in C++. It defines functions for push, pop, peek and display for the stack, and enqueue, dequeue, peek and display for the queue. Both make use of global variables to track the top/rear and front of the data structures, and check for overflow and underflow conditions before performing operations.

Uploaded by

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

Stack Implementation

#include<iostream>
#include<stdlib.h>
using namespace std;
int const MAX=5;
int top=-1;
int stack[MAX];
void push();
void pop();
void peek();
void display();
main()
{
int ch;
while(true)
{
cout<<"\n1.Push\n2.Pop\n3.Display\n4.Peek\n5.Exit\n";
cout<<"Choose your option\n";
cin>>ch;
switch(ch) {
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4: peek();
break;
case 5:exit(0);
}}}
void push() {
int element;
if(top==MAX-1) {
cout<<"Stack overflow\n";
return;
}
else {
cout<<"Enter the element\n";
cin>>element;
top=top+1;
stack[top]=element;
cout<<"Element added\n"<<element;
}}
void pop() {
int element;
if(top<0) {
cout<<"Stack underflow\n";
return;
}
else {
element=stack[top];
top=top-1;
cout<<"Element is removed\n"<<element;
}}
void display() {
int i;
if(top<0) {
cout<<"Empty stack\n";
return;
}
else {
cout<<"Stack is\n";
for(i=top;i>=0;i--)
cout<<"\n"<<stack[i];
}}
void peek() {
cout<<"The value in top is: "<<stack[top];
}

Queue Implementation:
#include<iostream>
#include<stdlib.h>
using namespace std;
int const MAX=5;
void enqueue();
void dequeue();
void display();
void peek();
int rear=-1;
int front=-1;
int queue[MAX];
main()
{
int ch;
while(true)
{
cout<<"\n1.Enqueue\n2.Dequeue\n3.Display\n4.Peek\n5.Exit\n";
cout<<"\nChoose your option\n";
cin>>ch;
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: peek();
break;
case 5: exit(0);
}
}
}
void enqueue(){
int element;
if((rear-front)==MAX-1)
cout<<"Queue overflow\n";
else
{
if(front==-1)
front=0;
cout<<"Enter the new element\n";
cin>>element;
rear=rear+1;
queue[rear]=element;
cout<<"Element is added\n"<<element;
}}
void dequeue()
{
int i;
if(front==-1||front>rear)
{
cout<<"Queue underflow\n";
return;
}
else
{
cout<<"Element is deleted from queue\n"<<queue[front];
for(i=front;i<=rear;i++)
{
queue[i]=queue[i+1];
}
rear=rear-1;
}
}
void display()
{
int i;
if(front==-1||front>rear)
{
cout<<"Queue is empty\n";
}
else
{
cout<<"Queue\n";
for(i=front;i<=rear;i++)
{
cout<<"\n"<<queue[i];
}
}
}
void peek(){
cout<<"The value in front is: "<<queue[front];
}

You might also like