//STACK
/* Java program to implement basic stack
operations */
class Stack {
static final int MAX = 3;
int top;int n;
int a[] = new int[MAX]; // Maximum size of Stack
/*boolean isEmpty()
{
return (top < 0);
}*/
Stack(int nn)
{
top = -1;
n=nn;
}
boolean push(int x)
{
if (top >= (n - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
top++;
a[top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop()
{
if (top ==-1) {
System.out.println("Stack Underflow");
return 0;\
}
else {
int x = a[top];
top--;
return x;
}
}
void print(){
if(top==-1)
System.out.println("stack empty");
else
for(int i = top;i>=0;i--){
System.out.print(" "+ a[i]);
}
}
}
// Driver code
public class StackMain1 {
public static void main()
{
Stack s = new Stack(3);
s.push(10);
s.push(20);
s.push(30);
s.print();
System.out.println();
System.out.println(s.pop() + " Popped from stack");
//System.out.println("Top element is :" + s.peek());
System.out.print("Elements present in stack :");
s.print();
System.out.println();
s.push(53);
s.print();
System.out.println();
s.push(98);
s.print();
System.out.println();
}
}
//queue
/**
* The class Queue implements operations of queue using Java
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*
* Resetting a Queue should not be a separate case so moment front=front+1 is done, check
* if front==rear. If front==rear that means there are no more items to delete and the
* queue is reset by making front and rear zero
*/
import java.util.*;
class Queue
{
int Q[]; // Array to implement Queue
int size; // Maximum size of the Queue
int front; // Index of front element
int rear; // Index of rear element
Queue(int cap) // Parameterised Constructor
{
size = cap;
Q = new int[size];
front = 0;
rear = 0;
}
void insert(int v) // Function to insert element in Queue
{
if(rear == size) // Condition for Overflow
{
System.out.println("OVERFLOW");
}
else
{
Q[rear] = v; // Storing value in Queue
rear = rear + 1;
}
}
int delete() // Function to delete element from Queue
{
if(front == 0 && rear == 0) // Condition for Underflow
{
System.out.println("UNDERFLOW");
return -999;
}
else
{
int val = Q[front]; // Storing the element which will be removed
front = front + 1;
if(front == rear) // Condition for emptying the Queue
{
front = 0;
rear = 0;
}
return val;
}
}
void display() // Function for printing elements in the queue
{
if(front == 0 && rear == 0)
{
System.out.println("The Queue is empty");
}
else
{
System.out.println("The elements in the queue are : ");
for(int i=front; i<rear; i++)
{
System.out.println(Q[i]);
}
}
}
}
public class QueueImplement1
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Array Queue Test\n");
System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
/* creating object of class arrayQueue */
Queue q = new Queue(n);
/* Perform Queue Operations */
char ch;
do{
System.out.println("\nQueue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("4. display");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
q.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Removed Element = "+q.delete());
break;
case 4 :
q.display();
break;
default : System.out.println("Wrong Entry \n ");
break;
}
/* display Queue */
//q.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}