0% found this document useful (0 votes)
60 views

Stack Queue 5

The class Stack123 implements a stack data structure using an array. It contains methods to push elements onto the stack, pop elements off the stack, check if the stack is empty, and view the top element. A constructor initializes the backing array and sets the top index to -1. Elements are added and removed by incrementing or decrementing the top index.

Uploaded by

fghf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Stack Queue 5

The class Stack123 implements a stack data structure using an array. It contains methods to push elements onto the stack, pop elements off the stack, check if the stack is empty, and view the top element. A constructor initializes the backing array and sets the top index to -1. Elements are added and removed by incrementing or decrementing the top index.

Uploaded by

fghf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Question:21

A Stack is a linear data structure in which the operations are performed based
on LIFO (Last In First Out).

Define a class Array_to_Stack with the following details:

Class name :StackArray1

Data member/instance variable:

array[] array to hold the integer


size stores the maximum capacity of the queue
N to point the index
top to point index of the topmost element

Member functions/methods:

StackArray()
constructor to initialize the data member array[] = max N = top = O and create
the integer array

void to add integers from the rear index if possible else display the

void push(int v)

message("Stack full”)to remove and return elements from

int pop_dat() front, if any, else returns -999

Specify the class Stack giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..

ISC Computer Science | 104


Source code
import java.util.Scanner;

class StackArray1

int arr[],N,top;

int size;

StackArray1()

arr=new int [size];

N=size;

top=-1;

void push(int val)

if(top+1<N)

top++;

arr[top]=val;

else

System.out.println("OVERFLOW");

int pop()

{
ISC Computer Science | 105
if(top==-1)

return -9999;

else

int item=arr[top];

top--;

return item;

void push()

if(top==-1)

System.out.println("OVERFLOW");

else

System.out.println("The number are:");

for(int i=0;i>=0;i++)

System.out.println(arr[i]);

public static void main()

Scanner sc = new Scanner(System.in);

StackArray1 ob = new StackArray1();

ISC Computer Science | 106


int ans=1;

while(ans==1)

System.out.println("\f***MENU****");

System.out.println("Enter your choice:");

int ch = sc.nextInt();

switch(ch)

case 1:
System.out.println("Enter the number which you wants to push/insert in
stack");

int num=sc.nextInt();

ob.push(num);

break;

case 2:

int x=ob.pop();

if(x==-9999)

System.out.println("UNDERFLOW");

else

System.out.println(x+"is poped /delete");

break;

case 3:

ob.pop();

break;

default:

ISC Computer Science | 107


System.out.println("Wrong choice ..... ");

}
System.out.println("Do you wish to continue press 1 otherwise any other
number...");

ans=sc.nextInt();

Variable Description
variable data type Purpose
arr[] Integer to store an element
N integer to store elements
top integer to select top among elements
size integer to store elemets size

ISC Computer Science | 108


Output

ISC Computer Science | 109


Question:22
A Stack is a linear data structure in which the operations are performed based
on LIFO (Last In First Out).

Define a class Array_to_Stack with the following details:

Class name :StackArray1

Data member/instance variable:

array[] array to hold the integer


size stores the maximum capacity of the queue
N to point the index
top to point index of the topmost element

Member functions/methods:

StackArray()
constructor to initialize the data member array[] = max N = top = O and create
the integer array

void to add integers from the rear index if possible else display the

void push(int v)

message("Stack full”)to remove and return elements from

int pop_dat() front, if any, else returns -999

Specify the class Stack giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..

ISC Computer Science | 110


Source code
import java.util.Scanner;

public class Stack {

private int[] arr;

private int top;

// Constructor to initialize the stack

public Stack(int size) {

arr = new int[size];

top = -1;

// Method to push an element onto the stack

public void push(int num) {

if (top == arr.length - 1) {

System.out.println("Stack is full");

} else {

top++;

arr[top] = num;

// Method to pop an element from the stack

public int pop() {

ISC Computer Science | 111


if (top == -1) {

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

return -1;

} else {

int poppedElement = arr[top];

top--;

return poppedElement;

// Method to get the top element of the stack

public int peek() {

if (top == -1) {

System.out.println("Stack is empty");

return -1;

} else {

return arr[top];

// Method to check if the stack is empty

public boolean isEmpty() {

return top == -1;

ISC Computer Science | 112


public void display() {

if (top == -1) {

System.out.println("Stack is empty");

} else {

System.out.print("Stack elements: ");

for (int i = top; i >= 0; i--) {

System.out.print(arr[i] + " ");

System.out.println();

public static void main(String[] args) {

System.out.println("Initialize a stack:");

Stack stack = new Stack(5);

System.out.println("Is the stack empty? " + stack.isEmpty());

System.out.println("\nInput some elements on the stack:");

stack.push(1);

stack.push(2);

stack.push(3);

stack.push(4);

stack.push(5);

stack.display();

ISC Computer Science | 113


System.out.println("\nTop element of the stack: " + stack.peek());

System.out.println("\nRemove two element from the stack:");

stack.pop();

stack.pop();

stack.display();

System.out.println("\nTop element of the stack after popping: " +


stack.peek());
System.out.println("\nIs the stack empty? " + stack.isEmpty());

Variable description
variable data type Purpose
arr[] Integer to store an element
top integer to select top among elements
size integer to store elemets size

ISC Computer Science | 114


Output

ISC Computer Science | 115


Question:23
A Stack is a linear data structure in which the operations are performed based
on LIFO (Last In First Out).

Define a class Array_to_Stack with the following details:

Class name :StackArray1

Data member/instance variable:

array[] array to hold the integer


size stores the maximum capacity of the queue
N to point the index
top to point index of the topmost element

Member functions/methods:

StackArray()
constructor to initialize the data member array[] = max N = top = O and create
the integer array

void to add integers from the rear index if possible else display the

void push(int v)

message("Stack full”)to remove and return elements from

int pop_dat() front, if any, else returns -999

Specify the class Stack giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..

ISC Computer Science | 116


Source code
import java.util.Scanner;

public class Stack123 {

private int[] arr;

private int top;

// Constructor to initialize the stack

public Stack123(int size) {

arr = new int[size];

top = -1;

// Method to push an element onto the stack

public void push(int num) {

if (top == arr.length - 1) {

System.out.println("Stack is full");

} else {

top++;

arr[top] = num;

}A

// Method to pop an element from the stack

public int pop() {

ISC Computer Science | 117


if (top == -1) {

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

return -1;

} else {

int poppedElement = arr[top];

top--;

return poppedElement;

// Method to get the top element of the stack

public int peek() {

if (top == -1) {

System.out.println("Stack is empty");

return -1;

} else {

return arr[top];

// Method to check if the stack is empty

public boolean isEmpty() {

return top == -1;

ISC Computer Science | 118


public void sort() {

if (top == -1) {

System.out.println("Stack is empty");

return;

Stack tempStack = new Stack(arr.length);

while (!isEmpty()) {

int temp = pop();

while (!tempStack.isEmpty() && tempStack.peek() > temp) {

push(tempStack.pop());

tempStack.push(temp);

while (!tempStack.isEmpty()) {

push(tempStack.pop());

ISC Computer Science | 119


public void display() {

if (top == -1) {

System.out.println("Stack is empty");

} else {

System.out.print("Stack elements: ");

for (int i = top; i >= 0; i--) {

System.out.print(arr[i] + " ");

System.out.println();

public static void main(String[] args) {

System.out.println("Initialize a stack:");

Stack stack = new Stack(6);

System.out.println("\nInput some elements on the stack:");

stack.push(1);

stack.push(3);

stack.push(2);

stack.push(0);

stack.push(7);

stack.push(5);

stack.display();

System.out.println("\nSort the elements of the stack in ascending order:");

ISC Computer Science | 120


stack.sort();

stack.display();

Variable description
variable data type Purpose
arr[] Integer to store an element
top integer to select top among elements
size integer to store elemets size

ISC Computer Science | 121


Output

ISC Computer Science | 122


Question:24
A Queue is a linear data structure in which the operations are performed based
on FIFO (First In First Out).

Define a class Queue with the following details:

Class name :Queue

Data member/instance variable:

queue[] array to hold the integer


maxsize stores the maximum capacity of the queue
front to point the index of front
rear to pint the index of rear

Member functions/methods:

Queue(int max)
constructor to initialize the data member cap = max, front = rear = O and create
the integer array

void to add integers from the rear index if possible else display the

add_dat(int v)

message("Queue full”)to remove and return elements from

int pop_dat() front, if any, else returns -999

Specify the class Queue giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..

ISC Computer Science | 123


Source code
import java.util.Scanner;

class Queue

int front,rear;

int queue[];

int maxsize;

Queue()

queue= new int [5];

rear=-1;

front=-1;

maxsize=5;

public static void main()

Scanner sc = new Scanner(System.in);

Queue ob = new Queue();

while(true)

System.out.println("\f***MENU****");

System.out.println("==============");

ISC Computer Science | 124


System.out.println("\n1.Insert");

System.out.println("\n2.Delete");

System.out.println("\n3.Display");

System.out.println("\n4");

System.out.println("Enter your choice:");

int ch = sc.nextInt();

switch(ch)

case 1:

int item;

System.out.println("Enter the element\n");

item =sc.nextInt();

ob.insert(item);

break;

case 2:

ob.delete();

break;

case 4:

System.out.println("Terminating the element");

System.exit(0);

break;

default:

System.out.println("\nInvalid choice");

ISC Computer Science | 125


}

void insert(int item)

if(rear==maxsize-1)

System.out.println("\n over flow");

return;

if(front ==-1 && rear==-1)

{ front=rear=0;

rear=rear+1;

queue[rear]=item;

System.out.println("item value\n");

void delete()

int item;

if(front==-1||front>rear)

System.out.println("\n under flow");

ISC Computer Science | 126


return;

else

item=queue[front];

if(front==rear)

front =rear=-1;

else

front=front+1;

System.out.println("\n value deleted");

void display()

if(rear==-1)

System.out.println("empty queue");

else

System.out.println("Printing value");

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

ISC Computer Science | 127


{

System.out.println(queue[i]);

Variable description
variable data type Purpose
front Integer to store an element from front
rear integer to store element from last
queue[] array to store array
maxsize integer to store elemets size

ISC Computer Science | 128


Output

ISC Computer Science | 129


Question:25
A Queue is a linear data structure in which the operations are performed based
on FIFO (First In First Out).

Define a class Queue with the following details:

Class name :Queue

Data member/instance variable:

cap[] array to hold the integer


maxsize stores the maximum capacity of the queue
front to point the index of front
rear to pint the index of rear

Member functions/methods:

Queue(int max)
constructor to initialize the data member cap = max, front = rear = O and create
the integer array

void to add integers from the rear index if possible else display the

add_dat(int v)

message("Queue full”)to remove and return elements from

int pop_dat() front, if any, else returns -999

Specify the class Queue giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined

ISC Computer Science | 130


Source code
class Queue {

int front, rear, size;

int capacity;

int array[];

public Queue(int capacity)

this.capacity = capacity;

front = this.size = 0;

rear = capacity - 1;

array = new int[this.capacity];

// Queue is full when size becomes

// equal to the capacity

boolean isFull(Queue queue)

return (queue.size == queue.capacity);

// Queue is empty when size is 0

boolean isEmpty(Queue queue)

ISC Computer Science | 131


return (queue.size == 0);

// Method to add an item to the queue.

// It changes rear and size

void enqueue(int item)

if (isFull(this))

return;

this.rear = (this.rear + 1)

% this.capacity;

this.array[this.rear] = item;

this.size = this.size + 1;

System.out.println(item

+ " enqueued to queue");

public void disp() /* function to display the elements of the queue */ {

System.out.print("\nThe elements in the queue are:");

if(front == -1) {

System.out.print("\nQueue is Empty");

else {

ISC Computer Science | 132


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

System.out.print(array[i] + " ");

System.out.println();

// Driver class

public class Main {

public static void main(String[] args)

Queue queue = new Queue(1000);

queue.enqueue(10);

queue.enqueue(20);

queue.enqueue(30);

queue.enqueue(40);

queue.disp();

System.out.println();

}}

ISC Computer Science | 133


Variable description
variable data type Purpose
front Integer to store an element from front
rear integer to store element from last
queue[] array to store array
capacity integer to store element
maxsize integer to store elemets size

ISC Computer Science | 134


Output

ISC Computer Science | 135

You might also like