0% found this document useful (0 votes)
25 views3 pages

Ila Shukla 22030121093 Assignment 3

The document contains code for Stack, Queue, and DemoDataStructure classes. The Stack and Queue classes implement push/pop and insert/delete methods respectively to add and remove elements. The DemoDataStructure class contains a main method that demonstrates using the Stack and Queue classes by pushing/popping elements to a stack and inserting/deleting elements from a queue, and printing the stack and queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Ila Shukla 22030121093 Assignment 3

The document contains code for Stack, Queue, and DemoDataStructure classes. The Stack and Queue classes implement push/pop and insert/delete methods respectively to add and remove elements. The DemoDataStructure class contains a main method that demonstrates using the Stack and Queue classes by pushing/popping elements to a stack and inserting/deleting elements from a queue, and printing the stack and queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: Ila Shukla PRN: 22030121093 Division C, Sem III, BCA

Stack.java
package MyDataStructure;
public class Stack {
private int maxSize;
private int top;
private int[] stackArray;

public Stack(int size) {


maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

public void push(int value) {


if (top < maxSize - 1) {
stackArray[++top] = value;
System.out.println("Pushed " + value + " to the stack.");
} else {
System.out.println("Stack is full. Cannot push " + value +
".");
}
}

public int pop() {


if (top >= 0) {
int poppedValue = stackArray[top--];
System.out.println("Popped " + poppedValue + " from the
stack.");
return poppedValue;
} else {
System.out.println("Stack is empty. Cannot pop.");
return -1;
}
}

public void printStack() {


if (top >= 0) {
System.out.print("Stack: ");
for (int i = 0; i <= top; i++) {
System.out.print(stackArray[i] + " ");
}
System.out.println();
} else {
System.out.println("Stack is empty.");
}
}
}
Name: Ila Shukla PRN: 22030121093 Division C, Sem III, BCA

Queue.java
package MyDataStructure;
public class Queue {
private int maxSize;
private int front;
private int rear;
private int[] queueArray;

public Queue(int size) {


maxSize = size;
queueArray = new int[maxSize];
front = 0;
rear = -1;
}

public void insert(int value) {


if (rear < maxSize - 1) {
queueArray[++rear] = value;
System.out.println("Inserted " + value + " into the queue.");
} else {
System.out.println("Queue is full. Cannot insert " + value +
".");
}
}

public int delete() {


if (front <= rear) {
int deletedValue = queueArray[front++];
System.out.println("Deleted " + deletedValue + " from the
queue.");
return deletedValue;
} else {
System.out.println("Queue is empty. Cannot delete.");
return -1;
}
}

public void printQueue() {


if (front <= rear) {
System.out.print("Queue: ");
for (int i = front; i <= rear; i++) {
System.out.print(queueArray[i] + " ");
}
System.out.println();
} else {
System.out.println("Queue is empty.");
}
}
}
Name: Ila Shukla PRN: 22030121093 Division C, Sem III, BCA

DemoDataStructure.java
package DemoPack;

public class DemoDataStructure {


public static void main(String[] args) {
MyDataStructure.Stack stack = new MyDataStructure.Stack(5);
MyDataStructure.Queue queue = new MyDataStructure.Queue(5);

// Demonstrate stack
stack.push(1);
stack.push(2);
stack.push(3);
stack.printStack();
stack.pop();
stack.printStack();

// Demonstrate queue
queue.insert(10);
queue.insert(20);
queue.insert(30);
queue.printQueue();
queue.delete();
queue.printQueue();
}
}

Output-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy