Week 06
Week 06
2022BCSE051
WEEK 6
08-04-2024
Aim:
Q1 Implement a static stack using an array with push, pop, peek, peep, isEmpty, size, and display operations.
#include<iostream>
class Stack{
public:
int data;
Stack* next;
Stack(int data1){data=data1;next=NULL;}
Stack* top=NULL;
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(){
return top->data;
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;
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;
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>
class Stack{
public:
int data;
Stack* next;
Stack* top=NULL;
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() {
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
};
int main() {
Stack s(NULL);
s.push(1);s.push(5);s.push(10);
cout<<s.peek()<<endl;
s.display();
cout<<s.isEmpty()<<endl;
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>
class Queue{
public:
int *ar;
int size;
int front,rear;
Queue(int size){
this->size=size;
front=0;
rear=0;
rear=(rear+1)%size;
Yash Modi
2022BCSE051
else{ar[rear]=data;}
int dqueue(){
if(front==rear)cout<<"Queue, Underflow"<<endl;
void display(){
if(front==rear){cout<<"Queue is empty"<<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>
class Queue {
public:
int data;
Queue* next;
int dequeue() {
return data;
int peek() {
return front->data;
void display() {
Queue* temp = front;do {cout << temp->data << " ";temp = temp->next;} while (temp != front);
};
int main() {
Queue q(1);q.enqueue(10);q.enqueue(20);q.enqueue(30);q.enqueue(40);q.display();
return 0;