// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Stack {
private static final int size = 5;
private int top;
private int[] S;
public Stack() {
S = new int[size];
top = -1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int value) {
if (top == size - 1) {
System.out.println("Stack is full. Cannot push " + value);
} else {
S[++top] = value;
System.out.println("Pushed " + value);
}
}
public void pop() {
if (isEmpty()) {
System.out.println("Stack is empty");
} else {
System.out.println("Deleted " + S[top--]);
}
}
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
} else {
return S[top];
}
}
public static void main(String args[]) {
Stack St = new Stack();
St.push(10);
St.push(20);
St.push(30);
St.push(40);
St.push(50);
St.push(60); // This will show "Stack is full" message
System.out.print("Top element = " + St.peek());
St.pop();
St.pop();
System.out.println("Top element after deleting = " + St.peek());
}
}
//......
// Java program to implement queue using array.
class QueueArray {
public static void main(String[] args) {
Queue q = new Queue(4);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
System.out.println(q.getFront());
q.dequeue();
q.enqueue(4);
q.display();
}
}
class Queue {
int[] arr;
int size;
int capacity;
int front;
int rear; // Added rear pointer for proper queue implementation
Queue(int qSize) {
size = 0;
front = 0;
rear = -1;
capacity = qSize;
arr = new int[qSize];
}
// Function to add an element to queue
void enqueue(int x) {
// If queue is full
if (size == capacity) {
System.out.println("Queue is full!");
return;
}
rear = (rear + 1) % capacity; // Circular queue implementation
arr[rear] = x;
// Increment queue size.
size++;
}
// Function to pop front element from queue.
void dequeue() {
// If queue is empty
if (size == 0) {
System.out.println("Queue is empty!");
return;
}
// Remove element from front
front = (front + 1) % capacity; // Circular queue implementation
// decrement queue size
size--;
// Reset front and rear if queue becomes empty
if (size == 0) {
front = 0;
rear = -1;
}
}
// Function which returns the front element.
int getFront() {
// if queue is empty
if (size == 0) return -1;
return arr[front];
}
// Function which prints the elements of array.
void display() {
if (size == 0) {
System.out.println("Queue is empty!");
return;
}
int index = front;
for (int i = 0; i < size; i++) {
System.out.print(arr[index] + " ");
index = (index + 1) % capacity;
}
System.out.println();
}
}
//......
class LinkedListStack {
public static void main(String[] args) {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(10);
stack.push(20);
stack.push(30);
stack.displayStack();
System.out.println("\n" + stack.pop() + " popped from Stack");
System.out.println("Top element is: " + stack.peek());
}
}
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class StackUsingLinkedList {
private Node top;
public StackUsingLinkedList() {
this.top = null;
}
public boolean isEmpty() {
return top == null;
}
public void push(int data) {
Node newNode = new Node(data);
if (top == null) {
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
}
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
} else {
int popped = top.data;
top = top.next;
return popped;
}
}
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
} else {
return top.data;
}
}
public void displayStack() {
Node currentNode = top;
System.out.println("Stack Elements : ");
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
}
}
//.......
🎁 Copied! Redirecting...