You are on page 1of 13

STACK

import java.util.Scanner;

class StackX {
private int MaxSize;
private int[] Stackarray;
private int top;

public StackX(int m) {
MaxSize = m;
Stackarray = new int[MaxSize];
top = -1;
}

public int IsFull() {


if (top == MaxSize - 1) {
return 1;
} else {
return 0;
}
}

public int IsEmpty() {


if (top == -1) {
return 1;
} else {
return 0;
}
}

public void push(int x) {


if (IsFull() == 1) {
System.out.println("Stack is full");
} else {
top++;
Stackarray[top] = x;
}
}

public int pop() {


if (IsEmpty() == 1) {
System.out.println("The Stack is empty");
return -1;
} else {
int x = Stackarray[top];
top--;
return x;
}
}

public int peek() {


return Stackarray[top];
}

public void display() {


int i;
for (i = 0; i <= top; i++) {
System.out.println(Stackarray[i]);
}
}

public int getSize() {


return MaxSize;
}
}

public class JAVALAB {


public static void main(String[] args) {
StackX theStack = new StackX(3);
Scanner scan = new Scanner(System.in);
int choice = 0;
do {
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Size");
System.out.println("5. Display/Print");
System.out.println("6. Exit");
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the value to push");
int data = scan.nextInt();
theStack.push(data);
break;
case 2:
int p;
p = theStack.pop();
if (p != -1)
System.out.println("The popped element is " + p);
break;
case 3:
System.out.println("The top is: " + theStack.peek());
break;
case 4:
System.out.println("The Size is: " + theStack.getSize());
break;
case 5:
System.out.println("The elements of stack are:");
theStack.display();
break;
case 6:
break;
default:
System.out.println("Enter Again");
break;
}
} while (choice != 6);
scan.close();
}
}

QUEUE
import java.util.Scanner;

class queue {
private int MaxSize;
private int[] Queuearray;
private int rear, front;

public queue(int m) {
MaxSize = m;
Queuearray = new int[MaxSize];
rear = -1;
front = -1;
}

public int IsFull() {


if (rear == MaxSize - 1) {
return 1;
} else {
return 0;
}
}
public int IsEmpty() {
if (front == -1) {
return 1;
} else {
return 0;
}
}

public void enqueue(int x) {


if (IsFull() == 1) {
System.out.println("Queue is full");
} else if (rear == -1 && front == -1)
{
rear = front = 0;
Queuearray[rear] = x;
}
else {
rear++;
Queuearray[rear] = x;
}
}

public int dequeue() {


if (IsEmpty() == 1) {
System.out.println("The Queue is empty");
return -1;
} else if (front == rear)
{
int x = Queuearray[front];
front = rear = -1;
return x;
}
else
{
int x = Queuearray[front];
front++;
return x;
}
}

public int frontelement() {


return Queuearray[front];
}

public int rearelement() {


return Queuearray[rear];
}
public void display() {
int i;
for (i = front; i <= rear; i++) {
System.out.println(Queuearray[i]);
}
}

public int getSize() {


return MaxSize;
}
}

public class JAVALAB {


public static void main(String[] args) {
queue thequeue = new queue(3);
Scanner scan = new Scanner(System.in);
int choice = 0;
do {
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Front");
System.out.println("4. Size");
System.out.println("5. Rear");
System.out.println("6. Display");
System.out.println("7. Exit");
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the value to enqueue");
int data = scan.nextInt();
thequeue.enqueue(data);
break;
case 2:
int p;
p = thequeue.dequeue();
if (p != -1)
System.out.println("The dequeued element is " + p);
break;
case 3:
System.out.println("The front element is: " +
thequeue.frontelement());
break;
case 4:
System.out.println("The Size is: " + thequeue.getSize());
break;
case 5:
System.out.println("The rear element is:"
+thequeue.rearelement());
break;
case 6:
System.out.println("The elements of queue are:");
thequeue.display();
break;
case 7:
break;
default:
System.out.println("Enter Again");
break;
}
} while (choice != 7);
scan.close();
}
}

CIRCULAR QUEUE
import java.util.Scanner;

class circularqueue {
private int MaxSize;
private int[] Queuearray;
private int rear, front;

public circularqueue(int m) {
MaxSize = m;
Queuearray = new int[MaxSize];
rear = -1;
front = -1;
}

public int IsFull() {


if (front == (rear+1)%MaxSize) {
return 1;
} else {
return 0;
}
}

public int IsEmpty() {


if (front == -1) {
return 1;
} else {
return 0;
}
}

public void enqueue(int x) {


if (IsFull() == 1) {
System.out.println("Queue is full");
} else if (rear == -1 && front == -1)
{
rear = front = 0;
Queuearray[rear] = x;
}
else {
rear = (rear+1)%MaxSize;
Queuearray[rear] = x;
}
}

public int dequeue() {


if (IsEmpty() == 1) {
System.out.println("The Queue is empty");
return -1;
} else if (front == rear)
{
int x = Queuearray[front];
front = rear = -1;
return x;
}
else
{
int x = Queuearray[front];
front = (front+1)%MaxSize;
return x;
}
}

public int frontelement() {


return Queuearray[front];
}

public int rearelement() {


return Queuearray[rear];
}

public void display() {


int i;
for (i = front; i != rear; i = (i+1)%MaxSize) {
System.out.println(Queuearray[i]);
}
System.out.println(Queuearray[rear]);
}

public int getSize() {


return MaxSize;
}
}

public class JAVALAB {


public static void main(String[] args) {
circularqueue thequeue = new circularqueue(3);
Scanner scan = new Scanner(System.in);
int choice = 0;
do {
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Front");
System.out.println("4. Size");
System.out.println("5. Rear");
System.out.println("6. Display");
System.out.println("7. Exit");
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the value to enqueue");
int data = scan.nextInt();
thequeue.enqueue(data);
break;
case 2:
int p;
p = thequeue.dequeue();
if (p != -1)
System.out.println("The dequeued element is " + p);
break;
case 3:
System.out.println("The front element is: " +
thequeue.frontelement());
break;
case 4:
System.out.println("The Size is: " + thequeue.getSize());
break;
case 5:
System.out.println("The rear element is:"
+thequeue.rearelement());
break;
case 6:
System.out.println("The elements of queue are:");
thequeue.display();
break;
case 7:
break;
default:
System.out.println("Enter Again");
break;
}
} while (choice != 7);
scan.close();
}
}

DOUBLE ENDED QUEUE


import java.util.Scanner;

class doubleendedqueue {
private int MaxSize;
private int[] Queuearray;
private int rear, front;

public doubleendedqueue(int m) {
MaxSize = m;
Queuearray = new int[MaxSize];
rear = -1;
front = -1;
}

public int IsFull() {


if (rear == MaxSize - 1) {
return 1;
} else {
return 0;
}
}
public int IsEmpty() {
if (front == -1) {
return 1;
} else {
return 0;
}
}

public void insertrear (int x) {


if (IsFull() == 1) {
System.out.println("Queue is full");
} else if (rear == -1 && front == -1)
{
rear = front = 0;
Queuearray[rear] = x;
}
else {
rear++;
Queuearray[rear] = x;
}
}

public int deletefront() {


if (IsEmpty() == 1) {
System.out.println("The Queue is empty");
return -1;
} else if (front == rear)
{
int x = Queuearray[front];
front = rear = -1;
return x;
}
else
{
int x = Queuearray[front];
front++;
return x;
}
}

public int deleterear(){


if (IsEmpty()==1){
System.out.println("The Queue is empty");
return -1;
}
else if (front==rear){
int x = Queuearray[rear];
front=rear=-1;
return x;
}
else {
int x = Queuearray[rear];
rear--;
return x;
}
}
public void insertfront(int x){
if (front==0){
System.out.println("Queue is full");
}
else if(front==-1){
front=rear=0;
Queuearray[front]=x;
}
else {
front--;
Queuearray[front]=x;
}
}

public int frontelement() {


return Queuearray[front];
}

public int rearelement() {


return Queuearray[rear];
}

public void display() {


int i;
for (i = front; i <= rear; i++) {
System.out.println(Queuearray[i]);
}
}

public int getSize() {


return MaxSize;
}
}

public class DSALAB {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of double ended queue");
int size= scan.nextInt();
doubleendedqueue thequeue = new doubleendedqueue(size);
int choice = 0;
do {
System.out.println("1. Insert rear");
System.out.println("2. Delete front");
System.out.println("3. Front element");
System.out.println("4. Size");
System.out.println("5. Rear");
System.out.println("6. Display");
System.out.println("7. Insert front");
System.out.println("8. Delete rear");
System.out.println("9. Exit");
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the value to insert rear");
int data = scan.nextInt();
thequeue.insertrear(data);
break;
case 2:
int p;
p = thequeue.deletefront();
if (p != -1)
System.out.println("The element deleted at front is "
+ p);
break;
case 3:
System.out.println("The front element is: " +
thequeue.frontelement());
break;
case 4:
System.out.println("The Size is: " + thequeue.getSize());
break;
case 5:
System.out.println("The rear element is:"
+thequeue.rearelement());
break;
case 6:
System.out.println("The elements of queue are:");
thequeue.display();
break;
case 7:
System.out.println("Enter the value to insert front");
int frontData = scan.nextInt();
thequeue.insertfront(frontData);
break;
case 8:
int rearDeleted = thequeue.deleterear();
if (rearDeleted != -1)
System.out.println("The element deleted at rear is " +
rearDeleted);
break;
case 9:
break;
default:
System.out.println("Enter Again");
break;
}
} while (choice != 9);
scan.close();
}
}

You might also like