0% found this document useful (0 votes)
9 views6 pages

Week 06

The document discusses implementing stack and queue data structures using arrays and linked lists. It includes code examples to perform operations like push, pop, peek, enqueue and dequeue on both static and dynamic implementations of stacks and queues.

Uploaded by

YASH MODI
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)
9 views6 pages

Week 06

The document discusses implementing stack and queue data structures using arrays and linked lists. It includes code examples to perform operations like push, pop, peek, enqueue and dequeue on both static and dynamic implementations of stacks and queues.

Uploaded by

YASH MODI
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

Yash Modi

2022BCSE051

WEEK 6

08-04-2024

Stack and Queues

Aim:

Implement the operations on Stack and Queue data structures.

Q1 Implement a static stack using an array with push, pop, peek, peep, isEmpty, size, and display operations.

#include<iostream>

using namespace std;

class Stack{

public:

int data;

Stack* next;

Stack(int data1){data=data1;next=NULL;}

Stack* top=NULL;

void push(int data){

Stack* temp=new Stack(data);

if(temp==NULL){cout<<"Stack Overflow"<<endl;}

temp->data=data;

temp->next=top;

top=temp;

void pop(){

Stack* temp;

if(top==NULL){cout<<"Stack Underflow"<<endl;}

temp=top;

top=top->next;

delete temp;

int peek(){

if(top==NULL){cout<<"Stack underflow"<<endl;return -1;}

return top->data;

int peep(int i){

Stack* temp=top;

if(temp==NULL){return -1;}
Yash Modi
2022BCSE051

if(i==1){return top->data;}

int count=0;

while(temp!=NULL){

count++;

if(count==i){return temp->data;}

temp=temp->next;

return -1;

bool isEmpty(){if(top==NULL){return true;}else{return false;}}

int size(){

int count=0;

Stack* temp=top;

while(temp!=NULL){count++;temp=temp->next;}

return count;

void display(){

Stack* temp=top;

if(top==NULL){cout<<"Stack Underflow"<<endl;}

while(temp!=NULL){cout<<temp->data<<" ";temp=temp->next;}

cout<<endl;

};

int main(){

Stack s(100);

s.push(1);s.push(5);s.push(10);

cout<<s.peek()<<endl;

s.display();

cout<<s.isEmpty()<<endl;

cout<<"Size of Stack : "<<s.size();

return 0;

}
Yash Modi
2022BCSE051

Q2 Write a program for the dynamic implementation of stack using a single linked list with the

above functionalities.

#include<iostream>

using namespace std;

class Stack{

public:

int data;

Stack* next;

Stack(int data1) {data=data1;next=NULL;}

Stack* top=NULL;

void push(int data) {

Stack* temp=new Stack(data);

if(temp==NULL){cout<<"Stack Overflow"<<endl;}

temp->data=data;

temp->next=top;

top=temp;

void pop() {

Stack* temp;

if(top==NULL){cout<<"Stack Underflow"<<endl;}

temp=top;

top=top->next;

delete temp;

int peek() {

if(top==NULL){cout<<"Stack underflow"<<endl;return -1;}

return top->data;

int peep(int i) {

Stack* temp=top;

if(temp==NULL)return -1;

if(i==1)return top->data;

int count=0;

while(temp!=NULL){count++;if(count==i)return temp->data;temp=temp->next;}

return -1;
Yash Modi
2022BCSE051

bool isEmpty(){if(top==NULL){return true;}else{return false;}}

int size(){int count=0;Stack* temp=top;while(temp!=NULL){count++;temp=temp->next;}return count;}

void display(){Stack* temp=top;if(top==NULL){cout<<"Stack Underflow"<<endl;}while(temp!=NULL){cout<<temp-


>data<<" ";temp=temp->next;}cout<<endl;}

};

int main() {

Stack s(NULL);

s.push(1);s.push(5);s.push(10);

cout<<s.peek()<<endl;

s.display();

cout<<s.isEmpty()<<endl;

cout<<"Size of Stack : "<<s.size();

return 0;}

Q3 Write a program for the static implementation of a circular queue using an array with enqueue, dequeue, front, isEmpty,
and size operations.

#include<bits/stdc++.h>

using namespace std;

class Queue{

public:

int *ar;

int size;

int front,rear;

Queue(int size){

this->size=size;

ar= new int[size];

front=0;

rear=0;

void enqueue(int data){

rear=(rear+1)%size;
Yash Modi
2022BCSE051

if(front==rear){cout<<"Queue, Overflow"<<endl;if(rear==0)rear=size-1;else rear-=1;}

else{ar[rear]=data;}

int dqueue(){

if(front==rear)cout<<"Queue, Underflow"<<endl;

else{front=(front+1)%size;int temp=ar[front];return temp;}

void display(){

if(front==rear){cout<<"Queue is empty"<<endl;}

else{for(int i=front+1;i<=rear;i++){cout<<ar[i]<<" ";}cout<<endl;}

};

int main() {

Queue q(8);

q.enqueue(1);q.enqueue(2);q.enqueue(3);q.enqueue(4);q.enqueue(5);q.enqueue(6);q.enqueue(7);q.display();

q.dqueue();q.dqueue();q.dqueue();q.dqueue();q.display();

return 0;

Q4 Write a program for the dynamic implementation of a queue using a single linked list with the

above functionalities.

#include <bits/stdc++.h>

using namespace std;

class Queue {

public:

int data;

Queue* next;

Queue(int data) {this->data = data;next = nullptr;}

Queue* front = nullptr;

void enqueue(int data) {

Queue* newNode = new Queue(data);

if (front == nullptr) {front = newNode;newNode->next = newNode;}

else {Queue* temp = front;while (temp->next != front) {temp = temp->next;}

temp->next = newNode;newNode->next = front;}


Yash Modi
2022BCSE051

int dequeue() {

if (front == nullptr) {cout << "Queue Underflow!" << endl;return -1;}

int data = front->data;if (front->next == front) {delete front;front = nullptr;}

else {Queue* temp = front;while (temp->next != front) {temp = temp->next;}

Queue* firstNode = front;front = front->next;temp->next = front;delete firstNode;}

return data;

int peek() {

if (front == nullptr) {cout << "Queue is empty." << endl;return -1;}

return front->data;

bool isEmpty() {return front == nullptr;}

void display() {

if (front == nullptr) {cout << "Queue is empty." << endl;return;}

Queue* temp = front;do {cout << temp->data << " ";temp = temp->next;} while (temp != front);

cout << endl;

};

int main() {

Queue q(1);q.enqueue(10);q.enqueue(20);q.enqueue(30);q.enqueue(40);q.display();

cout << "Peeked element: " << q.peek() << endl;q.dequeue();q.dequeue();q.display();

return 0;

You might also like