STACK QUEUE DEQUEUE CIRCU...
Done
STACK
Lecture 3.4) Coding With Clicks DSA Course
G Stack Data Structure
Push Operation Pop Operation
isFull isEmpty
top-1 top =0 top 1 top =2 top =1 top0 top=1 top =-1
2
Empty Enpty
Stack Stack
import java.util.Scanner;
class Stack {
int maxSize;
int top;
int[] stack;
I| Constructor to initialize the stack
public Stack(int size) {
maxSize = size;
stack =new int[maxSize];
top =-1; // Stack is initially empty
II Push operation
public void push(int value) {
if (top <maxSize - 1){
stack[++top] = value;
System.out.printin("Pushed: " + value);
}else {
System.out.println("Stack Overflow! Unable to push " + value);
II Pop operation
public int pop() {
if (top >= 0) {
int value=stack[top--1;
System.out.printin("Popped: " + value);
return value;
}else {
System.out.printin("Stack Underflow! No elements to pop.");
return -1;l/Indicating stack is empty
STACK QUEUE DEQUEUE CIRCU... v Done
WPeek operation
public int peek() {
if (top >= 0) {
return stack[top]:
} else {
System.out.printin("Stack is empty.");
return -1; // Indicating stack is empty
}
I/Display stack contents
public void display(){
if (top >= 0){
System.out.print("Stack: ");
for (int i= 0; i <=top; i++) {
System.out.print(stack[i] + " ");
System.out.printin ():
}else {
System.out.printin("Stack is empty.");
}
QUEUE
and Rear become0
When we enter value Front
Front Rear value will increase one by one
Inserting value the
Reat
10
o n t0
10 12
Front0
Rear-1
10 12 15
Front0 Rear-2 and so on.
Deletion of Datafrom Queue
increases
Whenwe delete value Front
15 19
15 17
10 12
Rear6
Front
15
12 15 4 17
Rear
Eront1
15 19
4 17
15
Rear
Front2
STACK QUEUE DEQUEUE CIRCU...v Done
Queue is an entity which can hold a maximum of 100 integers. The
queue enables the user to add integers from the rear and remove
integers from the front.
Define a class Queue with the following details:
Class name: Queue
Data members/instance variables:
Quel]: array to hold the integer elements
size: stores the size of the array
front: to point the index of the front
rear: to point the index of the rear
Member functions:
size = mm, front = 0,
Queue(int mm): constructor to initialize the data
rear =0
from the rear if possible else display
void addele(int v): to add integer
the message "Overflow"
present, otherwise displays
returns elements from front if
int delete(): returns -9999
the message "Underflow" and array elements
void display(): displays the
giving details of only the functions void
Specify the class Queue the other functions have
been
delete(). Assume that
addele(int) and int
defined.
import java.util.Scanner;
class Queue{
int quel]:
int size;
int front;
int rear;
public Queue(int m){
size = m;
int[size];
que = new
front = 0;
rear = 0;
vX
public void addele(int
if(rear == size)
System.out.println("Overflow");
elsel
que[reart+] = v; PUSHED"):
System.out.println(v + "
public int delele(0{
STACK QUEUE DEQUEUE CIRCU...v Done
public int delele({
if(rear == 0)
System.out.printin("Underflow");
return -9999;
int v = que[front++]:
if(front >= rear
front = 0;
rear = 0;
return v;
public void display(){
if(rear == 0)
System.out.println("QUEUE EMPTY");
else{
for(int i= front; i< rear; i++)
System.out.print(que[i] + "");
System.out.println();
Alinear data structure enables the user to add address from rear end
and remove address from front. Define a class Diary with the following
details:
Class name: Diary
Data members/instance variables:
Q: array to store the addresses
size: stores the maximum capacity of the array
start: to point the index of the front end
end: to point the index of the rear end
Member functions:
Diary(int max): constructor to initialize the data member size = max,
start = 0 and end=0
void pushadd(String n): to add address in the diary from the rear end if
possible, otherwise display the message "NO SPACE"
String popadd(): removes and returns the address from the front end of
the diary if any, else returns "?????"
void show): displays all the addresses in the diary
(a) Specify the class Diary giving details of the functions void
pushadd(String) and String popadd(). Assume that the other functions
have been defined.
The main) function and algorithm need not be written.
import java.util.Scanner;
eDion
10:41 l64
STACK QUEUE DEQUEUE CIRCU.. v Done
import java.util.Scanner;
class Diary(
String q:
int size;
int start;
int end;
public Diary(int maxX
size = max;
q= newString[size];
start = 0:
end = 0;
public void pushAdd(String n\
if(end + 1> size)
System.out.println ("NO SPACE");
else{
qlend++] =n;
System.out.printin(n +" PUSHED");
public String popAdd(){
if(start == end)
return "?????";
else{
String a = q[start];
start++;
if(start >= endy
start = 0;
end =0;
return a;
public void show(X
if(start == end)
System.out.println("QUEUE EMPTY");
else{
for(int i= start; i< end; i++)
System.out.print(g[i]+" ");
System.out.printin();
QUESTION : When Front = -1 and Rear = 1
import java.util.Scanner;
class Queue
int front, rear;
int] queue;
10:41 l53
STACK QUEUE DEQUEUE CIRCU... v Done
QUESTION:When Front =-1 and Rear =-1
import java.util.Scarnner;
class Queue
int front, rear;
int] queue;
int capacity:
I|Constructor to initialize the queue
public Queue(int size)
capacity = size;
queue = new int[capacity];
front = -1;
rear = -1;
I| Add an element to the queue
public void insert(int item)
if (rear == capacity- 1){
System.out.printIn("Queue is full. Cannot enqueue " +
item);
}else {
if (front == -1){
front = 0; // First element in the queue
rear++;
queue[rear] = item;
System.out.printin("Enqueued:" +item);
I/Remove an element from the queue
public void delete() {
if (front ==-1){
System.out.printin("Queue is empty. Cannot dequeue.");
}else{
System.out.printin("Dequeued: " + queue[front):
if (front == rear) {
front = rear = -1; Il Queue becomes empty after this
dequeue
}else {
front++;
8:34 l39
STACK QUEUE DEQUEUE CIRCU.. v Done
class Dequeue
int ele]:
int cap;
int front;
int rear;
public Dequeue(int max)
if(max > 100)
max = 100;
cap = max;
ele = new int[cap];:
front = ;
rear = 0;
}
public void pushFront(int v{
if(front == 0)
System.out.printn("full from front");
else
ele[-front] = v;
public int popFront()X
if(front == rear)
return -999:
else{
int v = ele[front++]:
if(front >= rearX
front = 0;
rear = 0;
return v;
}
public void pushRear(int v)\
if(rear == cap)
System.out.println("full from rear");
else
ele[rear++]=v;
public int popRear(){
if(front == rear)
return -999;
else{
int v = ele[--rear];
if(front >= rearX
front = 0;
rear = 0:
return v;
DTACK QUEUE DEQUEUE CIRCU..v Done
CIRCULAR QUEUE
FRONT
0
REAR
5
29
1
29 21 72 13 34 24 24 21
1 2 3 4 5
34 72
4
13
FRONT REAR
3
LINEAR QUEUE CIRCULAR QUEUE
enQueue(14) enQueue(22) enQueue(13) enQueue(-6) ANALYTILABS
ear Front Beg Eror
O000 tont
O0000
deQueue ()
Reo
deQueue() enQueue(9) enQueue(20)
A circular queue is a linear data structure which works on the principle
enQueue(5)
of FIFO, enables the user to enter data from the rear end and remove
data from the front end with the rear end connected to the front end to
form a circular pattern.
Define a class CirQueue with the following details:
Class name: CirQueue
Data members/instance variables:
cql: array to store the integers
cap: stores the maximum capacity of the array
front: to point the index of the front end
rear: to point the index of the rear end
Member functions:
8:35 l39
STACK QUEUE DEQUEUE CIRCU... v Done
import java.util.Scanner;
class CirQueuel
int cq[]:
int cap;
int front;
int rear;
public CirQueue(int max)}
cap = max;
cq = new int[cap]:
front = 0;
rear = 0;
public void push(int vX
if(cap == 0)
System.out.printin ("QUEUE IS FULL");
else{
cq[rear] =V;
rear = (rear + 1) % cq.length;
cap--;
public int pop()X
if(cap == cq.length)
return -9999;
else{
int v = cq[front):
front = (front +1) % cq,length;
cap++;
if(front == rear)
front = rear = 0;
return v;
}
public void show(){
if(cap == cq.length)
System.out.printin("QUEUE IS EMPTY");
else{
int i=front;
int count = cq.length;
while(count > capX
System.out.print(cq[i] + " ");
i= (i+1) %
cq.length;
count--;
System.out.printin():