0% found this document useful (0 votes)
2 views7 pages

Stack and Queue Implementations in C++

The document contains C++ implementations of data structures including a Stack, Queue, Stack using Queue, and Queue using Stack. Each class provides methods for basic operations such as push, pop, top/peek, and print. The main function demonstrates the usage of the Queue class with enqueue and dequeue operations.

Uploaded by

Nisha sharma
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)
2 views7 pages

Stack and Queue Implementations in C++

The document contains C++ implementations of data structures including a Stack, Queue, Stack using Queue, and Queue using Stack. Each class provides methods for basic operations such as push, pop, top/peek, and print. The main function demonstrates the usage of the Queue class with enqueue and dequeue operations.

Uploaded by

Nisha sharma
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

#include<bits/stdc++.

h>
using namespace std;
#define MAX 100

​ ​ ​ ​ STACK
class Stack{
private:
int f;
int arr[MAX];

public:
Stack(){
f=-1;
}

bool empty(){
return f==-1;
}

bool full(){
return f==MAX-1;
}

void push(int x){


if(full()){
cout<<"stack is full"<<x<<endl;
return;
}

f++;
arr[f]=x;
cout<<x<<" pushed to stack\n";
}

int pop(){
if(empty()){
cout<<"stack is empty";
return -1;
}
return arr[f--];
}

int top(){
if(empty()){
cout<<"stack is empty";
return -1;
}
return arr[f];
}

void print(){
if(empty()){
cout<<"stack is empty";
return;
}

cout<<"stack element: ";


for(int i=f; i>=0; i--){
cout<<arr[i]<<" ";
}
cout<<endl;
}

};

​ ​ ​ ​ QUEUE
class Queue{
private:
int arr[MAX];
int f,r;

public:
Queue(){
f=0, r=-1;
}
bool empty(){
return f>r;
}

bool full(){
return r==MAX-1;
}

void enqueue(int x){


if(full()){
cout<<"queue is full"<<x<<endl;
return;
}
r++;
arr[r]=x;
cout<<x<<" enqueueed in queue\n";
}

int dequeue(){
if(empty()){
cout<<"queue is empty\n";
return -1;
}
return arr[f++];
}

int front(){
if(empty()){
cout<<"queue is empty\n";
return -1;
}
return arr[f];
}

void print(){
if(empty()){
cout<<"queue is empty\n";
return ;
}
cout<<"queue elements: ";
for(int i=f; i<=r; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}

};

int main(){
// Stack st;
Queue st;
[Link](1);
[Link](2);
[Link](3);
[Link](4);

[Link]();

cout<<"front: "<<[Link]()<<endl;
cout<<"dequeue: "<<[Link]()<<endl;

[Link]();
return 0;
}
​ ​ ​ ​ STACK USING QUEUE
class Stack_using_queue{

queue<int> q;

public:
void push(int x){
int size=[Link]();
[Link](x);

for(int i=0; i<size; i++){


[Link]([Link]());
[Link]();
}
cout<<x<<" pushed to stack\n";
}

int pop(){
if([Link]()){
cout<<"stack is empty\n";
return -1;
}
int val=[Link]();
[Link]();
return val;
}

int top(){
if([Link]()){
cout<<"stack is empty\n";
return -1;
}
return [Link]();
}

bool isempty(){
return [Link]();
}

void print(){
if([Link]()){
cout<<"stack is empty\n";
return;
}
cout<<"stack elements: ";
queue<int> temp=q;
while(![Link]()){
cout<<[Link]()<<" ";
[Link]();
}
cout<<endl;
}

};

​ ​ ​ QUEUE USING STACK


class Queue_using_stack{

stack<int> inst, outst;

public:
Queue_using_stack(){}

void push(int x){


[Link](x);
}

int pop(){
if([Link]()){
while(![Link]()){
[Link]([Link]());
[Link]();
}
}
int val=[Link]();
[Link]();
return val;
}

int peek(){
if([Link]()){
while(![Link]()){
[Link]([Link]());
[Link]();
}
}
return [Link]();
}

bool empty(){
return [Link]() && [Link]();
}

};

You might also like