Untitled
unknown
plain_text
4 years ago
3.5 kB
3
Indexable
package day1; public class MyClass { public static void main(String args[]){ //create object of Stack class Stack stack = new Stack(); //push value into the stack stack.push(10); stack.push(20); stack.push(30); //display the stack stack.disp(); //pop value from stack and then display the stack System.out.println("\n" + stack.pop() + " pop from stack"); stack.disp(); //create object of Queue class Queue queue = new Queue(); //enqueue value into the stack queue.enqueue(10); queue.enqueue(20); queue.enqueue(30); //display the queue queue.disp(); //dequeue value from queue and then display the queue System.out.println(queue.dequeue() + " dequeue from queue"); queue.disp(); } } //create stack class class Stack{ //declare variable final int MAX_SIZE = 5; int top = -1; //declare array int stackArray[] = new int[MAX_SIZE]; //push() method public void push(int x){ //check stack is full or not if(top == MAX_SIZE-1){ System.out.println("Stack is overflow"); }else{ //assign value into the stack stackArray[++top] = x; } } //pop() method public int pop(){ //check stack is empty or not if(top < 0){ //if stack is empty, return -1 return -1; } //return delete value return stackArray[top--]; } //isfull() method public void isfull(){ if(top == MAX_SIZE-1){ System.out.println("Stack is overflow"); } } //isempty() method public void isempty(){ if(top < 0){ System.out.println("Stack is underflow"); } } //disp() method public void disp(){ if(top > 0){ System.out.println("Stack elements are"); //print the stack for(int i = 0; i <= top; i++){ System.out.println(stackArray[i]); } } } } //create Queue class class Queue{ //declare variable final int MAX_SIZE = 5; int f = 0, r = 0; //declare array int queueArray[] = new int[MAX_SIZE]; //enqueue() method public void enqueue(int x){ //check queue is full or not if(r == MAX_SIZE){ System.out.println("Queue is overflow"); }else{ //assign value into the queue queueArray[r++] = x; } } //dequeue() method public int dequeue(){ //check queue is empty or not if(f == r){ //if queue is empty, return -1 return -1; } //return delete value return queueArray[f++]; } //isfull() method public void isfull(){ if(r == MAX_SIZE){ System.out.println("Queue is overflow"); } } //isempty() method public void isempty(){ if(f == r){ System.out.println("Queue is underflow"); } } //disp() method public void disp(){ System.out.println("Queue elements are"); //print the queue for(int i = f; i < r; i++){ System.out.println(queueArray[i]); } } } [ Х C Q Q O T 1: Project 2 Eile Edit View Navigate Code Analyze Refactor Build Run Tools Vcs Window Help Stack and Queue [C:\
Editor is loading...