Stack Queue 5
Stack Queue 5
A Stack is a linear data structure in which the operations are performed based
on LIFO (Last In First Out).
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)
Specify the class Stack giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..
class StackArray1
int arr[],N,top;
int size;
StackArray1()
N=size;
top=-1;
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
for(int i=0;i>=0;i++)
System.out.println(arr[i]);
while(ans==1)
System.out.println("\f***MENU****");
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
break;
case 3:
ob.pop();
break;
default:
}
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
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)
Specify the class Stack giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..
top = -1;
if (top == arr.length - 1) {
System.out.println("Stack is full");
} else {
top++;
arr[top] = num;
System.out.println("Stack Underflow");
return -1;
} else {
top--;
return poppedElement;
if (top == -1) {
System.out.println("Stack is empty");
return -1;
} else {
return arr[top];
if (top == -1) {
System.out.println("Stack is empty");
} else {
System.out.println();
System.out.println("Initialize a stack:");
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.display();
stack.pop();
stack.pop();
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
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)
Specify the class Stack giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..
top = -1;
if (top == arr.length - 1) {
System.out.println("Stack is full");
} else {
top++;
arr[top] = num;
}A
System.out.println("Stack Underflow");
return -1;
} else {
top--;
return poppedElement;
if (top == -1) {
System.out.println("Stack is empty");
return -1;
} else {
return arr[top];
if (top == -1) {
System.out.println("Stack is empty");
return;
while (!isEmpty()) {
push(tempStack.pop());
tempStack.push(temp);
while (!tempStack.isEmpty()) {
push(tempStack.pop());
if (top == -1) {
System.out.println("Stack is empty");
} else {
System.out.println();
System.out.println("Initialize a stack:");
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(0);
stack.push(7);
stack.push(5);
stack.display();
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
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)
Specify the class Queue giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined..
class Queue
int front,rear;
int queue[];
int maxsize;
Queue()
rear=-1;
front=-1;
maxsize=5;
while(true)
System.out.println("\f***MENU****");
System.out.println("==============");
System.out.println("\n2.Delete");
System.out.println("\n3.Display");
System.out.println("\n4");
int ch = sc.nextInt();
switch(ch)
case 1:
int item;
item =sc.nextInt();
ob.insert(item);
break;
case 2:
ob.delete();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("\nInvalid choice");
if(rear==maxsize-1)
return;
{ front=rear=0;
rear=rear+1;
queue[rear]=item;
System.out.println("item value\n");
void delete()
int item;
if(front==-1||front>rear)
else
item=queue[front];
if(front==rear)
front =rear=-1;
else
front=front+1;
void display()
if(rear==-1)
System.out.println("empty queue");
else
System.out.println("Printing value");
for(int i=front;i<=rear;i++)
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
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)
Specify the class Queue giving the details of void add_dat(int) and int
pop_dat(). Assume that the other functions have been defined
int capacity;
int array[];
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
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
if(front == -1) {
System.out.print("\nQueue is Empty");
else {
System.out.println();
// Driver class
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.disp();
System.out.println();
}}