Class 12 ISC - Java Stack & Queue Programs

Class 12th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 12th java topics includes revision of class 11th, constructors, user-defined methods, objects and classes, library classes , etc.
Program 1:

Stack is kind of data structure Which can store element with the restriction That an element can be added in remove from the top end only.
The details of the class Stack is given below :

Class name : Stack

Data Members / Instance variables :

cha[] : array to hold the characters
size :  stores the maximum capacity of the stack
top :  to point the index of the topmost element of the stack.

Member functions/methods:

Stack(int mm) :  constructor to initialize the data member size = mm , top = -1 and create the character array.
void push_char(char v) : to add characters from the top end if possible else display the message("Stack full")  
char pop_char() : to remove and return characters from the top end, if any else return '$' . 
void display() : to display elements of the stack 
    
Specify the class Stack, giving the details of void push_char(char) and char pop_char( ). Assume that the other functions have been defined. 
The main( ) function and algorithm need NOT be written.
                                
Solution:
class Stack
{
char cha[];
int top;
int size;

// creating a stack
Stack(int mm)
{
    size=mm;
    top=-1;
    cha=new char[size];
}

//Adding element in the top of stack
public void push_char(char v)
{
    if(top ==(size-1))
    System.out.println("Stack full");
    
    else{
    System.out.println("Inserting: "+v);
    top=top+1;
    cha[top]=v;
    }
}
public char pop_char()
{
if(top==-1)
{
System.out.println("Stack is empty");
return '$';
}
else{
char val=cha[top];
top=top-1;
return val;
}
}

public void display()
{
    
    for(int i=0;i< top;i++)
    {
        System.out.print(cha[i]+",");    
    }
    System.out.print(cha[top]);
}
public static void main(String args[])
{
Stack stack=new Stack(10);
// adding element
stack.push_char('a');
stack.push_char('b');
stack.push_char('c');

// display stack
System.out.println("Current Stack: ");
stack.display();

//remove element
System.out.println("\n\nDeleting Element");
char ele=stack.pop_char();
System.out.println("Element Removed:"+ele);

// display stack
System.out.println("Current Stack: ");
stack.display();

}
}



Program 2:

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:

dat[ ] : array to hold the integer elements
cap : stores the maximum capacity of the queue
front : to point the index of the front
rear : to point the index of the rear.

Member functions/methods:

Queue(int max) : constructor to initialize the data member cap = max, front = rear = 0 and create the integer array
void add_dat(int v) : to add integers from the rear index if possible else display the message(“Queue full”)
int pop_dat( ) : to remove and return elements from front, if any, else returns -999
void display() : to display elements of the queue

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

The main( ) function and algorithm need NOT be written.
                
Solution:

class Queue
{
int dat[];
int cap, front, rear;
public Queue(int size)
{
  cap=size;
  dat = new int[cap];
  front=rear=0;
}
public void add_dat(int v)
{
 if(rear<=cap-1)
{
dat[rear]=v;
rear++;
}
else
System.out.println("Sorry Queue is full");        
 }
 public int pop_dat()
{
if(front!=rear)
{
return dat[front++];
 }
else
{
System.out.println("Sorry Queue is empty");
 return -999;
}            
}
 public void display()
 {
 for(int i=front; i< srear; i++)
 {
 System.out.print(dat[i]+" ");
}
}
}



Testing Queue :
class TestQueue
{
 public static void main(String ar[])
 {
     Queue obj=new Queue(3);
     
     obj.add_dat(5);System.out.println("Data added");
     obj.add_dat(6);System.out.println("Data added");
     obj.add_dat(7);System.out.println("Data added");
     
     //Displays Sorry queue is full in the execution of line below
     obj.add_dat(8);
     
     System.out.println("---------Data in Queue after addition-------");
     obj.display();        
     
     System.out.println(obj.pop_dat()+" deleted from queue");
     System.out.println(obj.pop_dat()+" deleted from queue");
     System.out.println(obj.pop_dat()+" deleted from queue");
     
     //Displays Sorry queue is empty in the execution of line below
     obj.pop_dat();        
    
     System.out.println("---------Data in Queue afer deletion-------");
     obj.display();
     
     
     
 }
}



Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com