You are on page 1of 9

JAVA LAB EXPERIMENT-1

AYUSHI TIWARI

CSE-5A

04113302719

Aim:-Implement stack in java programme

class Stack1 {

final int MAX = 1000;

int top;

int a[] = new int[MAX];

Stack1() {

top = -1;

boolean push(int x) {

if (top >= (MAX - 1)) {

System.out.println("Stack Overflow");

return false;

} else {

a[++top] = x;

System.out.println(x + " pushed into stack");

return true;

}
int pop() {

if (top < 0) {

System.out.println("Stack Underflow");

return 0;

} else {

int x = a[top--];

return x;

int peek() {

if (top < 0) {

System.out.println("Stack Underflow");

return 0;

} else {

int x = a[top];

return x;

class main {

public static void main(String args[]) {


Stack1 s = new Stack1();

s.push(10);

s.push(20);

s.push(30);

System.out.println(s.pop() + " Popped from stack");

class Stack1 {
final int MAX = 1000;
int top;
int a[] = new int[MAX];

Stack1() {
top = -1;
}

boolean push(int x) {
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
} else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}

int pop() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
} else {
int x = a[top--];
return x;
}
}

int peek() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
} else {
int x = a[top];
return x;
}
}
}

class main {
public static void main(String args[]) {
Stack1 s = new Stack1();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
}

1(b):-

class Queue {

private static int front, rear, capacity;

private static int queue[];

Queue(int c) {

front = rear = 0;

capacity = c;

queue = new int[capacity];

}
static void enque(int data) {

if (capacity == rear) {

System.out.printf("\nQUEUE IS FULL\n");

return;

} else

queue[rear] = data;

rear++;

return;

static void deque() {

if (rear == front) {

System.out.printf("Queue is empty");

} else {

for (int i = 0; i < rear - 1; i++) {

queue[i] = queue[i + 1];

if (rear < capacity)

queue[rear] = 0;

rear--;

return;

}
static void display() {

int i;

if (front == rear) {

System.out.printf("Queue is empty");

for (i = front; i < rear; i++) {

System.out.printf("%d---", queue[i]);

return;

static void qfront()

if (front == rear) {

System.out.printf("Queue is Empty");

return;

System.out.printf("Front Element is: %d", queue[front]);

return;

class Staticqueue {

public static void main(String[] args) {

Queue q = new Queue(4);


q.display();

q.enque(20);

q.enque(30);

q.enque(40);

q.enque(50);

q.display();

q.enque(60);

q.display();

q.deque();

q.deque();

System.out.printf("\n\nafter two node deletion\n\n");

q.display();

q.qfront();

class Queue {
private static int front, rear, capacity;
private static int queue[];

Queue(int c) {
front = rear = 0;
capacity = c;
queue = new int[capacity];
}

static void enque(int data) {


if (capacity == rear) {
System.out.printf("\nQUEUE IS FULL\n");
return;
} else
queue[rear] = data;
rear++;
return;
}

static void deque() {


if (rear == front) {
System.out.printf("Queue is empty");
} else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}

if (rear < capacity)


queue[rear] = 0;
rear--;
}
return;
}

static void display() {


int i;
if (front == rear) {
System.out.printf("Queue is empty");
}
for (i = front; i < rear; i++) {
System.out.printf("%d---", queue[i]);
}
return;
}
static void qfront()
{
if (front == rear) {
System.out.printf("Queue is Empty");
return;
}
System.out.printf("Front Element is: %d", queue[front]);
return;
}
}

class Staticqueue {
public static void main(String[] args) {
Queue q = new Queue(4);
q.display();
q.enque(20);
q.enque(30);
q.enque(40);
q.enque(50);
q.display();
q.enque(60);
q.display();
q.deque();
q.deque();
System.out.printf("\n\nafter two node deletion\n\n");

q.display();

q.qfront();
}
}

output

You might also like